3

Why does the following print 197, but not 'bc'?

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

Can someone explain how to do proper concatenation on Java?

P.S. I learnt some Python, and now transforming to learn Java.

user2988464
  • 3,251
  • 6
  • 21
  • 25
  • 5
    Well for starters those aren't Strings. String literals use double quotes. – PakkuDon Feb 10 '14 at 05:04
  • 1
    `197` is sum of unicode values of `b` and `c`, Python is different then Java in Python `'b'` and `'c'` are strings In Java they are char literals, string literals has to be double quoted `"` `"` – Grijesh Chauhan Feb 10 '14 at 05:06

6 Answers6

6

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

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

You are getting an int because you are adding the unicode values of those characters:

System.out.println((int) 'b'); // 98
System.out.println((int) 'c'); // 99
System.out.println('b' + 'c'); // 98 + 99 = 197
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • 3
    The Java language specification defines `char`s as UTF-16 code-units encoding Unicode code-points, not ASCII values. – Mike Samuel Feb 10 '14 at 05:08
1

'b' is not a String in Java it is char. Then 'b'+'c' prints 197.

But if you use "b"+"c" this will prints bc since "" used to represent String.

System.out.println("b" + "c"); // prints bc
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

Concatenating chars using + will change the value of the char into ascii and hence giving a numerical output. If you want bc as output, you need to have b and c as String. Currently, your b and c are char in Java.

In Java, String literals should be surrounded by "" and Character are surrounded by ''

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

Yes single quotation is Char while double quote represent string so:

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

Some alternatives can be:

"" + char1 + char2 + char3;

or:

new StringBuilder().append('b').append('c').toString();
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
1

In Java, Strings literals are represented with double quotes - ""

What you have done is added two char values together. What you want is:

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

What happened with your code is that it added the ASCII values of the chars to come up with 197.

The ASCII value of 'b' is 98 and the ASCII value of 'c' is 99.

So it went like this:

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

As a note with my reference to the ASCII value of the chars:

The char data type is a single 16-bit Unicode character.

From the Docs. However, for one byte (0-255), as far as I'm aware, chars can also be represented by their ASCII value because the ASCII values directly correspond to the Unicode code point values - see here.

The reason I referenced ASCII values in my answer above is because the 256 ASCII values cover all letter (uppercase and lowercase) and punctuation - so it covers all of the main stuff.

Technically what I said is correct - it did add the ASCII values (because they are the same as the Unicode values). However, technically it adds the Unicode codepoint decimal values.

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • 1
    [Unicode, not ASCII](http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html) – Christian Tapia Feb 10 '14 at 05:10
  • @Christian I know that chars are represented with unicode characters. However, ASCII values correspond to that nicely for explanation. – Michael Yaworski Feb 10 '14 at 05:11
  • 3
    @mikeyaworski Unicode indexes correspond even nicer since they are actually used here. – Pshemo Feb 10 '14 at 05:13
  • @Pshemo I have a hard time finding how Unicode indexes can represent `'b'` as `98`. I'm just saying it's easier to point people in the ASCII direction for this sort of question because the ASCII values (for a byte, I think) are the same thing. – Michael Yaworski Feb 10 '14 at 05:19
  • @mikeyaworski Here you go: http://unicode-table.com/en/search/?q=b Take a look at `b` to see decimal value of character index or `U+0062` to hexadecimal value. You can also use `System.out.pring((int)'b');` – Pshemo Feb 10 '14 at 05:21
  • @mikeyaworski http://stackoverflow.com/questions/10361579/are-unicode-and-ascii-characters-the-same – Christian Tapia Feb 10 '14 at 05:22
  • @Christian: That's what I've said. ASCII values don't cover everything, but they cover the main stuff (punctuation and letters) and directly correspond to the Unicode code point values (so there's no harm in referencing ASCII values for letters). It just seemed easier to find an ASCII table on the internet with less clutter than a Unicode table. I'm aware of the technicalities. – Michael Yaworski Feb 10 '14 at 05:25
  • 1
    @mikeyaworski Then point it in a note in your answer. You are aware of technicalities, but people may not. – Christian Tapia Feb 10 '14 at 05:26
  • @Christian I'll write that up. I'm worried of confusing people sometimes though. – Michael Yaworski Feb 10 '14 at 05:28
1

'b' and 'c' are not Strings they are characters. 197 is sum of unicode values of b and c

For Concatinating String you can use following 2 ways:

  • System.out.println("b"+"c");
  • System.out.println("b".concat("c"));
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55