-5

I was working on some project and got a condition when I have to check the object is null or not from a list and all variables of the object are null.

So can someone explain to me how an object is checked for null i.e. variable wise or some other way.

how an object is checked for null internally in java don't want the code. want the concept

Please in a little detail.

My Question: How Java internally checks if object contains a null value?

tushar garg
  • 69
  • 1
  • 6

4 Answers4

2

Apparently, you are actually asking how null checks are implemented under the hood.

The answer is implementation specific. It could be different for different JVMs and / or execution platforms. (If you want to research the specific implementation on a specific JVM, I suggest you checkout the JVM source code and/or get the JIT compiler to dump out the compiled native code for you to examine.)

Basically, there are two approaches:

  • An explicit x == null test will typically compile to an instruction sequence that compares the value of x against the value that represents a null. That is usually a 32-bit or 64-bit zero.

  • The implicit null check in x.toString() could be done the same way. Alternatively, it could be done by simply treating x as a machine address and attempting to fetch a value at that address. Assuming that the zero page has not been mapped, this will trigger a hardware "segmentation fault" exception. Java uses native code mechanisms to trap that exception, and turn it into a NullPointerException.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

If you're looking at a single item:

if(object == null)
{
   (...)
}

You mentioned a list. Let's pretend it's an ArrayList of Objects:

for(Object o : array_list)
{
   if(o == null)
   {
      (...)
   }
}

You'd also want to check to see if your list is null before you start looping through it.

Lee Presswood
  • 210
  • 2
  • 15
1

Basically any can be easily checked for null value. Every internal details and implementations of null and comparison with object are totally managed by java so all we need is to have a compare of the object with null as :-

Object obj = null; // Object can be replaced with any class 
if(obj == null){
// do your logics
} 

As far as any List or Collection is considered, to see if object stored in it are null or not :-

List <String> list = new ArrayList<String>();
list.add("hi");
list.add(null);
for(String s : list){
 if(s == null){
// do your logics here
   }
}
AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
0

Java does not check if an object is a "null".

You cannot have a null object as null does not extend the Object class.

What you can do in java is have a variable assigned to null, meaning it references "nothing". (In reality, it is referencing the bytes that null is defined as)

That is what the other answers are doing, they are checking if a reference variable is actually pointing to nothing (null). An object itself, however, is never null.

SwiftySwift
  • 477
  • 4
  • 13
  • This is not helpful. You are correct that the JLS says that `null` is not an object. However, in reality Java professionals are happy to use the phrase "check an object for null" because we all know what it means ... to us. – Stephen C Jul 24 '15 at 19:31
  • @StephenC This is true. However, I was reading through some of OP's comments and in a few of them he asked if an object is null if "all variables are null .... [and] how an object is checked for null internally" I interpreted this as wanting to know exact details, one of them being than object itself is not null, and that the variable is made to reference `null`, which is how we check if "an object is null" – SwiftySwift Jul 24 '15 at 19:40
  • I put that down to poor English skills ... – Stephen C Jul 24 '15 at 20:15
  • Fair enough. Guess we won't know what exactly OP wanted. – SwiftySwift Jul 24 '15 at 20:18
  • @Marcin you are correct i wanted to ask the same thing and very thanks but by the time you answered i found it. – tushar garg Apr 06 '16 at 07:18