Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion in the behaviour of null

 
Sahul Yasin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wilder C Rodrigues
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sahul Yasin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas and Wilder ,
Thanks for the explanation .
Regards,
Yasin
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic