Java learning - packaging

First, the meaning of

Objective: The basic data types for the properties of the object includes , for each elementary data type provides a wrapper class.

basic type Corresponding packaging
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Second, boxing and unboxing

Packing: The basic data type conversion to its corresponding packaging.
Unpacking: the split wrapper class corresponding to the basic data types.

2.1 Packing

2.1.1 autoboxing

int num1 = 5;
Integer num2 = num1; // 自动装箱。

2.1.2 Manual packing

int num1 = 5;
Integer num2 = new Integer(num1); // 手动装箱。

2.2 Unboxing

2.2.1 auto-unboxing

Integer num1 = new Integer(5);
int num2 = num1; // 自动拆箱。

2.2.2 Manual Unboxing

Integer num1 = new Integer(5);
int num2 = num1.intValue(); // 手动拆箱。

Third, the usual method of packaging

3.1 Type converted into other types of

Syntax: numName.xxxValue ();

method meaning return value
byte value () Into byte type byte
intValue() Converted to an int int
long value () Into a long type long
floatValue () Converted to a float float
doubleValue() Converted to double type double
toString() Into a String String
parseInt(String s) Strings to int static int
valueOf(String s) String into an Integer static Integer
Integer num1 = new Integer("5");
// 或者Integer num1 = 5;
// 或者 Integer num1 = new Integer(5);
String str = num1.toString();

3.2 Basic Data type conversion between a character string

3.2.1 into a string

A grammar: numName.toString ();

Integer num1 = new Integer("5");
String str = num1.toString();

Syntax two: String.valueOf (numName);

Integer num1 = new Integer("5");
String str = String.valueOf(num1);

Syntax three: numName + "";

Integer num1 = new Integer("5");
String str = num1 + "";

3.2.2 string into basic types

A grammar: Xxx.parseXxx (strName);
Note: encountered non-numeric stop.

String str1 = new String("123");
int num1 = Integer.parseInt(str1); // 结果:num1 = 123;
String str2 = new String("123a2");
int num2 = Integer.parseInt(str2); // 结果:num2 = 123;
String str3 = new String("123.4a2");
int num3 = Integer.parseInt(str3); // 结果:num3 = 123.4;

Syntax two: Xxx.valueOf (strName);

String str = new String("123");
int num = Integer.valueOf(str);

Guess you like

Origin blog.csdn.net/lizengbao/article/details/86676462