• 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 question

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

2.expr2 condition: (b=!b) ~ true -> return "world"


Why is (b=!b) being evaluated to true?
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anupam,
For the exam purpose ,Textpad editor is good.Simple & best.I use the same.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW anupam, it's the best java IDE
[ June 21, 2003: Message edited by: Andres Gonzalez ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres
Thanks for the much needed information.
 
reply
    Bookmark Topic Watch Topic
  • New Topic