• 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

Difference btw null and false....

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all,

I want to know the difference between null and false value.


the below code..... even if the value is null or false only the first if block is executed... how to fix it???



Thanks..

Philip
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the value is null only the first block will be executed, if it is false the second block will be executed.........

Try printing the value of apple.getValue()
If it is false it has to execute the second block.

null is the null literal in java while false is a boolean literal.

Hope this helps.........
 
Philip Zac
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Sachin,

Suppose a getValue is either false or null, and the first if block is executed, would the "null" be also considered as Boolean equivalent of the false value.

It seems so when i run it....

Thanks ..
philip
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suppose a getValue is either false or null, and the first if block is executed



If and only if getValue is null the first block will be executed
If and only if getValue is false the second block will be executed

would the "null" be also considered as Boolean equivalent of the false value.



No, it won't be considered as Boolean equivalent.

First print the value of getValue and check what is the value before the if else.

Then put in the first block. Repeat this for the second block.

This should clear all your doubts.
 
Philip Zac
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyway Sachin,
Thanks alot..... I would try as you advised me.

Thanks again.
Philip
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Disagree with you both.

Either your getValue() method can return an object in which case null is possible and your first block will throw an Exception . . .
Or, it can return a boolean, in which case your second block might run.

I think the most likely result of that code is



. . .

A compiler error (except maybe if it has a Boolean return type). Try it and see.

**************************************************************************

The null reserved word means a "state of non-existence" (I think that's what the Java Language Specification calls it). It can only be applied to a reference type.
The false reserved word is a boolean literal (opposite true) meaning the same as the common English word of the same spelling. It can only be applied to a boolean primitive or (in Java5 or higher only) by boxing and unboxing to a Boolean wrapper object.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that if you are using java with SQL, there are some logical inconsistencies between null and false.

Some databases are unable to distinguish an SQL NULL string from empty string ('')

Bug 4032732

The wikipedia entry on Null(SQL), is perhaps beyond the scope of this post, but it might help you grasp the conceptual difference between null and false.

Null_(SQL)
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SQL null? . . . I never thought of that, so that might be another explanation.

But JDBC methods which test for SQL null are usually called "isNull(. . .)".

I still think that code won't compile if it's pure Java.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can ANY method (eg. getValue()) return two different data types ?
One a value that can only be assigned to a reference variable (null), the other a value that can only be assigned to a boolean (true or false).
Notice that all mentions of getValue() have the same signature.
The example is rubbish.
Do not attempt to understand the use of null in Java by its use in SQL.
The same word is used in entirely different ways and purposes :
Object o1 = null ; Object o2 = null ;
System.out.println (o1 == o2) ; // prints : true
In SQL a null value never compares equal to another null value.
In SQL a null value is an unknown value.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Graeme Byers:
How can ANY method (eg. getValue()) return two different data types ?
One a value that can only be assigned to a reference variable (null), the other a value that can only be assigned to a boolean (true or false).
Notice that all mentions of getValue() have the same signature.
The example is rubbish.


It can't have two different data types, but as Campbell said, if the return type is Boolean (the wrapper class) it can be compared to both null and false (through auto(un)boxing).
 
If you settle for what they are giving you, you deserve what you get. Fight for 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