Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

boolean question

Thread view: 
Irlan agous - 16 Dec 2004 12:57 GMT
Hello, i dont unerstand why this statement can work

boolean b = true ? false ? true : false : true;

it gives me a false answer. How does this work? i would expect an error
messag? i cant get an answer in the java tutorial
Ryan Stewart - 16 Dec 2004 13:08 GMT
> Hello, i dont unerstand why this statement can work
>
> boolean b = true ? false ? true : false : true;
>
> it gives me a false answer. How does this work? i would expect an error
> messag? i cant get an answer in the java tutorial
Why would you expect an error message? This is analogous to writing:
boolean b;
if (true) {
   if (false) {
       b = true;
   } else {
       b = false;
   }
} else {
   b = true;
}

Obviously b will be false.
Irlan agous - 16 Dec 2004 13:18 GMT
Thanks, but how can u be sure it will be false, if i even dont know the
condition?

>> Hello, i dont unerstand why this statement can work
>>
[quoted text clipped - 15 lines]
>
> Obviously b will be false.
VisionSet - 16 Dec 2004 13:33 GMT
> Thanks, but how can u be sure it will be false, if i even dont know the
> condition?

boolean b = true

both assigns true to b and the whole statement evaluates to true

Signature

Mike W

Irlan agous - 16 Dec 2004 13:46 GMT
im sorry i guess its just me, but whatever i do the value stays false, i
dont get it

i have for example this

 boolean c = true ? true ? true : true : true;
 boolean c = false ? true ? true : true : true;

everything stays false

>> Thanks, but how can u be sure it will be false, if i even dont know the
>> condition?
>
> boolean b = true
>
> both assigns true to b and the whole statement evaluates to true
Steve Bosman - 16 Dec 2004 15:51 GMT
> im sorry i guess its just me, but whatever i do the value stays false, i
> dont get it
[quoted text clipped - 3 lines]
>   boolean c = true ? true ? true : true : true;
>   boolean c = false ? true ? true : true : true;

Post a genuine compilable example using these lines (for instance a
complete simple class) as quite frankly I don't believe that these two
lines are returning false. They are guaranteed to always give a result
of true.

Steve
Grant Wagner - 16 Dec 2004 16:06 GMT
> im sorry i guess its just me, but whatever i do the value stays false, i
> dont get it
[quoted text clipped - 5 lines]
>
> everything stays false

Then you are doing something very, very wrong.

public class BooleanTest {
   public static void main(String[] s) {
       boolean c = true ? true ? true : true : true;
       System.out.println(c);
   }
}

"true"

public class BooleanTest {
   public static void main(String[] s) {
       boolean c = false ? true ? true : true : true;
       System.out.println(c);
   }
}

"true"

As has been explained to you, the above are just shortcuts for:

boolean c;
if (true) {
   if (true) {
       c = true;
   } else {
       c = true;
   }
} else {
   c = true;
}

and

boolean c;
if (false) {
   if (true) {
       c = true;
   } else {
       c = true;
   }
} else {
   c = true;
}

In both cases, obviously -c- will be "true".

Instead of using examples that demonstrate what you -believe- the
results of your comparisons will be, why don't you show a SSCCE <url:
http://www.physci.org/codes/sscce.jsp />.

Signature

Grant Wagner <gwagner@agricoreunited.com

Boudewijn Dijkstra - 16 Dec 2004 13:46 GMT
> Hello, i dont unerstand why this statement can work
>
> boolean b = true ? false ? true : false : true;
>
> it gives me a false answer. How does this work? i would expect an error
> messag? i cant get an answer in the java tutorial

It's all about operator precedence:
 boolean b = true ? (false ? true : false) : true;

The expression:
 false ? true : false
evaluates to
 false

The simplified expression:
 true ? (false) : true
evaluates to
 false
Bjorn Abelli - 16 Dec 2004 13:54 GMT
"Irlan agous" wrote...

> Hello, i dont unerstand why this statement can work
>
[quoted text clipped - 3 lines]
> messag? i cant get an answer
> in the java tutorial

With some logic applied, the answer actually *is* in teh tutorial...

The conditional operator works in this way:

  condition : value-if-true : value-if-false

I guess you follow me so far?

Anyway, anything that can give a value can be put in the places for
value-if-true or value-if-false, even another conditional operator!

If I put a couple of parenthesis around it, maybe you can see what is going
on. The parenthesis isn't necessary, but can add clarity:

  boolean b = true ? (false ? true : false) : true;

So what does your example more precisely do?

- The condition for the "inner" one, is "false", which
 means that the value of *that* statement is false, which
 means that after that statement is performed, the
 conditional operator statement now is:

 boolean b = true ? (false) : true;

As the *condition* is true, the first value is selected, which makes the
value of the whole statement "false".

// Bjorn A
Thomas Schodt - 16 Dec 2004 13:57 GMT
> Hello, i dont understand why this statement can work
>
>  boolean b = true ? false ? true : false : true;
>
> it gives me a false answer. How does this work? i would expect an error
> message? i cant get an answer in the java tutorial

You don't understand the ternary operator?

   condition ? value1 : value2

if condition evaluates to true
the expression evaluates to value1
otherwise it evaluates to value2.

Take
  boolean b = true ? false ? true : false : true;
reformat for readability
  boolean b = true ? ( false ? true : false ) : true;
evaluate the inner ternary expression
  ( false ? true : false )
gives
  false
and subsequently
  boolean b = true ? ( false ) : true;
evaluates to
  boolean b = false;
Thomas G. Marshall - 16 Dec 2004 18:46 GMT
Thomas Schodt coughed up:
>> Hello, i dont understand why this statement can work
>>
[quoted text clipped - 23 lines]
> evaluates to
>    boolean b = false;

Criminey.  All we need is a few more people to post the same exact
explanation.

Folks, in a ng that is more tuned to beginners, the answers are likely to be
short.  Please consider reading through the existing ones before adding to
the collection.  There is no need to sound off with a simple answer just
because you can.

With less simple questions, the rules should be reversed: the reason is that
with complicated and lengthy answers there is often a nuance or detail left
out that deserves a rephrasing by another.

Signature

Whyowhydidn'tsunmakejavarequireanuppercaselettertostartclassnames....

Ryan Stewart - 16 Dec 2004 23:00 GMT
> Criminey.  All we need is a few more people to post the same exact
> explanation.

lol


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.