• 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

The boolean Type and boolean Values

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This passage is from the JLS:


An integer x can be converted to a
boolean, following the C language convention that any nonzero value is true, by the expression x!=0. An object reference obj can be converted to a boolean, following the C language convention that any reference other than null is true, by the expression obj!=null.


What does this mean? I thought a Boolean could not be converted or casted to anything.
JLS-4.2.5
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like this for instance:
int x = 1;
int y = 0;
boolean b1 = (x!=0); // b1 is true
boolean b2 = (y!=0); // b2 is false
Object o1 = new Object();
Object o2 = null;
boolean b3 = (o1!=null); // b3 is true
boolean b4 = (o2!=null); // b4 is false
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct a boolean can not be cast to anything -- but notice they're not casting it --
basically its just an expression just like any other, the result of which is of the type boolean, try typing it into a lil class and run it. It's not really magic.
Take the first expression:
int x = 1; boolean y;
y = x != 0; //returns true
it simply assigns the result of the expression x!=0 to the value of y.
int x = 0; boolean y;
y = x != 0; //returns false
 
Terrence White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm.
Your example has two integers: x and y.
x is used in the boolean expression x!=0 .
y is used in the boolean expression y!=0 .
Both expressions return a boolean and
are assigned to boolean values.
At what point are the integers x and y converted to booleans?
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The integers are not converted to boolean values, but the comparison of the integer value with the value 0 yields a boolean which is assigned to a boolean primitive.
In this respect I must admit that the JLS terminology is erroneous since an integer cannot be converted to a boolean. But you get the idea, don't you?
[ July 10, 2002: Message edited by: Valentin Crettaz ]
 
Terrence White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took the words from the JLS passage literally:
"...An integer x can be converted to a boolean..."
My confusion was really about those words, not the code itself.
Thanks for your help.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right - a integer can not be transformed into a boolean in Java. The reason this is shown is because this was a trick often used in C++:

You see, in C and C++, an int could be used wherever boolean was expected. The value was interpreted as 0 = false and anything else = true.
All the JLS is talking about is how to build an expression that allows you to do something similar in Java in spite of the fact that you can't actually an integer where a boolean in expected. Rather, you can use a boolean expression involving an integer where a boolean is expected.
I hope that helps,
Corey
 
Terrence White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! That did clear it up for me.
This forum stuff is cool...
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic