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 concept of automatic packing and unpacking of Java

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

Share

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

This article mainly explains the concept of automatic packing and unpacking of Java. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the concept of automatic packing and unpacking of Java".

Java as an object-oriented language, some people think that all you see is objects. In fact, before Java SE 5, basic types didn't exist by default using objects. If you want to treat basic types as objects, you have to convert them by yourself. However, after Java SE 5, automatic boxing and unboxing functions are provided for basic data types, which makes it extremely convenient to convert primitive types to objects.

Let's sort out the basic data types of java here. Never mind, I found that my mastery is not so clear. Here I also learn a total of eight, namely byte byte type (1 byte), char character type (2 bytes), short short integer type (2 bytes), int integer type (4 bytes), long long integer type (8 bytes), float floating point type (4 bytes). Double double precision floating point type (eight bytes), Boolean type (one byte)

Before javase5, if you want to manipulate basic data types as objects, you need to use the corresponding objects to package them.

Although this is not necessary now, but there are also some areas to pay attention to, I found out to bask in.

Let's talk about classes and objects first, and establish a concept first.

The class-- can be thought of as the design drawing object of the object-- is simply understood as a specific tool implemented according to the design diagram. If you really want to pull it apart, it is a professional course in software engineering. It would be nice if we had a concept.

The reason for packaging basic type data into objects is simple because objects can carry more data.

Examples of manual and automatic packing and unpacking

The class of Long,Integer,Double,Float,Boolean and so on is the so-called wrapper class, just like the meaning of the word wrapper, which is to provide a "wrap, shell", put the basic data types in it, take a look at the code, experience first.

Public class WrapperDemo {

Public staticvoid main (String [] args) {

Int data1=21

Int data2=24

/ / package as an object

Integer data1Wrapper = new Integer (data1)

Integer data2Wrapper = new Integer (data2)

/ / the raw data is directly divided by 3

System.out.println (data1/3)

/ / package the data and convert it to flexible, divided by 3

System.out.println (data1Wrapper.doubleValue () / 3)

/ / compare

System.out.println (data1Wrapper.compareTo (data2Wrapper))

}

}

Figure 1-1 running result of WrapperDemo

Through the above code and running results, you can see the benefits of packaging basic data types into objects, don't worry, this is just the practice before javase5, automatic boxing and unpacking has been supported after javase5, here, no longer write code alone, just write a few statements that can explain the problem, I believe it is easy to understand.

Before javase5, manual packaging Integer data1 = new Integer (10); after converting basic type data to object javase5, automatic packaging Integer data1 = 10 is supported You can convert basic type data into object eg: in the above code, you can use data1.compareTo (data2) to compare two basic data types directly; the basic data type itself has no method, and when you run it, you will find that it can still run correctly, which brings the benefits of automatic boxing and unboxing.

Let's talk about automatic packing and unpacking.

Automatic packing and unpacking, in essence, the compiler helped us, this is the so-called "compiler honey", since it is so good, can you rest assured to use it?

Don't worry, have a drink of water, and then we'll keep talking.

Take a look at the following situation, do you understand

Integer I = null;// indicates that I is not referenced to any object int j = I / / the code equivalent to int j = i.intValue () can be passed at compile time because its syntax is correct, but at run time, NullPointerException errors will be excluded, this is because I do not refer to any object caused by personal advice, if you do not understand the packing and unpacking very clearly, * * still pack and unpack it step by step, so that sometimes It may save you a lot of time for error correction.

Or automatic packing and unpacking.

Public class BoxDemo {public static void main (String [] args) {Integer data1= 500; Integer data2 = 500; System.out.println (data1==data2);}}

Before you see the result of the next run, guess what the result of the run will be. True or false?

Figure 1-2 results of BoxDemo operation

False? Yes, it is false. When boxed automatically, numbers with values between-128and 127will be placed in memory for reuse after being boxed.

If you go beyond the range of this value, it will not be reused, so every time the new comes out is a new object, and the result will naturally be false.

Here, the difference between the concept "=" and equals () "= =" is to compare whether two objects are referenced from the same object. "equals ()" compares the contents of two objects. This must be made clear, otherwise, when you encounter problems, it will really drive people crazy. At this point, I believe you have a deeper understanding of "the concept of automatic packing and unpacking of Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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