Java基本类型用法总结

1、装箱操作和整形池

int i = 1;
Integer j = i;//装箱操作相当于
j = Integer.valueOf(i);
装箱操作可使用整形池,不必实例化
public static Integer valueOf(int i) {
    return  i >= 128 || i < -128 ? new Integer(i) : SMALL_VALUES[i + 128];
}
/**
 * A cache of instances used by {@link Integer#valueOf(int)} and auto-boxing
 */
private static final Integer[] SMALL_VALUES = new Integer[256];
static {
    for (int i = -128; i < 128; i++) {
        SMALL_VALUES[i + 128] = new Integer(i);
    }
}
2、Integer比较

public void compare(Integer i, Integer j){
    Log.d(TAG, "equal: "+(i==j));//拿不到正确结果。基本类型比较值,对象比较地址。
    Log.d(TAG, "equal: "+(i.intValue()==j.intValue()));
    Log.d(TAG, "greater than: "+(i>j));
    Log.d(TAG, "greater than: "+(i.compareTo(j)));
}

3、拆箱操作

Integer k = new Integer(3);
int l = k;//自动拆箱(AutoUnboxing)操作调用k.intValue()
List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(null);
for (int i:list){
    //小心自动拆箱空指针
}
4、注意三元运算符结果类型

long i = 100L;
String result = String.valueOf(i>200L?10.0f:11L);//float占4byte,long占8byte,long却要转化为float
String.valueOf(10.0f + 11L);//float
String.valueOf(i>200L?(10.0f + 11L):12.0);//double
Life life = i>200L?new Person():new Animal();//public superclass

类定义

public static class Life{
    protected String sex;
}

public static class Person extends Life{
    private String name;

    @Override
    public String toString() {
        return "Person: My name is "+this.name;
    }
}

public static class Animal extends Life{
    private String name;

    @Override
    public String toString() {
        return "Animal: My name is "+this.name;
    }
}

5、注意无限循环小数

Log.d(TAG, "10.0-9.6="+(10.0-9.6));//二进制的0.4是无限循环小数,截断小数位导致不准确
Log.d(TAG, "10.0-9.6="+(10.0*100-9.6*100)/100);
NumberFormat numberFormat = new DecimalFormat("#.##");//仅保留两位小数
Log.d(TAG, "format: "+numberFormat.format(10.0-9.6));
BigDecimal bigDecimal = new BigDecimal(100.111);
6、临界测试

long distance = 30*10000*1000 * 60 * 8;//先按int运算(int放不下导致错误结果),结果强转long
long distanceNew = 1L * 30*10000*1000 * 60 * 8;//先转换扩容,再运算
7、四舍五入
Log.d(TAG, "Math.round(10.5): "+Math.round(10.5));//返回long,相当于Math.floor(10.5+0.5d)再强转
Log.d(TAG, "Math.floor(10.52): "+Math.floor(10.52));
Log.d(TAG, "Math.ceil(10.56): "+Math.ceil(10.56));
Log.d(TAG, "Math.scalb(2.5, 2): "+Math.scalb(2.5, 2));//2.5*2^2

BigDecimal principal = new BigDecimal(8888.0);
BigDecimal interestRate = new BigDecimal(0.00123*3);//季利息
BigDecimal interest = principal.multiply(interestRate).setScale(2, RoundingMode.HALF_EVEN);
Log.d(TAG, "money: "+(principal.doubleValue()+interest.doubleValue()));
RoundingMode roundingMode = RoundingMode.CEILING;
switch (roundingMode){
    case UP://positive values are rounded towards positive infinity, negative values are rounded towards negative infinity
        break;
    case DOWN://rounded towards zero
        int dd = (int) 1.5;//强转直接舍弃小数位
        break;
    case CEILING://进
        break;
    case FLOOR://舍
        break;
    case HALF_UP:
        break;
    case HALF_DOWN:
        break;
    case HALF_EVEN:
        break;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值