20

This is in continuation to my previous question and accroding to answers of this question Declaration of wrapper classes

Java wraps primitive data type to wrapper classes then why

char c = null; // invalid
int i = null; // invalid

is not allowed but

Character cObj = null; // valid
Integer iObj = null; // valid

is allowed.

Community
  • 1
  • 1
Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • 3
    Because primitive types *cannot* be ``null``. – qqilihq Oct 22 '13 at 07:29
  • because an integer value is empty will alwez be 0 and never null. (in the case of an int) – Rat-a-tat-a-tat Ratatouille Oct 22 '13 at 07:31
  • 1
    @qqilihq :) thats my question why? as java does wrapping then why does not java wrap primitive data types and allow `null` values – Vishrant Oct 22 '13 at 07:31
  • 1
    primitive data types are stored on stack whereas objects are allocated on heap, thus an object can have a null reference. – Vineet Kasat Oct 22 '13 at 07:38
  • @VineetKasat thanks vineet for your reply, a question is raised by your reply why I can not have null value for primitive data types that are stored in stack, I can even assign null in values of stack, I was reading one post that for array whose size is less then 64 is stored in stack memory. That means I can also assign null values to data types stored in stack memory. – Vishrant Oct 22 '13 at 11:59

7 Answers7

39

Because primitives represent value and Object variables represent references (something like pointers) to complex data objects. There is no null value general, it is a special keyword that "references to nothing" or empty reference - this is highly unprofessional answer, but I guess it will be most appropriate.

Besides, what could be in your opinion, numerical value of null? 0? -1? But, those are valid integers so what else?

I strongly suggest that you start familiarizing yourself with the following complex java tutorial. Every aspect that you have been asking about is explained there and supported with examples.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
31

null means "lack of an object". References can lack an object, primitives can't.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Kayaman
  • 72,141
  • 5
  • 83
  • 121
3

Java primitive type variables are store-by-value instead of store-by-reference variables. Where as the wrapper classes are objects basically like any other Java object except that all they do is wrap a primitive type.

2

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

As per jls-4.3.1, it is pointless to take about the null reference without the existence of an object.

Terry Li
  • 16,870
  • 30
  • 89
  • 134
2

Along with all above answer i would like to add this point too.

For primitive types,we have fixed memory size i.e for int we have 4 bytes and char we have 2 bytes. And null is used only for objects because there memory size is not fixed.

So by default we have,

   int a=0;

and not

   int a=null;

Same with other primitive types and hence null is only used for objects and not for primitive types.

Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
  • 1
    I don't think this is the right answer, 1st of all in Java char is not of 1 byte it is of 2 bytes for some specific reason, please search google for that, and I never heared about 32bit and 64bit primitive type in Java, as this is the first condition for java to be OS independent. – Vishrant Oct 22 '13 at 10:58
  • Thanks bro, just explained the answer and didnt thought of the character memory space and in my mind was String with one character and so it come to 1 byte. But its 2 bytes – Shoaib Chikate Oct 22 '13 at 11:51
1

Objects like Character and Integer are pointers: the actual number stored in the bytes that are that variable's value represents an address in memory for the rest of the JVM's memory. So, it is possible and meaningful to set that number to an address that goes nowhere, which is what null is.

A primitive like int or char, however, has a number that is interpreted as a number (integer, or ASCII code), and there's no way to make it "not a number" because all that the memory can possibly store is numbers.

DVA
  • 331
  • 2
  • 13
1

Referring to unboxing/autoboxing, you have to imagine them like are two ways that the compiler adopt to save you from going mad with continuos "casting" from primitive to object and vice-versa, but they are not flawless.

What happens if your Integer wrapper is null and you do a division? Not a division by 0 but a Null pointer exception, because java cannot unbox a not referenced object!

So it's safe and logical to keep different init rules for primitives and objects.

F2K
  • 481
  • 4
  • 11