I am trying to understand how the compiler views the following print statements. It is simple yet a bit intriguing.
This prints the added value. Convincing enough.
System.out.println(1+2); //output: 3
The output from the following looks convincing as well:
System.out.println(1+2+"3");//output: 33
My question (based on the behavior above) is here.
System.out.println("1"+2+3);//Should be 15 right? NO. It is 123.
I tried few other such statements which were along the same lines. I was able to see one two clear behaviors.
If integers are at the front, without the quotes, they are added and subsequent integers are just appended as suffix to the added values from the front.
if the statement starts with string, under quotes, then all other subsequent elements get appended as suffix.
Is this somewhere in the java api docs? Or just very obvious string or add operator behavior which I am not seeing. Any of your valuable insights will be appreciated.
Here is the code snippet:
public static void main(String[] args) {
System.out.println(1+2);
System.out.println(1+2+"3");
System.out.println("1"+2+3);
System.out.println("1"+2+"3");
System.out.println("1"+2);
}
// The Console output:
// 3
// 33
// 123
// 123
// 12
I guess the Java specification explains it best:
The + operator is syntactically left-associative, no matter whether it
is determined by type analysis to represent string concatenation or
numeric addition. In some cases care is required to get the desired
result. For example, the expression:
a + b + c
is always regarded as meaning:
(a + b) + c
So, if a
was a String
, then it concatenates with b
. The result would be of String
type, hence it continues to concatenate with c
.
I also found this interesting. I haven't worked too much in java recently but I found out a little bit of info that might be helpful through playing with it.
I think this has to do with automatic Type-casting that java does.
System.out.println("1"+2+3);
Prints 123 as you said. Since "1" is casted as string, Java assumes since the first one was a string, the ones following will be a string for concatenation unless otherwise noted
Although, this result does print 15 when you define the type
System.out.println("1" + (int)(2+3));
In that case, it can complete the operation before concatenating.
So I think java is assuming if the first one is a String, that the rest are going to be strings and to concatenate them.
EDIT: you can see some info about automatic type-conversion on oracle's website here
The +
operator is overloaded in the compiler. If both operands are numeric, +
is addition. If either or both operands are String, +
is string concatenation. (If one operand is String and the other is numeric, the number is cast to a String). Finally, +
binds left-to-right.
All this causes a compound formula a + b + c + ...
to do addition left-to-right until it hits the first string operand, at which point it switches to string concatenation for the remainder of the formula. So...
"1" + 2 + 3 + 4 = 1234 /* cat cat cat */
1 + "2" + 3 + 4 = 1234 /* cat cat cat */
1 + 2 + "3" + 4 = 334 /* add cat cat */
1 + 2 + 3 + "4" = 64 /* add add cat */
Thats interesting part of string.
When a String is added to any other data type, the resultant value is a String.The other variable is also converted to a String and then concatenated. However, when two integers are operated with a + sign, the + acts as an addition operator and not a concatenation operator.
If the expression within the println() or print() method contains parentheses, then the value within the parentheses is evaluated first. Consider the following example:
int a = 5;
int b = 6;
System.out.println(a + b); // Output will be: 11
System.out.println("5" + "6"); // Output will be: 56
System.out.println("" + a + b); // Output will be: 56
System.out.println(5 + 6 + a + " " + b + a); // Output will be: 16 65
System.out.println("Result: " + a + b); // Output will be: 56
System.out.println("Result: " + (a + b)); // Output will be: 11
You can see the difference between last two sysout statements