Why Java does not allow null while declaring primitive data types [duplicate]
Asked Answered
A

7

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.

Accrete answered 22/10, 2013 at 7:26 Comment(5)
Because primitive types cannot be null.Profundity
because an integer value is empty will alwez be 0 and never null. (in the case of an int)Carbone
@Profundity :) thats my question why? as java does wrapping then why does not java wrap primitive data types and allow null valuesAccrete
primitive data types are stored on stack whereas objects are allocated on heap, thus an object can have a null reference.Indemnify
@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.Accrete
L
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.

Lashawnda answered 22/10, 2013 at 7:30 Comment(0)
H
31

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

Hemelytron answered 22/10, 2013 at 7:30 Comment(3)
Can I do Byte b = null; byte b1 = b; ? byte MinValue ?Antineutrino
Sure you can do it, but you'll get a null pointer when b is unboxed.Hemelytron
Safely way to convert Byte/Integer to byte/int ?Antineutrino
S
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.

Supernova answered 22/10, 2013 at 7:31 Comment(0)
K
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.

Kokoschka answered 22/10, 2013 at 7:42 Comment(0)
M
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.

Meaningless answered 22/10, 2013 at 10:18 Comment(2)
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.Accrete
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 bytesMeaningless
A
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.

Autoclave answered 22/10, 2013 at 7:32 Comment(0)
L
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.

Loosejointed answered 22/10, 2013 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.