Java wrapper class

Source: Internet
Author: User
Tags character classes

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:

basic data type

wrapper class

byte

byte

boolean

boolean

short

short

char

character

int

integer

long

long

float

float

Double

Double

In these eight class names, in addition to the integer and character classes, the class names and basic data types of the other six classes are identical, except for the first letter of the class name. For the wrapper class, the main uses of these classes are:

(1) A constructor that has a basic value parameter and creates a wrapper class object. If you can create an object with an integer wrapper class, Integer obj=new integer (145);
(2) A constructor that takes a string argument and creates a wrapper class object. such as new Integer ("-45.36");
(3) Typevalue method, such as Obj.intvalue (), which can generate the base value of the object;
(4) A Parsetype method that converts a string to a base value, such as Integer.parseint (Args[0]);
(5) Hashcode method for generating the code of the halon table, such as Obj.hascode ();
(6) The Equals () method that compares two objects of the same class, such as Obj1.eauqls (OBJ2);
(7) The ToString () method that generates the string notation, such as obj.tostring ().

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);//convert int type to integer type integer in1 = new integer (n);//Convert an object of type integer to int m = i N.intvalue ();
2, Integer class internal common methods
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:

The string "120" is converted to int by decimal, then the result is 120int n = integer.parseint ("120", 10);//The string "12" is converted to int by 16, then the result is 18int n = Integer.parseint ("12", 16);//Convert the string "FF" to int in 16, the result is 255int 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 unboxing of the syntax, that is, in the basic data type and corresponding wrapper class conversion, the system will automatically, which will greatly facilitate the programmer's code writing. Use the sample code as follows:

<span style= "Font-family:microsoft Yahei;" The >//int type is automatically converted to an Integer type int m = 12;integer in = M;//integer type is automatically converted to int type int n = in;</span>

Java 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.