2

I came across a case in which java prints character are ASCII. If we try to print two character then its prints the sum of there ASCII value.

System.out.println('c'); =>> c
System.out.println('a'+'b'); =>> 195 (97+98)

Just want to know why in second case it prints the sum of there ASCII value

dead programmer
  • 4,223
  • 9
  • 46
  • 77

8 Answers8

4

Because a char can also be considered an int. What you're actually doing there is just adding 2 integers.

In your first example System.out.println('c'); refers to System.out.println(char); but in the second example 'a'+'b' is an int so you're calling System.out.println(int);

to output it as a string, convert it to a string

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

Thomas Nairn
  • 1,186
  • 8
  • 34
3

Behind the scenes, the compiler is acting too smart and replacing char + char with an int constant value.

In the first case println(char) is called and in the second case println(int) is called

Sample code :

public static void main(String[] args) {
    System.out.println('a');
    System.out.println('a' + 'b'); // the compiler first "resolves" the expression 'a'+'b' (since char cannot be added they are added as ints) and then tries to call the correct `println(int)` method.
}

Byte Code:

public static void main(java.lang.String[]);
   descriptor: ([Ljava/lang/String;)V
   flags: ACC_PUBLIC, ACC_STATIC
   Code:
     stack=2, locals=1, args_size=1
        0: getstatic     #16                 // Field java/lang/System.out:Ljav
/io/PrintStream;
        3: bipush        97                 // for single char. (pushed a byte as an int)
        5: invokevirtual #22                 // Method java/io/PrintStream.prin
ln:(C)V
        8: getstatic     #16                 // Field java/lang/System.out:Ljav
/io/PrintStream;
       11: sipush        195                // push sum of 2 chars as a short
       14: invokevirtual #28                 // Method java/io/PrintStream.prin
ln:(I)V
       17: return
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
2

For char Ascii values is used and that is a int val. There addition of int val is being calculated. But for below case "a"+"b", it is stringBuilder class which will concatenate two strings. System.out.println("a"+"b");--> ab

Rahul
  • 3,479
  • 3
  • 16
  • 28
  • *it is string operator overloaded method* - No. Java has no such thing as *operator overloading*. The compiler will use `StringBuilder` to add `"a"+"b"` – TheLostMind May 15 '15 at 13:10
2

Because char (not Character) is an unsigned integral type behind the scenes. However, when you add two char, you get an int.

What did you expect?

Ingo
  • 36,037
  • 5
  • 53
  • 100
2

Java uses double quotes to define String literals, and single quotes to define char literals - so this example will concatenate the two Strings:

    String strA = "a";
    String strB = "b";
    System.out.println(strA + strB);

..whereas this example will print the result of adding the 2 char integer values together:

    char charA = 'a';
    char charB = 'b';
    System.out.println(charA + charB);
phil76
  • 134
  • 7
1

Behind the scenes a char is represented by an int so when you add 2 chars the result will be an int representing the sum of their ASCII value.

Stephan
  • 8,000
  • 3
  • 36
  • 42
1

Java is not a complete OO language like rupy / python. Primitives are most important part of java. So in your case in the second statment it will use ascii operation only. It won't concat the chars and won't deliver the "ab". Charaters are mentioned within single quotes. Strings are within double quotes. If you pass "a" + "b" = "ab" because here you are adding two strings not a characters.

Hakuna Matata
  • 755
  • 3
  • 13
0

The + operator for char is defined as the sum of their integer values, resulting in an integer.

Because you ask about ASCII, we have to explain the sum this way…

A Java char holds a UTF-16 code-unit value, UTF-16 is an encoding for Unicode, Unicode is a superset of ASCII and UTF-16 encodes characters in the common subset with the same values as ASCII does, and in this code the size of a UTF-16 code-unit (two bytes) being different than the size of an ASCII code-unit (one byte) doesn't matter.

So, when you sum two char values that hold characters in the ASCII subset, the sum is the same as summing their ASCII values.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72