In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what is the relationship between the basic types and packaging types in Java. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The basic data types and packaging types in Java are:
Basic type wrapper type booleanBooleancharCharacterintIntegerbyteByteshortShortlongLongfloatFloatdoubleDouble
1. Why do these two types exist?
We all know that in the Java language, new has an object stored in the heap, and we use those objects through references in the stack; but for a range of types that are often used, such as int, it is not very effective if we store them in the heap with new-especially simple, small variables. So there are basic types, and like C++, Java takes a similar approach, for which instead of creating the new keyword, it stores the value of the variable directly on the stack, making it more efficient.
2. Why should there be a packaging type when there is a basic type?
We know that Java is an object-oriented programming language, and the basic type does not have the nature of an object. In order to make the basic type have the characteristics of an object, there is a wrapper type (for example, when we use a collection type, we must use a wrapper type instead of a basic type). It is equivalent to "wrapping" the basic type, making it have the properties of an object, and adding properties and methods to it. Enrich the basic types of operations and improve the efficiency of the code.
When you need to put something into a collection such as ArrayList,HashMap, a basic type like int,double cannot be put in it, because containers contain object, so you need these basic types of wrapper classes.
3. The two are transformed into each other:
Int to Integer
Int a = 0
Integer A1 = new Integer (I)
Integer to int
Integer A1 = new Integer (0)
Int a = a1.intValue ()
4. The difference between the two:
1. The declaration is different:
The basic type does not use the new keyword, while the wrapper type requires the new keyword to allocate storage space in the heap
two。 Different storage methods and locations:
The basic type stores the variable value directly in the stack, while the wrapper type puts the object in the heap and then uses it by reference.
3. The initial values are different:
The initial value of the basic type such as int is 0, while the initial value of the wrapper type (Integer) is null
4. Use it in different ways:
The basic type assignment can be used directly, while the wrapper type is used in collections such as List and Map.
So much for sharing the relationship between basic types and packaging types in Java. I hope the above content can be of some help and 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.