"Java Basic data type wrapper class"

Source: Internet
Author: User
Tags float double

I. Overview

There are 8 data types in Java, namely byte short int long boolean float double char, corresponding to this, there are 8 classes corresponding to each:

BYTE byte

Short Short

int Integer

Long Long

Boolean Boolean

float float

Double Double

Char Character

What are the benefits of encapsulating a basic data type as an object?

The basic data type wrapper class encapsulates a number of properties and methods that make the operation of the data more convenient and flexible.

To illustrate:

To get the maximum value that the int type can represent, you only need to use the static property: Integer.max_value.

To convert a decimal number to a binary number, you only need to call the static method: the Integer.tobinarystring () method.

Second, the basic data type and the conversion between the strings.

1. The base data type is converted to a string.

Method:

1.1 Basic data type + ""

1.2 Static methods using the String class valueof

1.3 Non-static methods for wrapping classes using basic data Types valueof

2. The string is converted to the base data type.

Method:

2.1 Use the Parsexxx method.

Usage Example: int x=integer.parseint ("123");

Note that the argument string in the method must be valid, otherwise the run-time exception NumberFormatException will be thrown.

In addition, there are Long.parselong, double.parsedouble, Float.parsefloat, Boolean.parseboolean, Byte.parsebyte, Short.parseshort method, but it should be noted that there is no similar method in the character class, because it is obvious that the method is meaningless once the string length exceeds 1.

2.2 Create a new object using the construct method.

For example: Integer x=new integer ("123");

Other basic data type wrapper class usages are similar. However, you should still pay attention to the construction method of the character class.

The conversion between the decimal number and the other binary numbers.

1. The decimal number is converted to a different binary number.

1.1 Use three common methods.

public static String tobinarystring (int i); This method converts a decimal number to a complement representation of a binary number.

public static String tohexstring (int i); This method converts a decimal number to a hexadecimal number.

public static String tooctalstring (int i); This method converts the decimal number to an octal number.

1.2 Use the ToString method.

Character.MIN_RADIXCharacter.MAX_RADIX。

Usage Example: String str=integer.tostring (12,2); Converts the decimal number 12 to a 2 binary number and returns the string representation of the binary number.

2 other binary numbers are converted to decimal numbers.

Use the parseint method.

public static int parseint (String s,int radix) throws NumberFormatException;
The method can treat string x as a radix and convert the number to a decimal number.
Throws a NumberFormatException exception if the conversion fails.

Four, automatic packing, unpacking operation.

If the base data type is already encapsulated as an object, how do you get the value of that base data type?

Usage: Intvalue (); (For integer Class) The use of other methods is similar.

Double.doublevalue (), Float.floatvalue (), Boolean.booleanvalue (), Byte.bytevalue (), Character.charactervalue (), Long.longvalue (),

Short.shortvalue ()

Example 1: Automatic Boxing example.

1  Public classDemo2 {3      Public Static voidMain (String args[])4     {5 Demo1 ();6     }7      Public Static voidDemo1 ()8     {9Integer i=NewInteger (123);TenInteger j=123; OneSystem.out.println (i==j); A System.out.println (I.equals (j)); - System.out.println (I.compareto (j)); -     } the}
View Code

Operation Result:

False

True

0

Automatic boxing is the process by which the basic data types are automatically encapsulated into a basic data type wrapper object.

The Equals method compares in the basic data type wrapper class whether the value of the base data type in the object is the same.

Example 2: About special Values 127

1  Public classDemo2 {3      Public Static voidMain (String args[])4     {5         //Demo1 ();6 Demo2 ();7     }8      Public Static voidDemo2 ()9     {Ten          for(inti=1;i<=200;i++) One         { AInteger x=i; -Integer y=i; -System.out.println (i+ ":" + (x==y)); the         } -     } -      Public Static voidDemo1 () -     { +Integer i=NewInteger (123); -Integer j=123; +System.out.println (i==j); A System.out.println (I.equals (j)); at System.out.println (I.compareto (j)); -     } -}
View Code

Look at line 127, the result before this line is true, the result after this line is false, for a reason.

JDK1.5 not only made the rules for automatic boxing and automatic unpacking, but also made a provision for the size of integers to open up new space storage objects:

If a byte is automatically boxed, the data is shared without re-opening the space.

Five, judge the unpacking operation or boxing operation.

1  Public classDemo2 {3      Public Static voidMain (String args[])4     {5         //Demo1 ();6         //Demo2 ();7 Demo3 ();8     }9      Public Static voidDemo3 ()Ten     { OneInteger I=1; AI=i+1; - System.out.println (i); -     } the      Public Static voidDemo2 () -     { -          for(inti=1;i<=200;i++) -         { +Integer x=i; -Integer y=i; +System.out.println (i+ ":" + (x==y)); A         } at     } -      Public Static voidDemo1 () -     { -Integer i=NewInteger (123); -Integer j=123; -System.out.println (i==j); in System.out.println (I.equals (j)); - System.out.println (I.compareto (j)); to     } +}
View Code

Run Result: 2

The most critical sentence is i=i+1; this operation.

This includes automatic unpacking and auto-boxing operations.

analysis :i+1, we know that operations in Java must be an operation between data of the same type. Here, a basic data type 1, there is a basic data type wrapper class object, then what is the operation between them? First, the operations in Java can only be operations between the basic data types, so, here, the object of the basic data type wrapper class calls the Intvalue method, returns an integer, where the integer is 1, and then the result is 2, but 2 is the base data type, so if you want to assign a value to I, There is also a need for an automatic boxing operation.

"Java Basic data type wrapper class"

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.