Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: any difference between java.lang.Integer and int?

Tags:

hibernate

Is there any noticeable difference between

<property name="pwdRetryCount" type="java.lang.Integer">
    <column name="pwd_retry_count" />
</property>

and

<property name="pwdRetryCount" type="int">
    <column name="pwd_retry_count" />
</property>
like image 997
ohseekay Avatar asked Nov 14 '11 01:11

ohseekay


People also ask

Is int and Integer same in Java?

In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.

What's the difference between integer and int?

The major difference between an Integer and an int is that Integer is a wrapper class whereas int is a primitive data type. An int is a data type that stores 32-bit signed two's complement integer whereas an Integer is a class that wraps a primitive type int in an object.

Is integer less efficient than int Java?

Use int when possible, and use Integer when needed. Since int is a primitive, it will be faster. Modern JVMs know how to optimize Integer s using auto-boxing, but if you're writing performance critical code, int is the way to go.

Which is faster int or integer in Java?

Primitive type int needs 4 bytes in Java but, Integer object occupies 16 bytes of memory. Hence, int is comparatively faster than an Integer object. Since, int is a primitive type it is less flexible.


1 Answers

They only have noticeable difference when handling null value.

It is because int is the primitive data type that cannot assign null to it while java.lang.Integer is the wrapper class of int which can accept null .

So , if pwd_retry_count column is nullable and you use int to map your entity object , for the record which pwd_retry_count is null , error occurs as int cannot store null.

like image 138
Ken Chan Avatar answered Oct 05 '22 08:10

Ken Chan