2

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?

Tom
  • 16,842
  • 17
  • 45
  • 54
Leo Li
  • 81
  • 1
  • 7
  • This all boils down to what `+c1` in `System.out.println(+c1);` really is, so I guess the dupe is good here. – Tom May 07 '17 at 11:58

1 Answers1

2

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.

Nishesh Pratap Singh
  • 2,131
  • 2
  • 13
  • 20
  • THANK YOU FOR THE BRILLIANT ANSWER! But I still have a little confusion: Why "System.out.println(+c1)" made the char type to the int type? I mean when I typed with the System.out.println() format, shouldn't everything in bracket be changed to char type? – Leo Li May 08 '17 at 00:30