File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The + operator is confusing, help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The + operator is confusing, help!" Watch "The + operator is confusing, help!" New topic

The + operator is confusing, help!

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
What is the output for the following lines of code? /please explain in terms of precedence of string concatenation vs, addition. ans is 5.
1: System.out.println(" " +2 + 3);
2: System.out.println(2 + 3);
3: System.out.println(2 + 3 +"");
4: System.out.println(2 + "" +3);

A) Compilation error at line 3
B) Prints 23, 5, 5 and 23.
C) Prints 5, 5, 5 and 23.
D) Prints 23, 5, 23 and 23.
parag bharambe
Ranch Hand

Joined: Sep 01, 2000
Posts: 41
Here is the explanation
First " "+2+3 .See it is evaulation not assignment of operator. Here eveulation order is left to right. So first 2 will be converted to string( due to " ") and then 3 is converted to string.
As both of them are string they are concatenated. Ans 23
b) 2+3
herr simple addition of variable is made and displayed as 5
Is same as previous one. Only difference is that 5 is later converted to string.
d) 2+" "+3
Here 2 is converted to string and later 3 is converted into string and both are concatednated. SO answer is 23
so i think answer should be B not D.
If i am wrong then correct me.

Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Answer is b.

Joined: Nov 07, 2000
Posts: 3
Whenever a string in involved anywhere in the expression, the '+' operator is in string-context. So 2+3+"" is exactly the same as ""+2+3 and as 2+""+3 and all result in 23. So D is the correct answer
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Chris is right, folks. The correct answer is D. Whenever you have a string in an expression, the other values are promoted to strings. since "" is a string (the empty string, not the null string), 2 and 3 are promoted to "2" and "3" respectively and the + operator here functions as the concatenation operator and concatenates all the three strings.
Dilip Nedungadi

Joined: Jun 23, 2000
Posts: 3
Parag and Rob are right. Run this prog and see for yourself
public class TestStringprint
public static void main(String[] args)
System.out.println(""+2 + 3);
System.out.println(2 + 3);
System.out.println(2 + 3 +"");
System.out.println(2 + "" +3);
Paul Caudle
Ranch Hand

Joined: Jun 08, 2000
Posts: 64
Hi Folks,
If you look at D and B the only difference is the 3rd one so lets look at that:
just like pure mathematics, this line reads from left to right (since addition and concatenation have the same level of importance in order of operations here). First we evaluate only the first '+' which is 2+3...meaning 5, leaving just 5+"" which then converts it to "5" rather than "23".
Meaning the correct answer is B.
The presence of a String in an equation does not influence the outcome so much as the location of that String in the equation.
I hope that helps a bit and also that it did not come across as condescending. Have a good one.

[This message has been edited by Paul Caudle (edited November 07, 2000).]
Mapraputa Is
Leverager of our synergies

Joined: Aug 26, 2000
Posts: 10065
Chrisis, may I ask you to register with proper name?
You can read this post for more details. We are glad to see you here, just a little formality´┐Ż

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
I agree. Here's the link:
subject: The + operator is confusing, help!
jQuery in Action, 3rd edition