Java basic types and wrapper classes

Source: Internet
Author: User

Java is an object-oriented language in which classes in Java Connect methods to data and form self-contained processing units. However, in Java, you cannot define a primitive type (primitive type), in order to be able to treat a primitive type as an object, and to connect related methods, Java provides wrapper classes for each of the basic types, so that we can turn these basic types into objects to handle. These packaging classes are: boolean,byte,short,character,integer,long,float,double, void total of 9 (note: date is not, no base type).

First, the packaging class (Wrapper Class) Common method

It is worth noting that Java can handle the basic type directly, but in some cases we need to treat it as an object, then we need to turn it into a wrapper class. All packaging classes (Wrapper Class) have common methods, and they 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 ().

Transition Relationship:

Basic type------> wrapper class
Integer obj=new integer (145);

Wrapper class------> Basic Types
int Num=obj.intvalue ();

string------> Wrapper class
Integer obj=new Integer ("-45.36");

Wrapper class------> String wrapper class

String str=obj.tostring ();

String------> Basic Type
int Num=integer.parseint ("-45.36");

Basic type------> String wrapper class

String str=string.valueof (5);


In certain occasions, the use of Java Packaging class to solve the problem, can greatly improve the efficiency of programming.

Second, the new features of JDK1.5: Automatic packaging/unpacking (autoboxing/unboxing)

Automatic unpacking/unpacking greatly facilitates the use of basic type data and their wrapper classes.

Automatic package: The basic type automatically turns into a wrapper class. (int >> Integer)

Automatic unpacking: Wrapper classes are automatically converted to basic types. (Integer >> int)

Before JDK1.5, we were always obsessed with the collection being unable to hold the base type, and now the automatic conversion mechanism
Solved our problem.

int a = 3;
Collection C = new ArrayList ();
C.add (a);//Convert to integer automatically.

Integer b = new integer (2);
C.add (b + 2);

Here the integer is automatically converted to int for the addition operation, and then int is converted to integer again.

Java basic types and wrapper classes

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.