Home Uncategorized Java what is unpacking and packing? Unpacking and packing are useful???

Java what is unpacking and packing? Unpacking and packing are useful???

by admin

packing and unpacking in Java
in Java, data types can be divided into two types: basic data type (value type) and class type (reference data type). The data of basic type is not an object, and its toString (), hashcode (), getClass (), equals () and other methods cannot be called as an object
Therefore, in Java, wrapper classes for each of the eight basic data types are provided. As follows:
index Basic data type Packaging
1 int Integer
2 char Character
3 Float Float
4 double Double
5 byte Byte
6 short Short
7 long Long
8 boolean Boolean
the so-called boxing is to wrap basic types with their corresponding reference types, so that they can have the characteristics of objects. For example, we can wrap int type into integer object, or double into double, and so on
The so-called unpacking is to simplify objects of reference type such as integer and double into data of value type in the opposite direction of boxing
//JDK1. 5. Before that, the packing and unpacking operations were carried out manually
public class
public static void main (String []agrs)
int i=10;
Integer j=new Integer(i); //Manual packing operation
int I1 = j.intvalue()//Manual unpacking
System. out. println(i1*i1);
}; //output:100

//JDK1. After 5, the operation of packing and unpacking can be carried out automatically, which is more convenient
public class
public static void main (String []agrs)
int i=10
Integer j=i;
int k=j;
System. out. println(++k);
}; //output:11

another useful feature of wrapper class is that it can convert string type to corresponding basic data type. Let’s take the conversion of string to int as an example:
public class
public static void main (String[] args)
String str=”1500″;
int i=Integer. parseInt(str);
System. out. println(++i);
catch (Exception e)
e. ()//Print exception information
};// output: 1501
If STR = “15er00” in the above method; An exception will be generated when calling the parseInt method. See the description of the method in JDK API:
public static int parseInt (string s) throws

parse the string parameter as a signed decimal integer. All characters in the string must be decimal except that the first character can be an ASCII minus sign – (u002d) used to represent a negative value. Return the integer value obtained, as if this parameter and cardinality 10 were given to the parseInt (Java. Lang. string, int) method as parameters

s – string containing the int representation to be parsed
integer value represented by decimal parameter
– if the string does not contain a resolvable integer
& nbsp; The methods of converting string into the basic data type corresponding to the wrapper class are summarized as follows. All methods will throw exceptions and need to use try & nbsp Catch
& nbsp
index Packaging Basic data type returned Method name
1 Integer int static int Integer. parseInt(Sting s)
2 Character char static char Character. parseChar(String s)
3 Float Float static float Float. (String s)
4 Dobule double static double Double. (String s)
5 Byte byte static byte Byte. parseByte(String s)
6 Short short static short Short. (String s)
7 Long long static long Long. parseLong(String s)
8 Boolean boolean static boolean Boolean. (String s)

a similar method is parsexxx (string s, int radius). For integer wrapper class, this radius represents base ParseInt (string s) defaults to decimal. For the meaning of the radius parameter of other wrapper classes, see the API documentation

boxing is to encapsulate the basic type into a class. For example, when you encapsulate int into integer, you can’t treat it as a number, but as a class. Its method is needed for its operation. Unpacking is to convert a class into a basic type. For example, you can’t use a class to calculate an addition, so you have to convert it into a basic type. Jdk5 it can be converted automatically in the future. It seems. You should know the basic types, just those eight

explain the automatic packing and unpacking of Java

I. what is automatic packing and unpacking & nbsp

is very simple. You can see the packing and unpacking process in the following two sentences of code

12345 //automatic packing integer & nbsp; total=& nbsp;99; //custom unpacking int & nbsptotalprim=& nbsp; total;

to put it simply, boxing is to automatically convert the basic data type to the wrapper type; Unpacking is to automatically convert the wrapper type to the basic data type

let’s take a look at the types of packing and unpacking:

this process is automatically executed, so we need to see its execution process:

123456789 public & nbspclassMain { & nbsp; & nbsp; & nbsp; & nbsp; public & nbsp; static & nbsp; void & nbsp; main (string [] & nbsp; args) & nbsp; { & nbsp; & nbsp; & nbsp; //Auto boxing & nbsp;& nbsp;& nbsp;Integer total=& nbsp;99;& nbsp;& nbsp;& nbsp; //custom unpacking & nbsp& nbsp;& nbsp;& nbsp;inttotalprim=& nbsp; total;& nbsp;& nbsp;& nbsp; } }

decompile the class file to get the following contents:

1 javap & nbsp- c

Integer total = 99;& nbsp;
when executing the above code, the system executes it for us: & nbsp
Integer total = Integer. valueOf(99);

int totalprim = total;& nbsp;
when executing the above code, the system executes it for us: &nbsp
int totalprim = total. intValue();

let’s take integer as an example to analyze its source code: & nbsp
1. Let’s first look at integer Valueof function

123 public & nbspstaticInteger valueOf(inti) {returni>=128||i<-128?newInteger(i):& nbsp; SMALL_ VALUES[i+128]; }

it will first judge the size of I: if I is less than – 128 or greater than or equal to 128, create an integer object, otherwise execute small_ VALUES[i + 128]。

first let’s look at the constructor of integer:

123456789 private & nbspfinalintvalue;publicInteger(intvalue) {this.value=value;}publicInteger(Stringstring)throws { & nbsp; & nbsp; & nbsp; & nbsp; this (parseInt (string)); }

it defines a value variable, which is initialized when an integer object is created. The second passed in is a string variable, which will first convert it into an int value and then initialize it

let’s take a look at small_ What are values [i + 128]:

1 private & nbspstaticfinalInteger[] SMALL_ VALUES=& nbsp;newInteger[256];

it is a static integer array object, that is, the final valueof returns an integer object

so we can summarize here: the boxing process will create corresponding objects, which will consume memory, so the boxing process will increase memory consumption and affect performance

2. Then look at the intvalue function

1234 @ override public & nbspintintValue() { & nbsp; & nbsp; & nbsp; & nbsp; return & nbsp; value; }

this is very simple. You can directly return the value value

II. Related issues & nbsp

as we saw above, in the constructor of integer, it can be divided into two cases: & nbsp
1、i >= 128 || i < – 128 =====> new Integer(i)
2、i < 128 && amp; i >= – 128 =====> Small_ VALUES[i + 128]

1privatestaticfinalInteger[] SMALL_ VALUES=& nbsp;

Related Posts

Leave a Comment