473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

What is the purpose of throwing null in Java?

8 Nibble
What happens when I run this?
Expand|Select|Wrap|Line Numbers
  1. public class WhatTheShoot {
  2.  
  3.     public static void main(String args[]){
  4.         try {
  5.             throw null;
  6.         } catch (Exception e){
  7.             System.out.println(e instanceof NullPointerException);
  8.             System.out.println(e instanceof FileNotFoundException);
  9.         }
  10.     }
  11. }
The response is:

Expand|Select|Wrap|Line Numbers
  1. true  
  2. 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 NullPointerException?

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.
Jan 11 '23 #1
3 8806
pritikumari
23 16bit
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*method.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.println(obj.age);
System.out.println(obj.name);
***}
}
Jan 28 '23 #2
kanchansaini123
2 2Bits
The java.lang.NullPointerException 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 NullPointerException 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.println(obj.age);
System.out.println(obj.name);
}
}
Jan 30 '23 #3
Vanisha
25 16bit
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;
}
}
Feb 1 '23 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Dave D | last post by:
What's the Big Java Development Environment These Days? I used to use VisualCafe years ago.... -- Dave
5
by: SOAP | last post by:
what is assertion in java? thank you ~ ªÑ²¼»ù®æ¦³¤É¦³¶^, ¶R½æ­n¯à©Ó¾á­·ÀI ~ ~ Samba, more than a low cost File and Printer server ~ -- Let us OpenSource --
1
by: garyusenet | last post by:
>From MSDN I'm trying to learn more about streamreader. I'm working my way down the MSDN definition. The first entry is the Public Constructor StreamReader. I'm fairly happy I have a basic grasp on...
14
by: cat_dog_ass | last post by:
....Microsoft ports the .NET framework to other operating systems? Will the only advantage that Java has over .NET be lost? I've been a Java programmer and will be shifting to .NET soon. Is this...
3
by: shivapadma | last post by:
1.when referenced pointer is not active then it is called dangling pointer. is this correct ? 2.the pointer which does not point to anything is called null pointer. is NULL macro is a...
0
by: johnstalin | last post by:
Shine Enterprise Java Pattern Shine Enterprise Java Pattern has been developed for variety of application. This pattern has these parts: • Maplet: a framework for doing web projects which are...
2
by: Kid Programmer | last post by:
Hey, I haven't posted on this forum in several years (I made this account a long time ago, hence the incredibly stupid username :/) and since then I've become a reasonable C++ programmer (reasonable...
2
gautamz07
by: gautamz07 | last post by:
What is collection in java ? Types of collections and thier uses ? and a code example
1
by: aartik | last post by:
what is parsing in java? explain with example
9
by: robert22 | last post by:
what is stream in java? can someone help?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.