English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of Java Basic Data Types and Encapsulation Types (Difference Between int and Integer)

int is provided by Java8One of the primitive data types.

Java provides a wrapper class for each primitive type, Integer is the wrapper class provided by Java for int (that is, Integer is a Java object, while int is just a primitive data type). The default value of int is 0, while the default value of Integer is null, so Integer can distinguish between unassigned and 0, while int cannot express the unassigned situation. For example, to express the difference between not taking an exam and a score of 0, you can only use Integer. In JSP development, the default value of Integer is null, so when displayed in a text box using EL expressions, the value is an empty string. While the default value of int is 0, when displayed in a text box using EL expressions, the result is 0. Therefore, int is not suitable as the type for form data in the web layer.

In Hibernate, if OID is defined as Integer type, Hibernate can determine whether an object is temporary based on whether its value is null. If OID is defined as int type, you need to set unsaved in the hbm mapping file.-The value property is 0.

Additionally, Integer provides multiple operations related to integers, such as converting a string to an integer, and Integer defines constants for the maximum and minimum values of integers.

int is a primitive type.

    Integer is a reference type...

    For example, int a = 5;
    Integer b = 5

For a, you can only use it for calculations, such as addition, subtraction, multiplication, and division.

You can use b for many things because it is an object, it has many methods, and you can use it like a String object. java.lang.Integer is a class. Operations on it must be done through class methods  

    int is the default in Java8It is one of the basic data types. It is not an object of a class.

  int is a primitive data type, and Integer is a class that wraps int.   

    Variables declared as int do not require instantiation, while variables declared as Integer do (because classes need to be instantiated). 

  int is a primitive type, and Integer is a wrapper class, which is a class.

  Integer is a more advanced data type than int, why does Java use int instead of Integer like VB?

  int是面向机器底层的数值类型,是Primitive类型的数据类型,而Integer是int的Warpper类,是面向对象的即OOP的对象类型。int   一般只用在数值计算中,而Integer是用在Java的其它要使用对象的地方,比如Map的Key与Value,List与Set的Element若要保存数值信息都要把int包装成Integer对象使用。

  int is a machine-level numerical type, which is a primitive data type, and Integer is the Wrapper class of int, which is an object type oriented to object-oriented programming (OOP). int is generally used only in numerical calculations, while Integer is used in other places in Java where objects need to be used, such as Map's Key and Value, List and Set's Element, if numerical information needs to be saved, int must be wrapped into an Integer object.   

    Java    provides two different types: reference types and primitive types (or built-in types). Int is a primitive data type in Java, and Integer is the encapsulation class provided by Java for int. Java provides an encapsulation class for each primitive type.  
    Primitive types          Encapsulation classes  
    boolean            Boolean  
    char               Character  
    byte               Byte  
    short              Short  
    int                Integer  
    long               Long  
    float              Float  

  double             Double

    Reference types and primitive types behave completely differently and have different semantics. Reference types and primitive types have different features and usages, including size and speed issues, which type of data structure this type stores, and the default values specified when reference types and primitive types are used as instance data of a class. The default value of object reference instance variables is null, while the default value of primitive type instance variables is related to their type.  

    int is generally sufficient for numerical parameters  

integer is generally used for type conversion