• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code issue  RSS feed

 
Tyler Jordan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to run the following code in a program. What is wrong with the following. Is there something wrong with doing a conditional on null? Or trying to run a conditional against a null?

String puck = null;
if (puck.equals(null))
{
puck = "puck";
System.out.println("PUCK = " + puck);
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To test against null you want to use == and not .equals
 
Tyler Jordan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, is null considered a primative?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No null is not a primitive.

When you use == between references, it determines if those references refer to the same object.

In the case of null, == will determine if a reference does not point to an actual object.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of pointing to a memory address, a null reference is a special value (all zeros) indicating that no object is referenced.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
... a null reference is a special value (all zeros) ...

Ok, this is nitpicking, but: a null value is not necessarily represented by "all zeros". What special value the JVM uses for a null reference is up to the implementation of the JVM.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The syntax puck.equals(whatever) calls a method on an object. When puck is null there is no object so the JVM can't call the method and it gives the NPE.
 
Benjamin Lau
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Test in NetBeans IDE 5.0
Failed:

error message:java.lang.NullPointerException
Successed:
 
shan sundaram
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Otherwise you can also null check using "instanceOf" keyword
Ex:
if(!(puck instanceOf String)) {
.........
}

It will check both null and object reference..
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, it's instanceof with a small o, not a capital O, and second, although instanceof returns false when you put null on the right hand side, it doesn't check for equality if the right hand side is not null.

Don't use instanceof just for this purpose, that's not what it was meant for.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!