File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Regarding null in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding null in Java" Watch "Regarding null in Java" New topic
Author

Regarding null in Java

Divakara Sr
Greenhorn

Joined: Sep 06, 2011
Posts: 8

Hi Folks,

Can anyone tell me what exactly is null in JAVA ?

If i declare some string variable like

String str= null;

Whether the memory allocated for this variable str?
If allocated, how many bytes will be allocated ?
if not memory allocated, How JVM will come to know that there is some variable called str is declared ?

If allocated and if there is null value inside the memory, then what exactly the representation of null in binary ?


Thanks & Regards
Divakara
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3361
    
    9
When you declare String str = null; Java will allocate about 4 - 8 bytes of memory for the str variable. Here, it will store the memory address of the object you are pointing to. Null means the memory address will be 0. Java knows that if the memory address is 0, it points to nothing.
Divakara Sr
Greenhorn

Joined: Sep 06, 2011
Posts: 8

Hi,

correct me if i misunderstood

do you mean for all the variable which are assigned to null will be stored at 0th location ?
What if i declare some 5 string variables and assigned to null ? Whether all points to the same location ?

Basically as per my understanding memory will be allocated based on the type(datatype).

when above is true for eg : if one object is having many data members and its object is assigned to null, then how JVM will calculate the memory for the null objects ?


Thanks & Rergards
Divakara
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3361
    
    9
In Java, a variable holds either primitives (ints, booleans, chars, etc) or references. It's a mistake to think that a variable declared as String holds a String object. It doesn't. It holds the address to that String.

Let's make it easier to understand with a House class. Let's say we have a variable: House house = null;
The variable "house" holds the address to a House. In this case, there is no address. It's just like a blank piece of paper. However, once we do house = new House(); Java will construct a new House somewhere in memory. The address of the House is then written to the house variable.

Now, houses take up a lot of space. Pieces of paper take up space as well. How much space an object of type House takes up, is not known. It depends on the House class. But the "house" variable itself will probably take up 4 or 8 bytes. Even if they are empty and you don't write an address to them.

When you do something like

house1 = house2;

it simply means we take the address written on one piece of paper, and write it down on the other piece of paper. We don't actually do anything with a House.
Divakara Sr
Greenhorn

Joined: Sep 06, 2011
Posts: 8

Finally the memory will be allocated even if we assign variables to null. ok Thanks a lot for your reply. I may come back with some questoins regarding the same topic. thanks once again

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding null in Java
 
Similar Threads
Need info on memory allocation...
Pass by value or reference?
GC question..........
Undefined Variables
String Question