Common objects in Java--basic wrapper classes

Source: Internet
Author: User

For more operations on the basic data types, more convenient operation, Java provides the corresponding class type for each of the basic data types. Wrapper class Type:

BYTE byte

Short Short

int Integer

Long Long

float float

Double Double

Char Character

Boolean Boolean

Used for conversions between the base data type and the string.

Demo

public class Integerdemo {

public static void Main (string[] args) {

No, the trouble is coming.

public static String tobinarystring (int i)

System.out.println (integer.tobinarystring (100));

public static String tooctalstring (int i)

System.out.println (integer.tooctalstring (100));

public static String tohexstring (int i)

System.out.println (integer.tohexstring (100));

public static final int Max_value

System.out.println (Integer.max_value);

public static final int Min_value

System.out.println (Integer.min_value);

}

}

how to construct an Integer:

public Integer (int value)

Public Integer (String s)

Note: This string must be made up of numeric characters

Demo

public class Integerdemo {

public static void Main (string[] args) {

Mode 1

int i = 100;

Integer II = new Integer (i);

System.out.println ("II:" + II);

Mode 2

String s = "100";

NumberFormatException

String s = "abc";

Integer III = new Integer (s);

System.out.println ("III:" + III);

}

}

Mutual conversions of type int and String type:

INT--String

String.valueof (number)

String--INT

Integer.parseint (s)

Demo:

public class Integerdemo {

public static void Main (string[] args) {

INT--String

int number = 100;

Mode 1

String S1 = "" + number;

System.out.println ("S1:" + S1);

Mode 2

String s2 = string.valueof (number);

System.out.println ("s2:" + s2);

Mode 3

INT--Integer--String

Integer i = new Integer (number);

String s3 = i.ToString ();

System.out.println ("S3:" + S3);

Mode 4

public static String toString (int i)

String S4 = integer.tostring (number);

System.out.println ("S4:" + S4);

System.out.println ("-----------------");

String--INT

String s = "100";

Mode 1

String--Integer--INT

Integer II = new Integer (s);

public int intvalue ()

int x = Ii.intvalue ();

System.out.println ("x:" + x);

Mode 2

public static int parseint (String s)

int y = Integer.parseint (s);

System.out.println ("y:" +y);

}

}

operation of the binary conversion in Integer

Common basic binary conversions

public static String tobinarystring (int i)

public static String tooctalstring (int i)

public static String tohexstring (int i)

Decimal to another binary

public static String toString (int i,int radix)

by this, we also saw the range ofthe binary: 2-36

for what ? 0,... 9,a...z

Other binary to Decimal

public static int parseint (String s,int Radix)

Demo

public class Integerdemo {

public static void Main (string[] args) {

decimal to binary, octal, hex

System.out.println (integer.tobinarystring (100));

System.out.println (integer.tooctalstring (100));

System.out.println (integer.tohexstring (100));

System.out.println ("-------------------------");

decimal to another binary

System.out.println (integer.tostring (100, 10));

System.out.println (integer.tostring (100, 2));

System.out.println (integer.tostring (100, 8));

System.out.println (integer.tostring (100, 16));

System.out.println (integer.tostring (100, 5));

System.out.println (integer.tostring (100, 7));

System.out.println (integer.tostring (100,-7));

System.out.println (integer.tostring (100, 70));

System.out.println (integer.tostring (100, 1));

System.out.println (integer.tostring (100, 17));

System.out.println (integer.tostring (100, 32));

System.out.println (integer.tostring (100, 37));

System.out.println (integer.tostring (100, 36));

System.out.println ("-------------------------");

other binary to decimal

System.out.println (Integer.parseint ("100", 10));

System.out.println (Integer.parseint ("100", 2));

System.out.println (Integer.parseint ("100", 8));

System.out.println (Integer.parseint ("100", 16));

System.out.println (Integer.parseint ("100", 23));

NumberFormatException

System.out.println (Integer.parseint ("123", 2));

}

}

new features of JDK5-Auto boxing / unpacking:

Auto-Boxing: Converting a base type to a wrapper class type

Auto-unpacking: Converting wrapper class types to basic types

Note A small problem:

when in use, Integer x = null; The code will appear NullPointerException.

It is recommended that you first determine whether it is null before youuse it.

Demo

public class Integerdemo {

public static void Main (string[] args) {

defines a wrapper class type variable I for an int type

Integer i = new integer (100);

Integer II = 100;

II + = 200;

System.out.println ("II:" + II);

through the post-compilation code

Integer II = integer.valueof (100); Automatic Boxing

II = integer.valueof (Ii.intvalue () + 200); automatic unpacking and automatic packing

System.out.println ((New StringBuilder ("II:")). Append (ii). toString ());

Integer III = NULL;

NullPointerException

if (iii! = NULL) {

III + = 1000;

SYSTEM.OUT.PRINTLN (iii);

}

}

}

Integer data is directly assigned, and if between -128 and 127 , the data is fetched directly from the buffer pool:

Demo

public class Integerdemo {

public static void Main (string[] args) {

Integer i1 = new Integer (127);

Integer i2 = new Integer (127);

System.out.println (I1 = = I2);

System.out.println (I1.equals (I2));

System.out.println ("-----------");

Integer i3 = new Integer (128);

Integer i4 = new Integer (128);

System.out.println (i3 = = I4);

System.out.println (I3.equals (I4));

System.out.println ("-----------");

Integer i5 = 128;

Integer I6 = 128;

SYSTEM.OUT.PRINTLN (i5 = = I6);

System.out.println (I5.equals (I6));

System.out.println ("-----------");

Integer i7 = 127;

Integer i8 = 127;

System.out.println (i7 = = i8);

System.out.println (I7.equals (i8));

by looking at the source code, we know that for data between -128 and 127 , a data buffer pool is made, and if the data is within that range, no new space is created each time

Integer II = integer.valueof (127);

}

}

Common objects in Java--basic wrapper classes

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.