File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ternary Operator Precedence while doing String concatenation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ternary Operator Precedence while doing String concatenation" Watch "Ternary Operator Precedence while doing String concatenation" New topic
Author

Ternary Operator Precedence while doing String concatenation

Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
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
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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!


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
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
gincy green
Greenhorn

Joined: May 26, 2008
Posts: 5
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"


Good Luck<br /> <br />with regards<br />Gincy Green<br /> <br />"Procrastination is the thief of Time"
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
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.
Karl Prenton
Ranch Hand

Joined: Mar 10, 2008
Posts: 51
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

Joined: Feb 20, 2007
Posts: 88
I got it
Thanks to all

rami
Anoop Singh
Greenhorn

Joined: May 27, 2008
Posts: 14
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
scjp 1.5, scwcd 5.0
Anoop Singh
Greenhorn

Joined: May 27, 2008
Posts: 14
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

Joined: Feb 20, 2007
Posts: 88
Thanks champ me
thanks you very much for spending your valuable time

rami
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ternary Operator Precedence while doing String concatenation