Home > Java > Javagetting Started > 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?

王林
Release: 2020-08-06 17:17:43
forward
2745 people have browsed it

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template