Why is java print character number?

I have the following class, see

   public class Go {
     public static void main(String args[]) {
      System.out.println("G" + "o");
      System.out.println('G' + 'o');
     }
   }

And this is the result of compilation;

  Go
  182

Why does my output contain a number?

+5
source share
7 answers

In the second case, it adds the unicode codes of two characters (G - 71 and o - 111) and prints the sum. This is due to the fact that it is charconsidered as a numerical type, therefore the operator +is the usual summation in this case.

+8
source
Operator

+with char constant 'G' + 'o'prints the charCode addition and the string concatenation operator c "G" + "o"will print Go.

+2
source
System.out.println("G" + "o");
  System.out.println('G' + 'o');

+ concat . ASCII ( UNICODE) .

+1

Java , , .

( char, ).

+1

SO , ASCII (71 G) + (111 o ) = 182, ).

String.valueOf(char c), .

+1

+ sum ( ), , String, String, String .

, , char java .

(71+111)=182, g + o= go

String, System.out.println('G' + "o") go, .

0

The + operator is defined for intboth and String:

int + int = int

String + String = String

When adding char + char, the best match would be:

(char->int) + (char->int) = int

But ""+'a'+'b'will give you ab:

( (String) + (char->String) ) + (char->String) = String
0
source

All Articles