• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

The + operator is confusing, help!  RSS feed

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Here is the explanation
a)
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
c)
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.
parag

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer is b.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
HTH
Shashi
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parag and Rob are right. Run this prog and see for yourself
//TestStringprint.java
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);
}
}
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
If you look at D and B the only difference is the 3rd one so lets look at that:
2+3+""
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.
-Paul

[This message has been edited by Paul Caudle (edited November 07, 2000).]
 
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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´┐Ż
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!