Java packaging and Java Packaging

Source: Internet
Author: User

Java packaging and Java Packaging

1. What is a Java packaging class?

The so-called Java packaging class is to package the eight basic data types in Java into classes. The following table lists the mappings between packages and basic data types.

Basic Data Type

Packaging

Byte

Byte

Short

Short

Int

Integer

Long

Long

Float

Float

Double

Double

Char

Character

Boolean

Boolean

Here, Byte, Short, Integer, Long, Float, and Double are subclasses of Number, and Character and Boolean are subclasses of Object.

Ii. Why is there a Java packaging class?

Java is an object-oriented programming language that advocates "Everything is an object", while the basic data type in Java is not an object, this causes a lot of inconvenience when we want to operate on the basic data type. For example, we want to convert the integer data to a string or convert the string to an integer or floating point data, or to obtain the binary, octal, and hexadecimal values of an integer, we need to perform operations from the object's perspective. Therefore, we can package basic data types into classes, in order to process the basic data type as an object.

Iii. packing and unpacking-mutual conversion between basic data types and packaging classes

The conversion between the basic data type and the packaging class is achieved through packing and unpacking. The packaging class provides packing and unpacking methods, such:

Through the construction method of the packaging class, we can implement packing

Integer a = new Integer (6); // boxed

Through the unpacking method of the packaging class, we can achieve unpacking

Float a = new Float (66.6f); // binning float B = a. floatValue (); // unpack

Before JDK1.5, the packing and unpacking methods must be manually implemented. After JDK1.5, Java provides automatic packing and unpacking mechanisms, for example:

Integer I = 60; // automatically bind Float f = 66.6f; // automatically bind int a = I; // automatically Unbox Float B = f; // automatically Unbox

Iv. Packaging applications

In actual application, we can change the string to the basic data type through the packaging class, for example:

Int I = Integer. parseInt ("123"); // Replace "123" with float f = Float. parseFloat ("456"); // converts "456" to float data.

You can also change the basic data type to a string, for example:

String s = Integer. toString (123); // replace 123 with a String

You can also convert binary, octal, decimal, and hexadecimal values, for example:

Int m = 10; String binaryString = Integer. toBinaryString (m); // obtain the binary form of m. String hexString = Integer. toHexString (m); // obtain the hexadecimal form of m. String octalString = Integer. toOctalString (m) // obtain the octal form of m.

In addition, the APIS provided by the packaging class can perform many operations.

V. Summary

Java packaging class encapsulates the basic data types from the object-oriented perspective, in order to operate on the basic data types with the object-oriented idea in the program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.