Win a copy of Design for the Mind this week in the Design forum!

# Operator question

cornel jew
Ranch Hand
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
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
Posts: 1090
2.expr2 condition: (b=!b) ~ true -> return "world"

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

Veena Pointi
Ranch Hand
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

Marlene Miller
Ranch Hand
Posts: 1392
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
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
Posts: 442
anupam,
For the exam purpose ,Textpad editor is good.Simple & best.I use the same.

Anupam Sinha
Ranch Hand
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
Posts: 1561
jbuilder personal is great, and free.
if you are willing to spend money, intellij IDEA. the best IDE I've ever used...

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

Anupam Sinha
Ranch Hand
Posts: 1090
Hi Andres
Thanks for the much needed information.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?