This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread 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 Murach's Java Servlets and JSP this week in the Servlets 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..!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sout- concatenation
 
Similar Threads
concatenation
Doubt in String concatenation (KB Chapter 4)
doubt about string concatenation
Concatination Operator
Assertion topic