本文为转载文章,原文地址:http://blog.csdn.net/anticlql/article/details/75206174
基础数据类型包装类
这些类封装了一个相应的基本数据类型数值,并为其提供了了一系列操作
以Integer为例
包装类常见方法
public static final int MAX_VALUE 最大的int型数(2³¹-1)
public static final int MIN_VALUE 最小的int型数(-231)
public long longvalue() 返回封装数据的long型值
public double doubleValue() 返回封装数据的double型值
public int intValue() 返回封装数据的int型值
public static int parseInt(String s) throws NumberFormatException 将字符串解析成int型数据,返回该数据
public static Integer valueOf(String s) throws NumberFormatException 返回Integer对象,其中封装的整型数据为字符串s所表示
例:
public class TestPackage {
public static void main(String[] args) {
Integer i = new Integer(100);
Double d = new Double("123.456");
int j = i.intValue() + d.intValue();
float f = i.floatValue() + d.floatValue();
System.out.println(j);
System.out.println(f);
double pi = Double.parseDouble("3.1415926");
double r = Double.valueOf("2.0").doubleValue();
double s = pi * r * r;
System.out.println(s);
try {
int k = Integer.parseInt("1.25");
} catch (NumberFormatException e) {
System.out.println("数据格式不对");
}
System.out.println(Integer.toBinaryString(123) + "B");
System.out.println(Integer.toHexString(123) + "H");
System.out.println(Integer.toOctalString(123) + "O");
}
}
输出结果
223
223.456
12.5663704
数据格式不对
1111011B
7bH
173O