Java basic data types and wrapper types

Source: Internet
Author: User

Wrapper classes for basic data types

Why do I need a wrapper class?

Java is not a purely object-oriented language. The Java language is an object-oriented language, but Java's basic data types are not object-oriented. However, we often need to transform the basic data into objects in practical use, which is easy to operate. For example: In the operation of a collection, this is where we need to convert the basic type data into objects.

Packaging classes are located in the Java.lang package, you do not need to import, the system automatically imported. The 8 wrapper classes in Java are: Byte,short,integer,long,float,double,character,boolean They are all used the same way, enabling bidirectional conversion of the native data type to the wrapper type.

Object relationships for wrapper classes and base data types:

Basic type Wrapper type
Boolean Boolean
Char Character
Int Integer
Byte Byte
Short Short
Long Long
Float Float
Double Double

How do I use the wrapper class?

The role of the wrapper class:

    • Provides: strings, basic data types, and how objects are converted to each other!
    • Contains related properties for each of the basic data types, such as: maximum, minimum, etc.

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. The integer class inherits the number parent class.

1, to achieve the conversion between int and integer class (this process uses the principle of automatic boxing and automatic unpacking)

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 = tennew Integer (+); // converting an int type to an integer type 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.

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 int n = integer.parseint ("+", "ten"); // converts the string "12" to int according to the 16 binary, the result is int n = integer.parseint ("a", +); // converts the string "FF" to int according to 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 = integer.tostring (m);//The value of string S is "1000".

We often use automatic boxing and automatic unpacking when converting between the wrapper class and the base data type. This is the new feature of JDK5.0.

Auto-boxing and automatic unpacking (auto-boxing & auto-unboxing)

A new feature of JDK5.0 is automatic boxing and automatic unpacking.

Automatic Boxing

    • The base type is automatically encapsulated in a wrapper of the similar type as: Integer i = 100;
    • Essentially, the compiler is added automatically at compile time: integer i = new integer (100);

Automatic unpacking

    • Wrapper class objects are automatically converted into basic data types. such as: int a = new Integer (100);
    • Essentially, the compiler is added automatically at compile time: int a = new Integer. Intvalue ();

Problems encountered in auto-boxing and auto-unpacking problems in the integer class:

Integer  d = 1234; integer  = 1234;        System.out.println (d= =D2); System.out.println (D.equals (D2));        System.out.println ("###################"= -100;    // the number between [-128,127] is still treated as a basic data type. Integer d4 = -100; SYSTEM.OUT.PRINTLN (d3= =D4); System.out.println (D3.equals (D4));

Show Results:

false true ################### true true

When the two numbers are 100, the = = judgment is equal, and when the two numbers are 200, the judgments are unequal.

Cause: Looking at the internal implementation code, the integer class has a cache problem that caches integers between -128~127. That is, if the value between -128~127 is assigned to an integer class object, the value of two objects is compared when comparing two objects (D3==D4). The memory address of the object is compared to this range.

Reference:

Http://www.mamicode.com/info-detail-1015549.html

http://www.jobui.com/mianshiti/it/java/7088/

http://cakin24.iteye.com/blog/2326545

http://lib.csdn.net/article/javase/2159 (the above content is transferred from this article)

Http://www.cnblogs.com/mengdd/archive/2013/01/21/2869921.html

http://www.jianshu.com/p/5d7c9b077771

Http://www.cnblogs.com/huajiezh/p/5225637.html

Http://www.cnblogs.com/xianzheng/p/5205968.html

Java basic data type and wrapper type (RPM)

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.