search
Chapter 2 PackagingDec 21, 2016 pm 01:05 PM

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) {        // ....    }
}

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)!


Statement
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
Is the conversion speed fast when converting XML to PDF on mobile phone?Is the conversion speed fast when converting XML to PDF on mobile phone?Apr 02, 2025 pm 10:09 PM

The speed of mobile XML to PDF depends on the following factors: the complexity of XML structure. Mobile hardware configuration conversion method (library, algorithm) code quality optimization methods (select efficient libraries, optimize algorithms, cache data, and utilize multi-threading). Overall, there is no absolute answer and it needs to be optimized according to the specific situation.

How to Use RSS Feeds for News Aggregation and Content Curation?How to Use RSS Feeds for News Aggregation and Content Curation?Mar 10, 2025 pm 03:47 PM

This article explains how to use RSS feeds for efficient news aggregation and content curation. It details subscribing to feeds, using RSS readers (like Feedly and Inoreader), organizing feeds, and leveraging features for targeted content. The bene

How Do I Use Atom Publishing Protocol for Web Content Management?How Do I Use Atom Publishing Protocol for Web Content Management?Mar 10, 2025 pm 05:48 PM

This article explains Atom Publishing Protocol (AtomPub) for web content management. It details using HTTP methods (GET, POST, PUT, DELETE) with Atom format for content creation, retrieval, updating, and deletion. The article also discusses AtomPub

How Do I Implement Content Syndication Using RSS?How Do I Implement Content Syndication Using RSS?Mar 10, 2025 pm 03:41 PM

This article details implementing content syndication using RSS feeds. It covers creating RSS feeds, identifying target websites, submitting feeds, and monitoring effectiveness. Challenges like limited control and rich media support are also discus

How Can I Secure RSS Feeds Against Unauthorized Access?How Can I Secure RSS Feeds Against Unauthorized Access?Mar 10, 2025 pm 03:42 PM

This article details securing RSS feeds against unauthorized access. It examines various methods including HTTP authentication, API keys with rate limiting, HTTPS, and content obfuscation (discouraged). Best practices involve IP restriction, revers

How to convert XML files to PDF on your phone?How to convert XML files to PDF on your phone?Apr 02, 2025 pm 10:12 PM

It is impossible to complete XML to PDF conversion directly on your phone with a single application. It is necessary to use cloud services, which can be achieved through two steps: 1. Convert XML to PDF in the cloud, 2. Access or download the converted PDF file on the mobile phone.

How Can I Integrate XML and Semantic Web Technologies?How Can I Integrate XML and Semantic Web Technologies?Mar 10, 2025 pm 05:50 PM

This article explores integrating XML and Semantic Web technologies. The core issue is mapping XML's structured data to RDF triples for semantic interoperability. Best practices involve ontology definition, strategic mapping approaches, careful att

How Do I Use XML for Data Interoperability in Healthcare/Finance/etc.?How Do I Use XML for Data Interoperability in Healthcare/Finance/etc.?Mar 10, 2025 pm 05:50 PM

This article details using XML for data interoperability, focusing on healthcare and finance. It covers schema definition, XML document creation, data transformation, parsing, and exchange mechanisms. Key XML standards (HL7, DICOM, FinML, ISO 20022)

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software