7

In java, you cannot state an array's size in its declaration

int[5] scores;  //bad

I'm told this is because the JVM does not allocate space in memory until an object is initialized. If you have an instance array variable (auto initialized with a default value of null), does that variable point to a place in the heap indicating null?

Adam
  • 791
  • 1
  • 12
  • 21

5 Answers5

6

A null reference is literally a zero-value. The operating system prevents any program from accessing the zero address in memory, so the JVM will proactively check and make sure that a reference value isn't zero before allowing you to access it. This lets the JVM give you a nice NullPointerException rather than a "The program has performed an Illegal Operation" crash.

So you could say that the variable "points to" an invalid heap location. Or you could just say the variable doesn't "point to" anything. At that point it's just a question of semantics.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
4

No, because in the JVM there's no need for that. If you're in a native language (C and C++, for instance), NULL is a pointer with a zero value, and that points to the memory base address. Obviously that's not a valid address, but you "can" dereference it anyway - especially in a system without protected memory, like old MS-DOS or small ones for embedded processors. Not that it would be a valid address - usually that location contains interrupt vectors and you shouldn't touch them. And of course in any modern OS that will raise a protection fault.

But in the JVM a reference to an object is more like a handle (i.e. an index in a table) and null is an 'impossible' value (an index that is outside the domain of the table), so it can't be dereferenced and doesn't occupy space in such table.

Fabio Ceconello
  • 15,819
  • 5
  • 38
  • 51
  • ok, so the value null is just held in the variable on the stack then? – Adam Nov 14 '11 at 21:13
  • Yes. The JVM will check when (and if) it is dereferenced, and raise an exception if it is null. – Fabio Ceconello Nov 14 '11 at 21:19
  • Also take a look at this related question, it has a very detailed answer: http://stackoverflow.com/questions/2707322/what-is-null-in-java – Fabio Ceconello Nov 14 '11 at 21:19
  • "Obviously that's not a valid address" - this is just incorrect. 0 is a perfectly valid memory address. There are many computing platforms where address 0 is valid. It's entirely up to the hardware and software (OS) as to whether 0 is valid. – Steve Kuo Nov 15 '11 at 00:19
  • 1
    I understand, but I was talking in the context of the question asked, in which we are dealing with pointers to objects (class instances). A C++ class instance can't reside at 0, because the whole purpose of the null pointer is to be an "impossible" value for a valid pointer and thus signal it as invalid/empty/uninitialized/etc. – Fabio Ceconello Nov 15 '11 at 23:11
  • There is nothing preventing putting a C++ object at location 0. There is nothing special about 0, other than the fact that many operating systems don't allow it. This is why (especially in embedded systems), it's a bad idea to assume that null is 0 when programming in C or C++. – Steve Kuo Nov 16 '11 at 16:53
  • In fact, in C++ null is 0, as Stroustrup himself stated. http://www2.research.att.com/~bs/bs_faq2.html#null – Fabio Ceconello Nov 16 '11 at 21:41
2

I think this post will answer your question - Java - Does null variable require space in memory

In Java, null is just a value that a reference (which is basically a restricted pointer) can have. It means that the reference refers to nothing. In this case you still consume the space for the reference. This is 4 bytes on 32-bit systems or 8 bytes on 64-bit systems. However, you're not consuming any space for the class that the reference points to until you actually allocate an instance of that class to point the reference at.

Edit: As far as the String, a String in Java takes 16 bits (2 bytes) for each character, plus a small amount of book-keeping overhead, which is probably undocumented and implementation specific.

(remember to upvote the answer in the link if it helps you out)

Community
  • 1
  • 1
Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
1

Nope...you'll have a null (0x00) reference in the object's variable.

stevevls
  • 10,675
  • 1
  • 45
  • 50
0

I would argue that "int[5] scores; //bad" is not due to memory allocation. Notice that when you declare something you are really declaring Type ReferenceName = new Type() typically.

Observe the two examples int[] scores = new int[5]; JLabel label = new JLabel();

The types (on the left hand side) are int[] and JLabel, which have nothing to do with memory allocation (except for a pointer), while the new instances (on the right side, requiring memory allocation) are int[5], requiring space for 5 ints, and JLabel(), requiring no arguments to call the constructor, but memory enough for a JLabel.

plainOldNerd
  • 305
  • 2
  • 8