• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Ternary Operator Precedence while doing String concatenation

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am not able to understand the precedence of ternary operator while doing string concatenation

Source :Self

public class StringTest {

public static void main(String[] args)
{
final String s1 = "Foo";
final String s2 = "Bar";

final boolean b = true;
String s3 = b?s1:s1 + s2;

System.out.println("s3 is when no brackets around ternary operator " + s3); //s3 = "Foo" // 1

String s4 = (b?s1:s1) + s2;
System.out.println("s4 is when brackets around ternary operator " + s4); //s4 = "FooBar" //2
}
}

Can anyone please explain me at //1 and //2 places?
It may be silly one , but i am not able to figure it out.
please help me
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rami marri:
Can anyone please explain me at //1 and //2 places?
It may be silly one , but i am not able to figure it out.
please help me




Hi,

I donot quite remember the precidence of the operators but I do know that whatever is in the brackets (if there is one present) then it is solved first,irrespective of the precidence. Or rather the brackets have the highest precidence.

So in the first case,
the output is as expected.

In the second case,
the brackets is solved first - and it would return s1 whether true or false as both the conditions have s1
Then this result is concatenated to s2 which gives you the result FooBar.

Hope that helped!
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mohammad,

Thanks for your reply.
My doubt is why we are getting s3 = "Foo" only.
I expected as "FooBar". I thought that might be due to precedence.
So i asked question in that way

thanks
rami
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rami marri,

in //1 the string s2 is concatenated only with string s1 which is in the second part (like this String s3 = b?s1 s1 + s2) so you are getting only "Foo"

but in //2 you are concatenating string s2 with the resulting string of (b?s1:s1) so you are getting "FooBar"
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rami marri:
Hi,
String s3 = b?s1 s1 + s2);



s1 is one expression and s1+s2 is taken as one expression
So you get s1 which is Foo.
 
Marshal
Posts: 80962
526
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look in the Java Tutorials (which you ought to bookmark by the way), you find the conditional operator (also called the ternary operator) ?: has the lowest-but-one precedence, so the concatenation takes place before the ?: In the first case however, the s1 alone is chosen. If you have b ? s1 : s1 you are wasting keystrokes.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the conditional operator is "syntactically right-associative" see section 15.25 :-
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html

just found this link as well : http://www.javabeginner.com/java-operators.htm

HTH
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it
Thanks to all

rami
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rimi

Well in case of ternary like in
//1
String s3 = b?s1:s1+s2;
Right hand part can be resolved as
if(b){
s1;
}
else{
s1+s2;
}
and the result of the if condition is assign to the s3 so we get //s3="Foo"

But in other case
//2

it will be resolved as
(
if(b){
s1;
}else{
s2;
}
) + s2;

the result of the if condition is resolved first and the result is concatenated with s2. and the final result is assign to the s4. That is why we get
//s4="FooBar"
Don't forget parenthesis has the highest precedence.

I Explain it in this form so that you have good understanding.
I hope you like it.
 
Anoop Singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Champ Me:
Hi Rimi

Well in case of ternary like in
//1
String s3 = b?s1:s1+s2;
Right hand part can be resolved as
if(b){
s1;
}
else{
s1+s2;
}
and the result of the if condition is assign to the s3 so we get //s3="Foo"

But in other case
//2

it will be resolved as
(
if(b){
s1;
}else{
s1;
}
) + s2;

the result of the if condition is resolved first and the result is concatenated with s2. and the final result is assign to the s4. That is why we get
//s4="FooBar"
Don't forget parenthesis has the highest precedence.

I Explain it in this form so that you have good understanding.
I hope you like it.

 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks champ me
thanks you very much for spending your valuable time

rami
 
Campbell Ritchie
Marshal
Posts: 80962
526
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree, thank you.
I never realised ?: associates to the right. Thank you Frank.

Those of you who are new to the Ranch: please use the code button below the message window; it makes quoted code much easier to read.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic