What is the purpose of throwing null in Java?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BloomS
    New Member
    • Jan 2023
    • 8

    What is the purpose of throwing null in Java?

    What happens when I run this?
    Code:
    public class WhatTheShoot {
    
        public static void main(String args[]){
            try {
                throw null;
            } catch (Exception e){
                System.out.println(e instanceof NullPointerException);
                System.out.println(e instanceof FileNotFoundException);
            }
        }
    }
    The response is:

    Code:
    true  
    false
    I was surprised that this did not result in a compile-time error.
    Why can I throw null in Java, and why does it upcast it to a NullPointerExce ption?

    I'm not sure if this is considered an "upcast" since I'm passing a null value.
    I cannot think of any valid reasons to use a null value in an interview.Maybe you're hoping to get fired, but why would anyone purposely do something that would result in their dismissal? I went through this resource but didn't get it.
  • pritikumari
    Banned
    New Member
    • Jan 2023
    • 23

    #2
    In Java there is default value for every type, when you don’t initialize the instance variables of a class Java compiler initializes them on your be-half with these values. Null is the default value of the object type, you can also manually assign null to objects*in*a*me thod.Object obj = null;
    But, you cannot use an object with null value or (a null value instead of an object)*if*you* do*so

    EXAMPLE

    public class Demo {
    String name = "Krishna";
    int age = 25;
    public static void main(String args[]) {
    Demo obj = null;
    System.out.prin tln(obj.age);
    System.out.prin tln(obj.name);
    ***}
    }

    Comment

    • kanchansaini123
      New Member
      • Jan 2023
      • 2

      #3
      The java.lang.NullP ointerException is thrown in Java when you point to an object with a null value. Java programmers usually encounter this infamous pointer exception when they forget to initialize a variable because null is the default value for uninitialized reference variables.

      Common scenarios where a programmer might encounter a NullPointerExce ption include:
      Calling an uninitialized variable
      Accessing or modifying a data field or member with a null object
      Passing a null object as an argument to a method
      Invoking a method with a null object
      Synchronizing a null object
      Throwing a null object
      Treating a null object as a Java array

      Example:-
      public class Demo {
      String name = "Krishna";
      int age = 25;
      public static void main(String args[]) {
      Demo obj = null;
      System.out.prin tln(obj.age);
      System.out.prin tln(obj.name);
      }
      }

      Comment

      • Vanisha
        New Member
        • Jan 2023
        • 26

        #4
        Null Pointer Exception is thrown when program attempts to use an object reference that has the null value. Null is reserved keyword in java for literal values. It is much like the other keywords public, static or final.

        ​Example:
        public class Demo
        { public static void main(String args[])
        {
        throw null;
        }
        }

        Comment

        Working...