Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, why the output of int a=('a'+'b'+'c'); are different form System.out.println('a'+'b'+'c'+"")

Tags:

java

the original question is like this.

public class test {
    public static void main(String[] args){
        int i = '1' + '2' + '3' + "";
        System.out.println(i);
    }
}

and this gives me an error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from String to int

then I changed the code like this:

public class test {
    public static void main(String[] args){
        int i = '1' + '2' + '3';
        System.out.println(i);
    }
}

the out put is 150.

but when I write my code like this:

public class test {
    public static void main(String[] args){
        System.out.println('a'+'b'+'c'+"");
    }
}

the output become 294.

I wonder why.

like image 332
Retaker Wang Avatar asked Feb 17 '15 14:02

Retaker Wang


People also ask

What is the difference between system out print and system out Println?

The main difference between the two is that print() retains the cursor in the same line after printing the argument, while println() moves the cursor to the next line.

Why do we use system out Println in Java?

Java System. out. println() is used to print an argument that is passed to it.

Why s1 == s2 is false?

The s1 is in the string pool whereas s2 is created in heap memory. Hence s1==s2 will return false.

What is System Out :: Println?

In Java, System. out. println() is a statement which prints the argument passed to it. The println() method display results on the monitor. Usually, a method is invoked by objectname.


2 Answers

  • The first one does not compile, because you concatenate a String at the end which cause the value to be a String which can't be converted directly to int.
  • The output of the second one is 150, because ASCII value for character 1,2,3 are 49,50,51 which return 150 when doing the addition.
  • The output of the last one is 294, because you are doing an addition of char values in the ASCII table (97+98+99)

You can verify the values here for a,b and c (or any other character).

Edit : To explain why the last one output the correct value instead of throwing an error, you first sum all the values as explained before, then convert it to a String adding "" to the sum of the ASCII values of the chars. However, the println method expect a String which is why it does not throw any error.

The first one would work if you would do Integer.parseInt('1' + '2' + '3' + "");

like image 120
Jean-François Savard Avatar answered Nov 15 '22 00:11

Jean-François Savard


When you do this

int i = '1' + '2' + '3';

the JVM sums the ASCII codes of the given numbers. The result is 150.

When you add the empty String, you are trying to sum an int/char with a String. This is not possible. You can implicitly convert char to int and vice versa because they are primitive types. You cannot do this with String objects because they are not primitives but references. That's why you get an error.

When you do the println the primitive values are firstly summed and the automatically boxed into reference type so the sum is boxed into a Character object. The empty String is converted to a Character and then is added to the first one. So the result is a Character object that has an ASCII code 294. Then the toString method of the Character is called because that's what the println(Object) method does. And the result is 294

I hope this will help you to understand what is happening :)

like image 36
Kiril Aleksandrov Avatar answered Nov 14 '22 23:11

Kiril Aleksandrov