When I use:
char c1 = '\u534e';
System.out.println(+c1);
Then I get:
21326
But when I use:
char c1 = '\u534e';
System.out.println("c1 = " +c1);
Then I got the char printed correctly:
c1 = 华
What's the problem?
When I use:
char c1 = '\u534e';
System.out.println(+c1);
Then I get:
21326
But when I use:
char c1 = '\u534e';
System.out.println("c1 = " +c1);
Then I got the char printed correctly:
c1 = 华
What's the problem?
There are 3 scenarios in your question :
1. System.out.println(+c1);
Adding + with a char type will convert it to int, so it will call println(int) method of System.out, making it to print an integer value for that character.
2. System.out.println(c1);
In this case we haven't added +, so it is still taking as char, so in this case println(char) will be called and this will cause actual char to be printed.
3. System.out.println("c1 = " +c1);
When we concatenate a char type to String, whole concatenation will be treated as String, so in this case println(String) will be called and actual character with rest of the string get printed.