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

How JAVA wraps classes, unboxing and boxing automatically

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about how JAVA wraps classes, unboxed and boxed automatically. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What is the packaging category?

As we all know, JAVA is an object-oriented programming language, but is it really completely object-oriented? In fact, there is some knowledge in JAVA that is not object-oriented, and that is the basic data types that we often use!

Why do you need something like packaging? In fact, the reason is very simple. Sometimes we need to treat some basic data types as an object. For example, when we need to add some basic data types to list, we add them directly through list.add (100). At this time, we have already converted the basic data types into objects.

So what is the correspondence between the wrapper class and the basic data type? Look below, ↓.

How to use the wrapper class?

First of all, the wrapper class is located in the java.lang package, so what is the use of these wrapper classes?

Provide: ways to convert strings, basic type data, and objects to each other

Contains relevant attributes for each basic data type, such as maximum value, minimum value, etc.

In fact, the methods in each of the wrapper classes are similar, so I'll use the Integer class as an illustration here:

First, let's take a look at the source code of the Integer class:

You can see that Integer inherits from Number

Take a look at the explanation in the comments. In fact, the translation is to wrap a value into a class.

Well, we took a very simple and rough look at the source code of Integer, and then let's actually do it!

For example, the following line of code wraps the number "100" as an object

Integer I = new Integer

In fact, this code can also be written as follows:

Int t = 100th Integer I = new Integer (t)

In fact, it is to wrap an int basic data type into an object.

Next, let's look at a method called Integer.parseInt ().

Integer in = Integer.parseInt ("123456")

This method is to convert a String type into an int type, a very practical method!

Now that you can convert a String type to an int type, you can certainly convert an int type to a String type!

Int t = 100th string tstr = Integer.toString (t)

However, there is actually a very simple way to convert an int type to a String type.

String T1 = 234 + ""

Because of the space, I will only show some of the methods in Integer. I suggest you take a look at the source code of each class in the wrapper class. There are many methods that you can also implement one by one.

Highlight: automatic packing and unpacking (auto boxing & & boxing)

Do you still look confused and boxed when you see these two words for the first time? Unpacking? It's actually very simple and rude. So what exactly is automatic packing? Autoboxing means that the basic type is automatically encapsulated into the same type of packaging as it, for example:

Integer tt = 100

The above code is auto-boxing, which has been provided since Java SE5. In this process, the corresponding Integer object is automatically created according to the numerical value, which is called boxing.

In fact, there is a story behind this line of code, and the actual code is:

Integer tt = new Integer

After learning about automatic packing, what is automatic unpacking? As the name implies, corresponding to boxing, the wrapper class is automatically converted to the basic data type.

Integer tt = 100; / / packing int ttint = tt; / / unpacking

In fact, the compiler secretly did something for us:

New Integer (100). IntValue ()

It calls the intValue () method in the Integer object. Why do you say that? Let's take a look at the test code:

Integer tt = null;// defines an empty object int ttint = tt;System.out.println (ttint)

Running result:

Exception in thread "main" java.lang.NullPointerExceptionat demo1.main (demo1.java:9)

You can see that it was reported wrong. why? Because we define tt as null, that is, it is an empty object (that is, it does not exist), but we use int ttint = tt, because when we write this line of code, it calls the method in Integer, but we don't actually create the object, so we report a null pointer error.

Through some of the above code, you may have some understanding of packing and unpacking, in fact, from the bottom point of view, the valueOf (int) method of Integer is automatically called when packing. When unpacking, the intValue method of Integer is automatically called. All these methods can be found in the source code. Everyone can go and have a look!

Let's move on to the wrapper class and look at the following code:

Integer tt1= 1234 Integer tt2 = 1234 system. Out.println (tt1==tt2); System.out.println (tt1.equals (tt2))

Take a look at the output:

Falsetrue

Everything is normal, but there is no hurry! Let's move on to the following:

Integer tt1= 123; / / changed to 123X Integer tt2. Out.println (tt1==tt2); System.out.println (tt1.equals (tt2))

Output result:

Truetrue

Why is it true? Haven't I created two objects? Should it be false? In fact, the number between [- 128-127] is still treated as a basic data type. Why? In fact, because basic data types are faster than objects, that's what jdk developers write, and there's nothing we can do about it. Just remember this, everyone!

Thank you for reading! This is the end of the article on "how JAVA wraps classes, unpacking and packing automatically". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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

Internet Technology

Wechat

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

12
Report