• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sout- concatenation

 
Lovleen Gupta
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lovleen Gupta
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..Got it..!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic