java基本数据类型使用的意料之外

1.意料之外1
short a=1;
a=a+1;//编译错误 1为int型需要进行强转
a+=1;//编译正确  +=操作符会进行隐式自动类型转换

2.Integer中IntegerCache

public class Test {
    /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     * <p>
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     * *************************  IntegerCache
     * 缓存以支持之间值的自动装箱的对象标识语义
     * -JLS要求的128和127(含)。
     * 缓存在第一次使用时初始化。缓存的大小
     * 可以由{@code-XX:AutoBoxCacheMax=<size>}选项控制。
     * 在VM初始化期间,java.lang.Integer.IntegerCache.high属性
     * 可以在中的专用系统属性中设置和保存
     * sun.misc.VM类。
     */
    public static void main(String[] args) {
        Integer a = 127, b = 127, c = 128, d = 128;
        System.out.println(a == b);
        // 输出true
        
        System.out.println(c == d);
        // 输出false
    }
}

3.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值