With such data type int python is already in the class, of course, we have established the Java class encapsulates the basic data types
Java and Python but not so radical, fundamental data type in Java is not directly category
But another open stove, Create Create a class that implements one-package
Basic data types | Wrapper Class | Constructor 1 | Constructor 2 | |
int | Integer | Integer(int value) | Integer(String s) | |
byte | Byte | Byte(byte value) | Byte(String s) | |
char | Character | Character(char value) | ||
short | Short | Short(short value) | Short(String s) | |
long | Long | Long(long value) | Long(String s) | |
float | Float | Float(double value) | Float(float value) | Float(String s) |
double | Double | Double(double value) | Double(String s) | |
boolean | Boolean | Boolean(boolean value) | Boolean(String s) |
Boxing and unboxing
Unpacking: object classes reduced to basic data types
Packing: basic data type to the object class corresponding to
Java can be automatic packing and unpacking
Sample code:
public class T2 {
public static void main(String [] args) {
// 装箱
int x = 10;
Integer xx = x;
System.out.println("xx = "+xx);
// 拆箱
Float f1 = new Float(3.14f);
Float f2 = new Float(3.1415926d);
Float f3 = new Float("3.1415926535897");
System.out.println("f1 = "+f1);
System.out.println("f2 = "+f2);
System.out.println("f3 = "+f3);
StringBuffer s = new StringBuffer("111");
String str = s.toString();
System.out.println("str = "+str);
// 封装类也有toString方法,可以将其内容转成字符串
System.out.println("f1 = "+f1.toString());
}
}
With an encapsulation class implements two implementations string rev ideas
With the above boxing and unboxing, it may be utilized readily occur to construct a temporary anonymous data corresponding to the type of packaging objects
Then this anonymous object is assigned a basic data type parsing out
Sample code:
In addition, Java also provides a method of packaging parse series string class to be implemented in the basic data type conversion
Sample code:
public class T2 {
public static void main(String [] args) {
// 装箱
int x = 10;
Integer xx = x;
System.out.println("xx = "+xx);
// 拆箱
Float f1 = new Float(3.14f);
Float f2 = new Float(3.1415926d);
Float f3 = new Float("3.1415926535897");
System.out.println("f1 = "+f1);
System.out.println("f2 = "+f2);
System.out.println("f3 = "+f3);
StringBuffer s = new StringBuffer("111");
String str = s.toString();
System.out.println("str = "+str);
// 封装类也有toString方法,可以将其内容转成字符串
System.out.println("f1 = "+f1.toString());
// 封装类的parse方法,可以一步完成将字符换转化为基本数据类型
String intStr = "2147483647";
String binaryStr = "10101010101";
int maxInt = Integer.parseInt(intStr, 10);
int binaryInt = Integer.parseInt(binaryStr, 2);
System.out.println(maxInt+" "+binaryInt);
}
}
Further methods can be used to generate a binary string:
public class T2 {
public static void main(String [] args) {
// 使用包装类的toBinaryString 转二进制字符串
String transferResult = Integer.toBinaryString(1000);
System.out.println(transferResult);
// 使用包装类的toHexString 转16进制字符串
String transferResult1 = Integer.toHexString(1000);
System.out.println(transferResult1);
// 使用包装类的toOctString 转8进制字符串
String transferResult2 = Integer.toOctalString(1000);
System.out.println(transferResult2);
}
}
operation result: