This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is null in Java?
How is null implemented in Java?
Suppose I say
String x = null;
How is this null stored internally?
Check out this question:
What is null in Java?
Basically, usually stored as 0's same as C++ but this can be implementation-specific and so you shouldn't rely on it.
As per java specification it is a sort of literal.
There is also a special null type, the type of the expression null,
which has no name. Because the null type has no name, it is impossible
to declare a variable of the null type or to cast to the null type.
The null reference is the only possible value of an expression of null
type. The null reference can always be cast to any reference type. In
practice, the programmer can ignore the null type and just pretend
that null is merely a special literal that can be of any reference
type.
In Java the runtime must ensure that all heap-allocated memory is zeroed out before the pointer to the block is exposed to the Java code. The all-zeroes block will be interpreted as the initial values of the instance fields. This pretty much guarantees that null will be implemented as a zero value in any JVM implementation.
The null value is stored in the variable x.
Related
This question already has answers here:
Null check in Java 8 Elvis operator?
(2 answers)
Closed 5 years ago.
I have a situation where I need get the property of a Java object if the object exists or null.
Something like foo == null ? null : foo.bar
Is there an operator available in java to do the same?
No, there is no propagate null operator in Java, cf. C# for example, which does have one. (It was a proposal at some point in Java's evolution, but has not sadly yet been incorporated into the language: foo = foo?.bar would be an obvious notation.)
You need to write this out longhand, as you have done.
This question already has answers here:
Interview : Java Equals
(7 answers)
Closed 6 years ago.
I have seen in many places that the constant value is used first and then the variable for example ("ram").equals(a) and in many places I have seen that the variable is used first and then the constant value with which they want to compare for example a.equals("ram").
what is the difference between a.equals("ram") and ("ram").equals(a) ?
which one is better to use and why ?
The first style is safer in situations when variable a is allowed to be null, because you can skip null checking.
When you write
if (a.equals("ram")) { ... }
you must ensure that a is not null to avoid a null pointer exception. This is not necessary for
if ("ram".equals(a)) { ... }
because equals() method is required to process null arguments without throwing an exception:
For any non-null reference value x, x.equals(null) should return false.
("ram").equals(a) is better. The code will never break even if a is null.
saves us from null pointer exception.
This question already has answers here:
No Exception while type casting with a null in java
(10 answers)
Closed 7 years ago.
When we are using System.out.println(null) then we get compilation error and to resolve this we typecast null like:
System.out.println((String)null)
and then it starts working fine.
My questions is how we are allowed to typecast null when we say that null is something which is not pointing to any location and thus has no value in itself.
Also if null is not pointing to any location then the object which is null will get Garbage collected then why we are using it at first place? I know it is litle bit out of the topic but it came to my mind.
I have seen few answers in below link but they mostly talk about println method functionality and implementation but I want to know about null only.
No Exception while type casting with a null in java
In Java, "null" is a special literal of the null type. It can be cast to any reference type, but not to any primitive type such as int or boolean. The null literal doesn't necessarily have value zero. And it is impossible to cast to the null type or declare a variable of this type.
That's why it does not throw error you can also do this null==null
This question already has answers here:
Java Object Null Check for method
(8 answers)
Closed 7 years ago.
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?
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.
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.
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
}
}
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.
what happens in memory when I, say, do the following:
in C: char *c=NULL;
in java: MyClass mc=null;
what happens in memory, how is this null represented in memory in both of these languages? Thanks
In C you are basically setting the pointer to zero value. In fact
char *c = NULL;
is equivalent to
char *c = 0;
Zero pointer cannot be dereferenced since there is no memory mapping for this address. On platforms with virtual memory, an attempt to do so triggers a page fault transferring control to the operating system which then usually handles the situation by killing the offending process (in UNIX your process receives Segmentation Violation signal SIGSEGV).
In Java, all non-primitive type variables are references. null in Java is a literal denoting the only value of a special unnamed type. This type can be cast to any reference type allowing you to put null into any reference variable. An attempt to use such reference to access an object will throw an unchecked exception called NullPointerException. See JLS 3.10.7 and JLS 4.1 for details.
What you do here is assign a char pointer to NULL.
It has basically the same effect, with the huge difference that dereferencing NULL in C will make your program die with a SIGSEGV (well, under Unix-like OSes, including Linux and Mac OS X), while Java will throw a NullPointerException.
Note that mc is really a reference, not a class by itself. It is nearly the same as a C pointer.
NULL has some practical value in both cases anyway: you can test if (mc == null) in Java. In C, that would be a simple if (!p) (but if (p == NULL) also works).
You can find your answer in the following posts:
What exactly is null in Java memory
Java - Does null variable require space in memory
So basically, the null itself does not take space but the object being put equal to null does take space
NULL is a special value indicating there's no data at that location, it's specific representation can vary. in C it's just 0, i'm note sure about java.