一、包装类常见属性和方法:
1)包装类(如:Integer,Double等)这些类封装了一个相应的基本数据类型数值,并为其提供了一系列操作。
2)以下以java.lang.Integer为例,构造方法:
Integer(int value)
Integer(String s)
3)属性
public static final int MAX_VALUE //最大的int型数
public static final int MIN_VALUE //最小的int型数
4)方法
public int intValue() //返回封装数据的int型值
public long longValue() //返回封装数据的long型值
public double doubleValue() //返回封装数据的double型值
public static int parseInt(String s)
throws NumberFormatException
//将字符串解析成int型数据,并返回该数据。
public static Interger valueOf(String s)
throws NumberFormatException
//返回Integer对象,其中封装的整型数据为字符串s所示。
综合举例:
package com.hpe.Integer;
public class TestInterger {
public static void main(String[] args) {
Integer i = new Integer(100);//100这值在堆上
Double d = new Double("123.456");//将字符串转换成double类型,再放到Double对象中
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");
}
}
运行结果:
223
223.456
12.5663704
数据格式不对!
1111011B
7bH