java 各种数据类型的互相转换
int -- Integer
int num = 20;
A:Integer i = new Integer(num);
B:Integer i = Integer.valueOf(num);
Integer -- int
Integer i = new Integer(20);
A:int num = i.intValue();
int -- String
int num = 20;
A:String s = String.valueOf(num);
B:String s = ""+num;
C:String s = Integer.toString(num);
String -- int
String s = "20";
A:int num = Integer.parseInt(s);
B:Integer i = new Integer(s);或者Integer i = Integer.valueOf(s);
int num = i.intValue();
String -- StringBuilder
String str = "abcdefghijklmnopqrs";
StringBuilder stb = new StringBuilder(str);
string->byte
Byte static byte parseByte(String s)
byte->string
Byte static String toString(byte b)
char->string
Character static String to String (char c)
string->Short
Short static Short parseShort(String s)
Short->String
Short static String toString(Short s)
String->Integer
Integer static int parseInt(String s)
Integer->String
Integer static String tostring(int i)
String->Long
Long static long parseLong(String s)
Long->String
Long static String toString(Long i)
String->Float
Float static float parseFloat(String s)
Float->String
Float static String toString(float f)
String->Double
Double static double parseDouble(String s)
Double->String
Double static String toString(Double)
基本数据类型的对象包装类
(1)为了更方便的操作每个基本数据类型,java对其提供了很多的属性和方法供我们使用。
(2)用途:
**将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能操作该数据。
**常用的操作之一:用于基本数据类型与字符串之间的转换。
A:方便操作
B:用于和字符串进行相互转换
(3)基本数据类型和对象类型的对应
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
boolean
Boolean
char
Character
字段摘要:
static int MAX_VALUE 值为 2^31-1 的常量,它表示 int 类型能够表示的最大值
static int MIN_VALUE 值为 -2^31 的常量,它表示 int 类型能够表示的最小值
static Class<Integer> TYPE 表示基本类型int的Class 实例
Integer(int value) 构造一个新分配的Integer对象,它表示指定的int值。
Inreger(String s) 注意:s必须是纯数字的字符串。否则会有异常NumberFormatException
常用方法
Integer.toBinaryString();
以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
Integer.toOctalString();
以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。
Integer.toHexString();
以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
static int Integer.parseInt(String s) 将字符串参数作为有符号的十进制整数进行解析,
字符串必须是int型范围内的数字字符串
static int Integer.parseInt(String s,int basic)
使用第二个参数指定的基数,将字符串参数解析为有符号的整数.
字符串必须是int型范围内的数字字符串
short shortValue() 以short类型返回该Integer的值。
int intValue() 以int类型返回该Integer的值。
static Integer valueOf(int num) 返回一个表示指定的 int 值的 Integer 实例。
static Integer valueOf(String s) 返回保存指定的String的值的Integer对象。
static Integer valueOf(String s, int radix)
返回一个Integer对象,该对象中保存了用第二个参数提供的基数进行
解析时从指定的String中提取的值。
还没有评论,来说两句吧...