[Java] char type can be cast to int type

Introduction

When I was reading the Java SE8 Silver problem book, it said, "Char type can be implicitly cast to int type or double type." I'm ashamed to say that I've been using Java for years in my business, but I didn't know this fact at all.

So, I tried to find out the reason why char type can be cast to int type or double type, but the reason was not written in the problem collection, so I investigated it myself. I tried to summarize the results.

Code used for investigation

TypeCast.java


public class TypeCast {
	public static void main(String[] args) {
		char ch = '1';

		//Implicitly cast to int type or double type.
		int i = ch;
		double d = ch;
		System.out.println(ch + "Result of casting to int type: " + i);
		System.out.println(ch + "Result of casting to double type: " + d);

		//Pass char type as an argument to a method whose argument is double type.
		foo(ch);
	}

	static void foo(double d) {
		System.out.println("double type: " + d);
	}
}

Output result

The result of casting 1 to int type: 49
The result of casting 1 to double type: 49.0
double type: 49.0

The reason why casting a char type "1" to an int type results in "49"

Looking at the char type wrapper class Character type API, "Unicode character representation" Is written. So if you look at "1" in the Unicode table, the value is "U + 0031". You can see that it is represented by.

The values in the Unicode table are in hexadecimal, which is actually "0x31", so if you change this to decimal, it will be "49". In other words, if the char type "1" is implicitly cast to an int type, this "49" is stored in an int type variable.

Summary

Recommended Posts

[Java] char type can be cast to int type
Whether options can be used due to different Java versions
Java to be involved from today
How to use Java enum type
Java arguments to run with gradle run can be specified with --args (since Gradle 4.9)
Introduction to Java that can be understood even with Krillin (Part 1)
[Java] Implicit type cast (AOJ10-sum of numbers)
[java] Summary of how to handle char
[Java] Convert Object type null to String type
Reason to add L to the number to be put in Java long type
Type conversion from java BigDecimal type to String type
Things to be aware of when writing Java
[Java] Be careful of the key type of Map
There seems to be no else-if in java
Java type conversion (String, int, Date, Calendar, etc.)
Minecraft BE server development from PHP to Java
[Java] Incompatible types error occurred when assigning a character string to a char type variable
[JAVA] Stream type
[Java] Enumeration type
Java Optional type
Check if a string can be converted to an int without using Integer # parseInt
Java double type
[Java] Introduction to Java
Introduction to java
Java variable declaration, initialization, data type (cast and promotion)
Write a class that can be ordered in Java
java: How to write a generic type list [Note]
Stream processing of Java 8 can be omitted so far!
GetXxxx of ResultSet was addicted to primitive type (Java)
[Java] Things to note about type inference extended in Java 10
Java variable scope (range where variables can be seen)
[Java] Things to be aware of when outputting FizzBuzz