This week's book giveaway is in the Android forum.We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

Operator question

cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
Hi All
I just need some explanation about the following code.
boolean b = true;
String s1 = (b=!b)?(b=!b)?"Hello":"hello" b=!b)?"world":"World";
It prints world.
How does it works.
Thanks
Corn
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
It's right associative so:
String s1 = (b=!b)?(b=!b)?"Hello":"hello" b=!b)?"world":"World";
is broken down to:
String s1 = (b=!b)?<(b=!b)?"Hello":"hello">:<(b=!b)?"world":"World";
>
condition b=!b)
expr1: (b=!b)?"Hello":"hello"
expr2: (b=!b)?"world":"World"
And the execution steps are as follows:
1.Main condition: (b=!b) ~ false -> proceed to expr2
2.expr2 condition: (b=!b) ~ true -> return "world"
Hope this helps.
[ June 21, 2003: Message edited by: Alton Hernandez ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
2.expr2 condition: (b=!b) ~ true -> return "world"

Why is (b=!b) being evaluated to true?
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Originally posted by Anupam Sinha:

Why is (b=!b) being evaluated to true?

Actually b=!b is not comparison operation.It is an assignment operation .b=!b ,!b is false ,so b is assigned false.
Veena

SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I would also like to try to explain it.
For the purpose of adding parentheses correctly, let�s temporarily replace (b=!b) with the symbol x and any string literal with the symbol s.
x ? x ? s : s : x ? s : s
The operator ?: groups right to left. Starting on the right, add parentheses.
x ? x ? s : s : (x ? s : s)
x ? (x ? s : s) : (x ? s : s)
Now that we see the structure, replace the symbols.
(b=!b) ? ( (b=!b) ? �Hello� : �hello� ) : ( (b=!b) ? �world� : �World� )
Start on the left and evaluate the condition b = !b.
Since b is originally true, !b is false. Assign false to b. (b=!b) is false.
Skip the first expression ( (b=!b) ? �Hello� : �hello� ).
Evaluate the second expression ( (b=!b) ? �world� : �World� ).
Since b is false, !b is true. Assign true to b. (b=!b) is true.
The result is �world�.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Thanks a lot Veena and Marlene. Well I thought that the placing of the = and ! is not as it should be(should be means the way I use it) but then didn't thought about that. Your posts were very hepful and made me realize whats actually happening. Thanks a lot. One more thing I am curently thinking of trying a IDE for java. Any suggestions. Forte is way beyond my current system specifications. I am downloading JBuilder personal from Borland and I have downloaded BuleJ, RealJ and JCreator. In case you use have an idea about IDE's please suggest.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
anupam,
For the exam purpose ,Textpad editor is good.Simple & best.I use the same.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi Veena
Thanks for the advice, but firstly I have been using notepad and Sun's JDK 1.4. I would also like to try an IDE. Secondly I have already given the exam.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
jbuilder personal is great, and free.
if you are willing to spend money, intellij IDEA. the best IDE I've ever used...

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
BTW anupam, it's the best java IDE
[ June 21, 2003: Message edited by: Andres Gonzalez ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi Andres
Thanks for the much needed information.

I agree. Here's the link: http://aspose.com/file-tools

subject: Operator question