• 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

+ operator doubt

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello



The output of above code is
1234
46
I know the concept of '+' operator behaviour when used with strings.
Can someone please explain what happened here to the '+' operator precedence by just changing the string literal location.

Thanks
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in line 1 the compiler first encouters "", as it scans from left to right, hence it converts everything that follows it into string..

in line 2 it first encounters an integer hence performs addition and then int + string-->string
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int a1 = 12;
int a2 = 34;
System.out.println(""+a1+a2);
System.out.println(a1+a2+"");

""+a1+a2 -> ""+a1 = 12 [String] so "12"+a2 = 1234 {Stirng concantenation}
If one of the operands to + is a string it performs concantenation

a1+a2+"" -> a1+a2 = 46 [int] and now 46+"" = 46 {String}
Thanks
Deepak
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to understand when string conversion happens.
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.4

Basically if any of the operands of + is a string then other one will be converted to a string. And also + is left associative. So an expression with + gets evaluated from left to right.

System.out.println(""+a1+a2);
System.out.println(a1+a2+"");



So for the first statement, a1 gets converted in a string, and then a2.
But in the second statement, first a1 and a2 get added, and then the result is converted to a string.
In fact following statement will give same result as 1st one

System.out.println(a1+(a2+""));



Thanks.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you put System.out.println(""+12+34) - it take the final output as String.

when you put System.out.println(12+34+"") - it takes the final output as String but before that it adds both the integers.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Operators are evaluated based on precedence.
Operators of same precedence are evaluated from left to right.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic