Char c = 7; Why will this statement be executed in java without errors?

char c=7; 

The above statement will be executed in java without errors, even if we assign a number to a symbol. where 7 is not a character. Why is this done?

+4
source share
5 answers

Because in Java, char is an integral data type whose values ​​are 16-bit unsigned integers representing UTF-16 code units. Since char is a numeric data type, when you assign it a numeric value, it simply accepts the encoding of any Unicode character represented by that value.

Run the following two lines of code:

 char c = 65; System.out.println("Character: " + c); 

You will see the result:

 Character: A 

(I would use 7, as in the example, but this is a non-printable character.) The "A" character is printed because the decimal value 65 (or 41 hex) is encoded in that letter of the alphabet. See Joel Spolsky's article Absolute Minimum Every software developer Absolutely, should know positively about Unicode and character sets (no excuses!) For more information on Unicode.

Update:

If you say that assigning an int value to char usually gives a compiler error "a possible loss of precision", as shown in the following code:

 int i = 65; char c = i; System.out.println("Character: " + c); 

The answer is what PSpeed ​​mentioned in his comment. The first (2-line) version of the code performs literal assignment because the value is known at compile time. Since the value 65 is in the correct range for char ('\ u0000' to '\ uffff' inclusive or from 0 to 65535), an assignment is allowed. In the second (3-line) version, assignment is not allowed, since the int variable can take any value from -2147483648 to 2147483647 inclusive, most of which are outside the range for the char variable that should contain.

+13
source

Read the http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html , in particular the Unicode Character Representation section. The raw value of a character has an integer equivalent.

+5
source

Implicit conversion takes place. The compiler can check the range.

+4
source

Note the difference between the two outputs:

  int x = 47; System.out.println('[' + x + ']'); // 231 (= 91 + 47 + 93) System.out.println("[" + x + "]"); // [47] 

A char is a character and a number. (Floor wax and dessert tops.)

+2
source

It covertly hides the ascii character.

-4
source

All Articles