permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion in the behaviour of null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion in the behaviour of null" Watch "Confusion in the behaviour of null" New topic
Author

Confusion in the behaviour of null

Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
Hi guys,
The following code prints false. Actually i thought,it might throw null pointer exception.

Boolean b =Boolean.valueOf(null);
System.out.println(b.booleanValue());

And also if(null instanceof Object) //extra code here
returns false .
Can any one of you explain whats happening internally?.And is there any other similar kind places ,where the null usage does not throw any exceptions ?
Regards,
Yasin
[ January 08, 2004: Message edited by: Yasin ]
[ January 08, 2004: Message edited by: Yasin ]

Regards,<br />Yasin<br />SCJP,SCWCD,SCBCD
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
The method valueOf() will create a valid Boolean object.
As you might have noticed the valueOf(String) method is been executed, this method returns a Boolean with the value true if the string argument is not null and is equal, ignoring case, to the string "true".
This means that the value of your Boolean b object is false.
The null type has one value, the null reference, represented by the literal null and this literal is of the null type.


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Wilder C Rodrigues
Ranch Hand

Joined: May 03, 2003
Posts: 107
Hi,
If the String value passed in the valueOf method is null or you simple pass null (reference to anything) or if you pass anything different of "true" with ignored case, the return will be false.
Example 1:

Boolean b =Boolean.valueOf("TRUE");
System.out.println(b.booleanValue()); //the out will be true
Boolean b =Boolean.valueOf("true");
System.out.println(b.booleanValue()); //the out will be true
Boolean b =Boolean.valueOf("");
System.out.println(b.booleanValue()); //the out will be false
Boolean b =Boolean.valueOf(null);
System.out.println(b.booleanValue()); //the out will be false

In the case below the null that you are comparing is reference to nothing. The null ins't object of any class.
And also if(null instanceof Object) //extra code here
returns false.
I hope have helped you.
Bests,
Wilder C. Rodrigues
SCPJ 2, SCDJ 2, SCWCD
Fortaleza/Brazil


SCEA Part I, SCAJ, SCPJ, SCDJ, SCWCD, SCBCD, SCMAD<br /> <br />"The significant problems we face can not be solved at the same level of thinking we were at when we created them." - Albert Einstein
Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
Hi Thomas and Wilder ,
Thanks for the explanation .
Regards,
Yasin
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"Yasin"-
Welcome to the JavaRanch! We like to keep a nice professional lookin' image... (we don't want anyone to show up the Moose). So, can you please adjust your displayed name to match the JavaRanch Naming Policy? Basically it should be a believeable and not obviously fictitious first and last name.
You can change it here.
Thanks! and again welcome to the JavaRanch!
Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
hi Jessica Sant,
Yes. Iam bit new to this Javaranch forums.Its pretty good . I never read this naming policy before. Ok ,now I have changed my display name .
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Hi
Your first stop in a case like this should be the API.
It should state what would happen in strange cases like this one.

[ January 14, 2004: Message edited by: Jim Crawford ]

<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion in the behaviour of null