How does Java concatenate 2 lines?

Why is the next print 197 but not "bc"?

System.out.println('b' + 'c');

Can someone explain how to do proper concatenation in Java?

PS I have learned some Python and am now moving on to learning Java.

+3
source share
6 answers

'b'and 'c'not Strings, they are chars. Instead, use double quotes "...":

System.out.println("b" + "c");

You get intbecause you add the unicode values ​​of these characters:

System.out.println((int) 'b'); // 98
System.out.println((int) 'c'); // 99
System.out.println('b' + 'c'); // 98 + 99 = 197
+6
source

'b'is not Stringin Java, it is char. Then 'b'+'c'prints 197.

"b"+"c", bc, "" String.

System.out.println("b" + "c"); // prints bc
+1

+ char ascii , , . bc , b c String. b c char Java.

Java "", ''

+1

, Char, :

System.out.println("b" + "c");

:

"" + char1 + char2 + char3;

new StringBuilder().append('b').append('c').toString();
+1

Java Strings - ""

char. :

System.out.println("b" + "c"); // bc

, , ASCII chars, 197.

ASCII 'b' 98, ASCII 'c' 99.

:

System.out.println('b' + 'c'); // 98 + 99 = 197

ASCII chars:

char - 16- .

Docs. (0-255), , chars ASCII, ASCII - . .

, ASCII , , 256 ASCII ( ) - .

, , - ASCII ( Unicode). Unicode.

+1

'b' 'c' , . 197 - unicode b c

Concatinating String :

  • System.out.println("b"+"c");
  • System.out.println("b".concat("c"));
+1

All Articles