• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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..!!
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic