Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The difference between basic class and wrapper class in Java and how to convert them

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is a detailed introduction to "the difference between basic classes and wrapper classes in Java and how to convert between them". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "the difference between basic classes and wrapper classes in Java and how to convert between them" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.

I. Introduction

When you need to put things into ArrayList and HashMap, basic types like int and double cannot be put in, because containers are filled with objects, which requires these basic types of wrapper classes.

Basic data types are stored in the stack for greater efficiency

Each primitive type has a corresponding wrapper class in the java.lang package, new objects that exist in the heap (used by references in the stack), are an object, and therefore have more methods such as "transformations"

Examples:

1.int to Integer

int i = 0;Integer ii = new Integer(i);

2.Integer to int

Integer ii = new Integer(0);int i = ii. integer Value ();

Declaration: primitive types do not use the new keyword, while wrapper types need to use the new keyword to allocate storage space in the heap;

Storage method and location: The basic type is to store the variable value directly in the stack, while the wrapper type is to put the object in the heap and then use it by reference;

Integer a = null; int b = a;//throw NullPointException This usage compiles OK, but throws a null pointer exception, int b = a is actually int b = a.intValue(), because the reference value of a is null, calling the method on an empty object throws a NullPointException

The initial values are different: int is 0, boolean is false for the primitive type, and null for the wrapper type;

Usage: The basic type is directly assigned and used directly, while the wrapper type is used in collections such as Collection and Map.

III. Automatic packing and unpacking

Boxing: automatically converts base data types to wrapper types;

Unboxing: automatically converts wrapper types to primitive data types.

Before Java SE5, to generate an Integer object with a value of 10, you had to do this:

Integer i = new Integer(10);

And since Java SE5 has provided automatic boxing and automatic unpacking

int i = 10; //box int n = i; //unpacking read here, this article "Java basic class and wrapper class difference and how to convert between the two" article has been introduced, want to master the knowledge of this article also need to practice to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report