Home > Backend Development > XML/RSS Tutorial > Chapter 2 Packaging

Chapter 2 Packaging

黄舟
Release: 2016-12-21 13:05:27
Original
1913 people have browsed it

We know that Java is an object-oriented language, and one of its essences is that polymorphism can be used to improve the flexibility of the program.

But there are 8 basic types in java: byte, short, int, long, float, double, char, boolean. They exist in memory as values, not objects.

They are not subclasses of Object and cannot participate in object-oriented development. Before the Java 1.5 version, the following code could not pass.

package com.souvc.api;public class Test {    public static void main(String[] args) {
        String str = "hello";
        doSome(str);// 可以,因为String是Object的子类

        int i = 1;        // 程序编译不通过,原因在于int不是Object的子类,不能使用多态性。        doSome(i);
    }    public static void doSome(Object o) {        // ....    }
}
Copy after login

The reason for the problem is that the basic type int has no inheritance relationship and is not a subclass of Object. Therefore, if we want the basic type to exist in the form of an object, we need to define a class Integer, and then use its instance to describe a basic type int.

The advantage of this is that we use objects to describe basic type data, and classes inherit from Object. This allows basic types to participate in object-oriented development. Fortunately, classes like Integer do not need to be defined by us, because Java already provides wrapper classes corresponding to 8 basic types.

Note: The automatic unboxing feature appeared after version 1.5 of Java, and the above code can be compiled normally. We will describe the automatic unpacking and packing of boxes in detail later.

For 8 basic types, java provides their corresponding packaging classes:

Basic type packaging class

byte java.lang.Byte

short java.lang.Short

int java.lang. Integer

long java.lang.Long

float java.lang.Float

double java.lang.Double

char java.lang.Character

boolean java.lang.Boolean

Except Character and Boolean Except for the parent class Object, the rest are inherited from: java.lang.Number

Number is an abstract class. It cannot be instantiated itself. Subclasses of Number must provide methods to convert the represented value to byte, double, float, int, long and short

For example:

abstract double doubleValue() returns the specified value in double form

abstract int intValue() Returns the specified numerical value in the form of int

abstract float floatValue() Returns the specified numerical value in the form of float

For the remaining abstract methods, please refer to the API documentation: java.lang.Number.

Now let’s learn how to convert between basic types and wrapper classes.

When we need to convert the basic type to a wrapper class, we can call a static method valueOf() of the wrapper class:

Integer i = Integer.valueOf(1);
Double d = Double.valueOf(1.1 :

Integer i = new Integer(1);int n = i.intValue();

Double d = new Double(1.1);double dn = d.doubleValue();


Although we can pass the above Methods convert between basic types and wrapper classes. But it is relatively troublesome when actually writing code. Java launched a new feature after version 1.5: automatic unboxing.

The following code fails to compile in java1.4, but it can be compiled after java1.5:

int i = new Integer(1);//You can automatically convert the wrapper class into a basic type and automatically unbox it

Integer in = 1;//You can automatically convert basic types into packaging classes. Automatic boxing


So how does Java implement automatic unboxing?

In fact, JVM does not support this feature. Automatic unboxing is just a compiler. "Preprocessing" operations during compilation. The compiler makes changes when compiling to bytecode when it sees the need to convert between wrapper classes and primitive types:

In compiled bytecode in source code

Integer a = 100 => Integer a = Integer.valueOf(100);

Integer b = 200 => Integer b = Integer.valueOf(200);

Integer c = a+b => Integer c = Integer.valueOf (a.intValue( ) + b.intValue( ));

double d = c => double d = c . doubleValue( );

2.1 Integer class - integer class 34


2.1.1 Constructor method - constructor method of Integer class 34
2.1.2 Constant - constant of Integer class 34
2.1.3 bitCount method - get the number of 1 in two's complement 34
2.1.4 byteValue method ——Get the value of byte type 35
2.1.5 CompareTo method——Compare integer 35
2.1.6 Decode method——Decode the string into int type 35
2.1.7 DoubleValue method——Return the double value 36
2.1.8 equals method - determine the equality of integer objects 36
2.1.9 FloatValue method - get the float value 37
2.1.10 GetInteger method - get the system attribute value of the integer 37
2.1.11 hashCode method - generate the hash code of the integer 39
2.1.12 highestOneBit method - Get the index of the highest bit 1 of the integer binary 39
2.1.13 intValue() method - get the int value 40
2.1.14 longValue method - get the long value 40
2.1.15 lowestOneBit method ——Get the index of the lowest binary bit 1 of the integer 41
2.1.16 parseInt method——Parse the string into an int value 41
2.1.17 reverse method——Reverse the bit order of the two’s complement of the integer 43
2.1.18 reverseBytes Method - Reverse the order of integer bytes 44
2.1.19 | shortValue method - get the short value 44
2.1.20 | signum method - get the integer sign 44
2.1.21 | toBinaryString method - generate a binary string of integers 45
2.1.22 toHexString method - generates a hexadecimal string of integers 45
2.1.23 toOctalString method - generates an octal string of integers46
2.1.24 toString method - generates a decimal string of integers47
2.1 .25 ValueOf method - Create Integer object 49


2.2 Long class - Long integer class 50


2.2.1 Constructor method - Constructor method of Long class 51
2.2.2 Constant - Constant of Long class 51
2.2.3 bitCount method - get the number of 1's in two's complement 51
2.2.4 byteValue method - get the byte value 51
2.2.5 compareTo method - compare long integers 52
2.2.6 decode method—— The string is decoded into long type 52
2.2.7 | doubleValue method - returns the double value 53
2.2.8 | equals method - determines the equality of long integer objects 53
2.2.9 | floatValue method - gets the float value 54
2.2.10 | getLong Method - Get the system attribute value of the long integer 54
2.2.11 | hashCode method - Generate the hash code of the long integer 56
2.2.12 | highestOneBit method - Get the index of the highest binary bit 1 of the long integer 56
2.2.13 | intValue () method - get the int value 57
2.2.14 longValue method - get the long value 57
2.2.15 lowestOneBit method - get the index of the lowest bit 1 of the long integer binary 57
2.2.16 parseLong method - convert the string Parsed as a long value 58
2.2.17 Reverse method - Reverse the bit order of the two's complement of a long integer 60
2.2.18 ReverseBytes method - Reverse the order of the long integer bytes 60
2.2.19 ShortValue method - Get short value 61
2.2.20 signum method - Get the long integer sign 61
2.2.21 |toBinaryString method - generate a long integer binary string 61
2.2.22 toHexString method - generate a long integer hexadecimal string 62
2.2.23 toOctalString method - generates an octal string of long integer 62
2.2.24 toString method - generates a decimal string of long integer 63
2.2.25 valueOf method - creates a Long object 65


2.3 Short Class - short integer class 67


2.3.1 Constructor - constructor method of Short class 67
2.3.2 Constant - constant of Short class 67
2.3.3 compareTo method - compare short integers 67
2.3 .4 decode method - decode the string into short type 68
2.3.5 |doubleValue method - return the double value 68
2.3.6 |equals method - determine whether short integer objects are equal 68
2.3.7 | floatValue method - get the float value 69
2.3.8 hashCode method - generates a hash code of a short integer 69
2.3.9 intValue() method - obtains an int value 70
2.3.10 longValue method - obtains a long value 70
2.3.11 parseShort method— —Parse the string into a short value 70
2.3.12 reverseBytes method—reverse the order of short integer bytes72
2.3.13 shortValue method—get the short value 72
2.3.14 toString method—generate a short integer Decimal string 73
2.3.15 ValueOf method - Create Short object 73


2.4 Boolean class - Boolean class 75


2.4.1 Constructor - Constructor method of Boolean class 75
2.4.2 Constant - Constants of the Boolean class 75
2.4.3 | booleanValue method - Get boolean value 76
2.4.4 | compareTo method - compare Boolean values ​​76
2.4.5 | equals method - determine equality 77
2.4.6 | getBoolean method - get Boolean Type system attribute value 77
2.4.7 hashCode method - generates a hash code of a Boolean object78
2.4.8 parseBoolean method - parses a string into a boolean value78
2.4.9 toString method - generates a Boolean value String 78
2.4.10 valueOf method - Create Boolean object 79


2.5 Byte class - byte object 80


2.5.1 Constructor - constructor of Byte class 80
2.5.2 Constant - constant of Byte class 80
2.5.3 compareTo method - compare bytes Object 80
2.5.4 decode method - decode the string into Byte value 81
2.5.5 |doubleValue method - get the double value 82
2.5.6 equals method - determine byte equality 82
2.5.7 floatValue method - — Get the float value 83
2.5.8 | hashCode method - generate the hash code of the byte object 83
2.5.9 | intValue method - get the int value 83
2.5.10 | longValue method - get the long value 83
2.5.11 parseByte method - parse the string into a byte value 84
2.5.12 | shortValue method - get the short value 85
2.5.13 | toString method - generate a decimal string of byte values ​​85
2.5.14 | valueOf method - create Byte object 86


2.6 Character class - character class 88


2.6.1 Constructor method - constructor method of Character class 88
2.6.2 Constant - constant of Character class 88
2.6.3 charCount method - Calculate the number of code points of the specified character 89
2.6.4 | charValue method - get the char value 89
2.6.5 | codePointAt method - get the code point of the character array element 90
2.6.6 | codePointBefore method - get the previous character array index The code point of the element 91
2.6.7 The codePointCount method - returns the number of code points in the subarray of the character array 93
2.6.8 The compareTo method - compares character objects 94
2.6.9 The equals method - determines whether the character objects are equal 95
2.6.10 getNumericValue method - returns the int value represented by the character95
2.6.11 getType method - returns a value indicating the general category of the character97
2.6.12 hashCode method - generates the hash code of the character object97
2.6.13 isDefined method - determine whether it is a Unicode character 98
2.6.14 isDigit method - determine whether it is a numeric character 98
2.6.15 isLetter method - determine whether it is an alphabetic character 99
2.6.16 isLowerCase method - determine Whether it is a lowercase character 100
2.6.17 isUpperCase method - determine whether it is an uppercase character 100
2.6.18 toLowerCase method - convert to a lowercase character 101
2.6.19 |toUpperCase method - convert to an uppercase character 101


2.7 Double ——Double precision number class 102


2.7.1 Constructor method——Constructor method of Double class 102
2.7.2 Constant——Constant of Double class 102
2.7.3 byteValue method——Get byte value 102
2.7. 4. compare method - compare double precision digital objects 103
2.7.5 compareTo method - compare two Double objects 103
2.7.6 intValue method - return this Double value in int form 104
2.7.7 doubleToLongBits method - Return the representation of the specified floating point value 104
2.7.8 | doubleToRawLongBits method - retain NaN values ​​and return the representation of the specified floating point value 105
2.7.9 | doubleValue method - get the double value 105
2.7.10 | equals method - judge Whether Double objects are equal106
2.7.11 floatValue method--obtain float value 107
2.7.12|hashCode method--generate the hash code of Double object107
2.7.13|isInfinite method--determine whether the size of Double value is infinite107
2.7.14 isNaN method - determine whether the Double value is a non-numeric value 108
2.7.15 longBitsToDouble method - return the double value of the given bit representation 109
2.7.16 longValue method - get the long value 110
2.7. 17. The parseDouble method - parses the string into a double value 110
2.7.18 The shortValue method - gets the short value 110
2.7.19 The toHexString method - generates a hexadecimal string of double precision numbers 111
2.7.20 toString Method - Generate a decimal string of double precision numbers 112
2.7.21 ValueOf method - Create a Double object 112


2.8 Float - Floating point class 113


2.8.1 Constructor method - Constructor method of Float class 113
2.8.2 Constants - Constants of Float class 114
2.8.3 byteValue method - Get byte value 114
2.8.4 Compare method - Compare Float objects 114
2.8.5 compareTo method - compares the values ​​represented by two Float objects 115
2.8.6 doubleValue method - obtains the double value 115
2.8.7 equals method - determines whether Double objects are equal 115
2.8.8 floatToIntBits method ——Return the representation of the floating point value 116
2.8.9 floatToRawIntBits method——Keep the non-numeric value and return the representation of the specified floating point value 117
2.8.10 floatValue method——Get the float value 118
2.8.11 hashCode method— — Returns the hash code of the Float object 118
2.8.12 intBitsToFloat method — returns the float value in the specified bit representation 118
2.8.13 intValue method — gets the int value 119
2.8.14 isInfinite method — determines the float value Whether the size is infinite 120
2.8.15 | isNaN method - determine whether the Float value is a non-numeric value 120
2.8.16 | longValue method - get the long value 121
2.8.17 | parseFloat method - parse the string into a float value 121
2.8.18 shortValue method--get the short value 122
2.8.19 toHexString method--generate a hexadecimal string of floating-point numbers 122
2.8.20 toString method--generate a decimal string of floating-point numbers 123
2.8 .21 ValueOf method - Create floating point object 124

The above is the content of the packaging class. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template