• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Code issue

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, is null considered a primative?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test in NetBeans IDE 5.0
Failed:

error message:java.lang.NullPointerException
Successed:
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic