In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use and operate the packaging class of Java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Packaging class
What is the packaging class?
Overview of basic type wrapper classes: the advantage of encapsulating basic data types into objects is that more functional methods can be defined in objects. The data wrapper class wraps the values of basic data types into objects in Java. The Java language provides wrapper classes for eight basic data types. First, take a look at the wrapper classes of eight data types:
The reason for the emergence of the wrapper type: because Java is an object-oriented language, the basic type does not have the nature of the object. In order to "integrate" with other objects, the wrapper type appears, which is equivalent to "wrapping" the basic type, which makes it have the nature of the object, adds properties and methods to it, and enriches the operation of the basic type.
Common operations: for conversion between basic data types and strings
Integer class: just talk about this class here, the other seven can check the official documentation, the usage is the same.
The construction method of Integer:
Integer (int value) / / constructs a newly assigned Integer object that represents the specified int value Integer (String s) / / constructs a newly assigned Integer object that represents the int value indicated by the String parameter.
Example:
Integer i1 = new Integer (123); Integer i2 = new Integer ("123"); System.out.println (i1); System.out.println (i2); / / the result output of both is 123, which is the use of the two construction methods.
The most important thing is the conversion between String and int types, which of course requires some methods of Integer: first, int- > String:
/ / there are two methods for int----String: int a = 100 int / method 1: directly concatenate String S1 = "" + a; / / convenient and concise method System.out.println (S1); / / method 2: the string representation of the int parameter returned by the valueOf (int I) method in the String wrapper class. String S2 = String.valueOf (a); / / pass in an integer. You need to receive System.out.println (S1) with String type.
String- > int:
/ / method 1: / / String---- > Integer- > int// first convert String to Integer wrapper class, and then use the method intValue () in Integer to return intString s = "100s"; Integer I = Integer.valueOf (s); i.intValue (); / / method intValue () in Integer returns intSystem.out.println (I) / / method 2: / / Integer method: parseInt (String s), using string parameters as signed decimal integers int y = Integer.parseInt (s); System.out.println (s)
Integer's equals method: Integer's equals method inherits from Object and is overridden to determine whether two Integer values are equal:
Integer i1 = new Integer (123); / / enter int type 123Integer i2 = new Integer ("123"); / / enter string type 123boolean b = i1.equals (i2); System.out.println (b)
/ / the number of true output here
/ / objects of type i1 and i2 of type Integer contain the same values; other cases return false
The other main methods of Integer, the red font is the name of the method: you can try it yourself.
Summary of basic types and packaging types:
Advantages of basic types: relatively simple data storage and high computational efficiency
Advantages of wrapper classes: some are easy, for example, the element of the collection must be an object type, which satisfies java's idea that everything is an object.
The declaration is different: the basic type does not apply the new keyword, while the wrapper type needs to use the new keyword to allocate storage space in the heap
Storage method and location are different: 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.
The initial value is different: the initial value of the basic type, such as int, is 0.Boolean is false, while the initial value of the wrapper type is null.
If you use it in different ways, you can directly assign values to basic types and use them directly.
In Java, there are many classes, such as Math,System,Date, etc., which can be found in the Java official documentation if necessary.
Automatic packing and unpacking:
Boxing: convert the basic data type to the corresponding packaging type
Unpacking: convert the packaging type to the corresponding basic data type
Box:
/ / this is a common Integer construct: Integer i1 = new Integer (100); / / and this is written as automatic boxing, in fact, this bottom layer also does new Integer (100) Integer i1 = 100 position / the effect is the same.
Unpacking:
/ / for example, to add 200Integer i1 = 100 position to i1 / because now i1 is a reference data type, it has to be converted to the basic data type i1 = i1.intValue () + 200; / / this is called manual unpacking System.out.println (i1); / / so the output is 300pm / automatic unpacking: nteger i1 = 100fii1packing 200 / / this is the automatic unpacking, / / in fact, the bottom layer of the automatic unpacking completes a unpacking and a packing / / first i2.intValue (), then i2 = i2+200System.out.println (i1)
But we will find a small problem:
/ / when I is null: Integer I = null;i+=100;System.out.println (I); / / this execution will report an error of NullPointerException / / so we need to add a judgment: Integer I = null;if (iTunes null) {iPremium 100;} System.out.println (I)
So we need to pay attention to:
As long as it is an object, a judgment that is not null must be made before the operation.
In fact, wrapper classes work better than basic types-- wrapper classes can do what basic types can do. But what wrapper classes can do, basic types may not do, such as assigning a null value.
Thank you for reading this article carefully. I hope the article "how to use and operate the packaging class of Java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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.
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.