Primitive types in Java and original package types

Source: Internet
Author: User

Java offers two different types: reference types and primitive types (or built-in types). For example: int is the raw data type of Java, and integer is the wrapper class provided by Java for Int. Java provides encapsulation classes for each primitive type, and the common primitive and corresponding encapsulation classes are as follows:


Raw type Encapsulation class
Boolean Boolean
Char Character
BYTE byte
Short Short
int Integer
Long Long
float float
Double Double


The difference between a reference type and a primitive type:

1. The two are initialized differently

int i = 5;                       Original type integer j = new Integer (ten);     The object references  Java 1.5 to support automatic boxing   , so the Integer j = 10;

You do not need to call new with the original type, and you do not need to create an object. This saves time and space. Mixing primitive types and objects can also result in unexpected results associated with assignments.

2. The original type is the class, the reference type is the object

Original type size comparison with "= =", reference type size comparison with "equals"

3. The reference type can be serialized, not the original type.

4. The method provided by the reference type can be flexibly converted, can be extended, the original type does not

5. Only reference types can be used in the collection class and cannot use the original type

6. The original type has no null concept, the reference type has, in some cases need to tell if a parameter is initialized, if the original type is used, then the value of 0 does not know whether it is the initial value or not initialized the system automatically.

7. Sometimes it is necessary to use the encapsulation class, for example, you need to use
Request.setattribute (String key, object value); When this method, the second argument is the type of Object, and you want to put an integer, then you can only put an integer can not put int.

Summarize:

The behavior of primitive types and encapsulation types is completely different, and they have different semantics. Reference types and primitive types have different characteristics and usages, including: size and speed issues, which types of data structures are stored as the default values that are specified when reference types and primitive types are used as instance data for a class. The default value of an object reference instance variable is NULL, and the default value of the original type instance variable is related to their type.
int (primitive type) is generally done as a numeric parameter enough.
Integer (package type) is generally used when converting the type of the more

Primitive types in Java and original package types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.