[Java] Implicit type cast (AOJ10-sum of numbers)

Type cast

public class Main {
	public static void main(String[] args)  {

        char c = '1';
        //Implicitly cast to int type, double type
        int i = c;
        double d = c;
        System.out.println(c + "To int type →" + i);
        System.out.println(c + "To double type →" + d);

        //Pass char type as an argument to a method whose argument is double type
        doubleMethod(c);
    }

    static void doubleMethod(double d) {
        System.out.println("double type →" + d);
    }
}
Cast 1 to int type → 49
Cast 1 to double type → 49.0
double type → 49.0

Sum of numbers (ITP1-8)

Create a program that calculates the sum of each digit of a given number. Input Multiple datasets are given as input. Each dataset is given in one line containing one integer x. x is an integer of 1000 digits or less. When x is 0, the input ends. Do not output to this dataset. Output For each data set, output the sum of each digit of x on one line.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int ans;

        while(true){
    	String line = br.readLine();
    	    if(line.length() == 1 && line.equals("0")){
    	        break;
    	    }else{
        	    ans= 0;
        	    for(int i = 0; i < line.length(); i++){
        		    ans += line.charAt(i)-'0';
        	    
        	   /*Confirmation of type cast
        	   System.out.println("line.charAt(i) "+line.charAt(i));
    	       System.out.println("line[i] "+(int)line.charAt(i));
               System.out.println("(int)'0' "+(int)'0');
               */
    	       }
    	       System.out.println(ans);

    	    }
        }
    }
}  

Recommended Posts

[Java] Implicit type cast (AOJ10-sum of numbers)
[Java] Correct comparison of String type
[Basic knowledge of Java] About type conversion
[Java] Comparison of String type character strings
Java Summary of frequently searched type conversions
Java type conversion
[Introduction to Java] About type conversion (cast, promotion)
[JAVA] Stream type
[Java] Enumeration type
Java Optional type
[Java] Be careful of the key type of Map
Java double type
[Java] char type can be cast to int type
[Java] Overview of Java
Java review ③ (Basic usage of arrays / reference type)
Java variable declaration, initialization, data type (cast and promotion)
GetXxxx of ResultSet was addicted to primitive type (Java)
[Java] One type of alphabet prohibited FizzBuzz with binding [Binding]
Expired collection of java
Predicted Features of Java
[Java] Significance of serialVersionUID
NIO.2 review of java
Review of java Shilber
[Java] Data type ①-Basic type
java --Unification of comments
[Java, Kotlin] Type Variance
Java class type field
History of Java annotation
Type determination in Java
Java study # 1 (typical type)
java (merits of polymorphism)
Conditional branching of numbers
Studying Java ~ Part 8 ~ Cast
NIO review of java
[Java] About enum type
[Java] Implicit inheritance memo
[Java] Three features of Java
Summary of Java support 2018
[Java] Date type conversion
[Java] One type of alphabet prohibited With binding Hello, world! [Binding]
Initialization with an empty string to an instance of Java String type
[Java] To know the type information of type parameters at runtime
[Java] Difference between assignment of basic type variable and assignment of reference type variable