Why Can't Primitive Data Types Be "Null" in Java

Why can't primitive data types be null in Java?

A primitive type is just data. What we call objects, on the other hand, are just pointers to where the data is stored. For example:

Integer object = new Integer(3);
int number = 3;

In this case, object is just a pointer to an Integer object whose value happens to be 3. That is, at the memory position where the variable object is stored, all you have is a reference to where the data really is. The memory position where number is stored, on the other hand, contains the value 3 directly.

So, you could set the object to null, but that would just mean that the data of that object is in null (that is, not assigned). You cannot set an int to null, because the language would interpret that as being the value 0.

Hope that helps!

Why Java does not allow null while declaring primitive data types

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.

Why can't I compare null field to a primitive

Because obj.n == 1 is interpreted as primitive integer comparison, so obj.n will be auto-unboxed calling out.n.intValue(), which leads to the exception.

Formally, JLS 15.21 makes it clear that compiler knows your code is a numeric comparison, and then JLS 15.21.1 applies which states:

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).

Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).

Why do I get a Null value was assigned to a property of primitive type setter of error message when using HibernateCriteriaBuilder in Grails

According to this SO thread, the solution is to use the non-primitive wrapper types; e.g., Integer instead of int.

A null value cannot be assigned to a primitive type error (Spring/Hibernate)

The problem with your code is that tableNumber field in Booking.java is of type int but in your jsp you have the following.

<form:option value="" label="--- Select ---" />

This basically implies that the value from the select dropdown can be null in which case Spring has no way to convert a null to a primitive type hence it throws such an exception on page load itself. There are 2 possible solutions to this issue :

1> Change the value from value="" to using some number like value="-1" for the blank option and handle the -1 in your application logic accordingly.

<form:option value="-1" label="--- Select ---" />

2> Other option is to declare the tableNumber field to be of type Integer instead of int.

Prevent NullpointerException on primitives attributes for a Java class

Primitive types can't be null. So methods returning primitive types can't return null.



Related Topics



Leave a reply



Submit