Java Basic data type wrapper class

Source: Internet
Author: User
Tags character classes

http://blog.163.com/bt_q/blog/static/11926672920104902636829/

The Java language is an object-oriented language, but the basic data types in Java are not object-oriented, which in the actual use of a lot of inconvenience, in order to solve this shortcoming, in the design of the class for each basic data type to design a corresponding class to represent, The classes that correspond to the eight basic data types are collectively referred to as wrapper classes (Wrapper Class), and some are translated as outer or data type classes.

The wrapper classes are located in the Java.lang package, and the corresponding relationship between the wrapper class and the base data type is shown in the following table:

Wrapper class Correspondence Table

Basic data types

Packing class

Byte

Byte

Boolean

Boolean

Short

Short

Char

Character

Int

Integer

Long

Long

Float

Float

Double

Double

In these eight class names, except for the integer and character classes, the class name and the base data type of the other six classes have been all the time, only the first letter of the class name is capitalized.

For the wrapper class, the use of these classes consists of two main types:

A, as the basic data type corresponding to the class type exists, convenient to involve the operation of the object.

b, contains the relevant attributes for each basic data type, such as maximum, minimum, and related methods of operation.

Since the use of eight packaging classes is similar, the following is an example of the most commonly used integer class to describe the actual use of the wrapper class.

1. Implement conversions between int and integer classes

In the actual conversion, using the integer class construction method and the internal Intvalue method of the integer class to achieve the conversion between these types, the implementation of the code is as follows:

int n = 10;

Integer in = new Integer (100);

converting an int type to an integer type

Integer in1 = new integer (n);

Convert an object of type integer to an int type

int m = In.intvalue ();

2, Integer class internal common methods

Inside the integer class are some methods related to int operations, and some of the more common methods are described below:

A, parseint method

public static int parseint (String s)

The function of this method is to convert a numeric string to an int value. Converting a string to the corresponding int number is a common operation in future interface programming. Examples of use are:

String s = "123";

int n = integer.parseint (s);

The value of the INT variable n is 123, which actually implements the conversion between the string and int, and if the string contains not all numeric characters, the execution of the program will be an exception. (Description: The concept of an exception will be described in the next chapter)

Another parseint method:

public static int parseint (String s, int radix)

The implementation converts the string to an int as specified by the parameter radix, using the following example:

Converts the string "120" to int by decimal, the result is 120

int n = integer.parseint ("120", 10);

Converts the string "12" to int by 16, the result is 18

int n = integer.parseint ("12", 16);

Converts the string "FF" to int by 16, the result is 255

int n = integer.parseint ("FF", 16);

This allows for a more flexible conversion.

B, ToString Method

public static String toString (int i)

The function of this method is to convert the int type to the corresponding string type.

Use the sample code as follows:

int m = 1000;

String s = integer.tostring (M);

The value of the string s is "1000".

Another ToString rule implements converting an int value to a specific binary string:

public static int parseint (String s, int radix)

Use the sample code as follows:

int m = 20;

String s = integer.tostring (M);

The value of the string s is "14".

In fact, since the 1.5 (5.0) version of the JDK has introduced the Automatic Disassembly box syntax, that is, in the basic data types and corresponding packaging class conversion, the system will automatically, which will greatly facilitate the programmer's code writing. Use the sample code as follows:

int type is automatically converted to an integer type

int m = 12;

Integer in = m;

The integer type is automatically converted to type int

int n = in;

Therefore, the type conversion will be very simple in actual use, the system will automatically implement the corresponding conversion

Java basic data type wrapper class

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.