This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sout- concatenation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "sout- concatenation" Watch "sout- concatenation" New topic
Author

sout- concatenation

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Here goes the code:

[code]
class Feline{
public static void main(String[] args){
Long x = 42l;
Long y = 44l;
System.out.println(" " + 7 + 2 + ""); //line1
System.out.println(foo() + x + 5 + ""); //line2
System.out.println(x + y + foo()); //line3
}
static String foo(){return "foo";}
}
[\code]

My Doubt:
Why in line 1 the output is 72?
and why in line 3 the output is 86foo?

I don't see any consistency.. Can somebody please explain?
Thanks.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guptajee,





My Doubt:
Why in line 1 the output is 72?
and why in line 3 the output is 86foo?


if any one of the left and right operand of the "+" concatenation operator is String, the resultant would be String concatenation.
If both are number it would be addition.

Remember left to right operation.

Thanks and Regards,
cmbhatt


cmbhatt
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Thanks..Got it..!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: sout- concatenation