Home  >  Article  >  Java  >  What are the similarities and differences between wrapper types and basic data types in Java?

What are the similarities and differences between wrapper types and basic data types in Java?

王林
王林forward
2020-08-06 17:17:032654browse

What are the similarities and differences between wrapper types and basic data types in Java?

Similarities and differences between basic types and packaging types:

(Recommended tutorial: java introductory tutorial)

1. In Java, everything is an object, but the eight basic types are not objects.

2. Different declaration methods, basic types do not need to be created through the new keyword, while encapsulated types require the new keyword.

3. The storage method and location are different. The basic type directly stores the value of the variable and saves it on the stack for efficient access. The encapsulated type needs to point to the instance through a reference, and the specific instance is saved in the heap.

4. The initial value is different. The initial value of the encapsulated type is null, and the initial value of the basic type depends on the specific type. For example, the initial value of the int type is 0, and the boolean type is false;

(Video tutorial recommendation: java video tutorial)

5. Different usage methods. For example, only packaging types can be used when working with collection classes.

6. When should you use a wrapper class and when should you use a basic type? It depends on the basic business: whether this field allows null values, if null values ​​are allowed, you must use a wrapper class, otherwise the value Type is enough. If you use generics and reflection to call functions, you need to use a wrapper class!

The above is the detailed content of What are the similarities and differences between wrapper types and basic data types in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:What is a read-write lockNext article:What is a read-write lock