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

the original question is 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 as follows:

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

out is 150.

but when I write my code as follows:

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

the output becomes 294.

I wonder why.

+7
java
source share
4 answers
  • The first one does not compile because you concatenate the string at the end, which causes the String value, which cannot be converted directly to int.
  • The second output is 150, because the ASCII value for the 1,2,3 character is 49.50.51, which returns 150 when the addition is done.
  • The result of the latter is 294, because you add char values ​​to the ASCII table (97 + 98 + 99)

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

Edit:. To explain why the latter displays the correct value instead of throwing an error, you first sum all the values, as explained earlier, and then convert them to a string that adds "" to the sum of the ASCII character values. However, the println method expects a String, so it does not cause any errors.

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

+7
source share

When you do it

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

The JVM sums the ASCII codes of the given numbers. Result 150 .

When you add an empty String , you are trying to sum int / char with String . It's impossible. 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 links. This is why you get an error message.

When you execute println , primitive values ​​are first summed and automatically placed in the box in the reference type, so the sum is placed in the Character object. An empty String converted to Character , and then added to the first. Thus, the result is a Character object that has an ASCII code of 294. The toString Character method is then called because the println(Object) method does this. And result 294

Hope this helps you understand what is happening :)

+2
source share

The first is not possible because you cannot convert String to int this way.

The second one works because char is a kind of number, so adding char adds the numbers that 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 the operator in brackets on the left, which means that 'a'+'b'+'c'+"" should be read as (('a'+'b')+'c')+"" . 'a' has an ASCII code of 97, so you have 294+"" . Then Java knows that you need to convert the value to String in order to be able to bind two strings. At the end you will get line 294 . Change your last code to the following System.out.println('a'+'b'+('c'+"")); , and you will see that the result will be 195c .

You should notice that System.out.println is a method that is used to convert values ​​(of different types) to their String representation. This is always possible, since each int can be converted to a String representation, but not vice versa; not every String is an int representation (therefore, Java will not allow you to do this so simply).

0
source share

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

Second: [int i = '1' + '2' + '3';] Binary arithmetic operations on char are advanced to int. It is equal to: [int i = 49 + 50 + 51] - total: 150.

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

0
source share

All Articles