• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sout- concatenation

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..Got it..!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic