7

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.

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
Retaker Wang
  • 89
  • 1
  • 1
  • 4
  • 4
    the difference between 150 and 294 is that you did not sum the same chars ('1', '2', '3' against 'a', 'b', 'c') – yunandtidus Feb 17 '15 at 14:53
  • @RC mot a duplicate, doesn't cover what happens when you add a string to it. – djechlin Feb 17 '15 at 14:54
  • @djechlin, the selected answer, answers it. – yunandtidus Feb 17 '15 at 14:55
  • @yunandtidus it's far too dense / lengthy for this question. There is a lot of value to add with a concise answer to this question. I upvoted the concise answer below even though it's slightly incomplete. – djechlin Feb 17 '15 at 15:03
  • @yunandtidus e.g. why is int x = 'a' + 'b' + 'c' + "" bad but System.out.println('a' + 'b' + 'c' + "") good? It's a diverse question but I certainly don't want to punish the OP for doing more research, and a good answer really should cover it. – djechlin Feb 17 '15 at 15:05
  • @djechlin, i agree on the string concatenation, but the root of the problem and it's java specification are far better explained in the possible duplicate link – yunandtidus Feb 17 '15 at 15:13
  • @yunandtidus right, and this question needs to be actually open to garner a better answer. – djechlin Feb 17 '15 at 15:19

4 Answers4

8
  • 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' + "");

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
3

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 :)

Kiril Aleksandrov
  • 2,601
  • 20
  • 27
  • I think this answer catches the actual situation, plus I learned something. (Plus it firmly shows the question is not a duplicate.) – djechlin Feb 17 '15 at 15:06
1

The first is impossible because you can't convert String to int this way.

The second works because chars are kind of numbers, so adding chars is adding the numbers they really are. Char '1' is the number 49 (see ASCII table), so the sum is 49+50+51 which is 150.

The third works this way because + is a left parenthesized operator, which means that 'a'+'b'+'c'+"" should be read as (('a'+'b')+'c')+"". 'a' has ASCII code 97, so you have 294+"". Then Java knows that is should convert the value to a String to be able to catenate the two strings. At the end you have the the string 294. Modify your last code to the following System.out.println('a'+'b'+('c'+"")); and you will see that the result will be 195c.

You must note that System.out.println is a method that is used to convert values (of different types) to their String representation. This is always possible as every int can be converted to a String representation of it, but not the converse; not every String is a representation of an int (so Java will not let you do it so simply).

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
1

First: [int i = '1' + '2' + '3' + "";] If you concat an empty string value, you convert it to a String object, and then String objects can't convert to int.

Second: [int i = '1' + '2' + '3';] The binary arithmetic operations on char promote to int. It's equal to: [int i = 49 + 50 + 51] - total: 150.

Third: [System.out.println('a'+'b'+'c'+"");] At this case you convert 'a' + 'b' + 'c' (that is 294) to String (+"") and then print the result like a String value and that works ok.

Jorg8
  • 11
  • 3