How to check NULL in Java? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to check NULL in Java?

danielm77danielm77 USAPosts: 72Member

Can someone please let me know the standard way to check whether an object is NULL? I'm new to Java. Really appreciate your help.

Tagged:

Comments

  • sunlotussunlotus Posts: 55Member

    Try

    if(object_name.equals(null))
    {
    do something
    }
  • webhostingwebhosting Posts: 2Member

    [code]
    if(obj == null)
    [/code]

  • GouthamKumarGouthamKumar NellorePosts: 17Member

    we can use both == operator or equals operator to check whether an object is null or not.
    Object obj ;
    Here we have created a reference for Object class. So the value will be null.
    if(obj == null)
    System.out.println("obj is not assigned");
    else
    System.out.println("obj is assigned. Carry on");

    0r

    if(obj.equals(null))
    System.out.println("obj is not assigned");
    else
    System.out.println("obj is assigned. Carry on");

Sign In or Register to comment.