zoukankan      html  css  js  c++  java
  • 关于Integer的比较

    package com.wzf.design.other.integer;
    
    import org.apache.commons.lang3.StringUtils;
    
    /**
     * @Author: HERO
     * @Date: 2020/4/12 8:27
     * @Version 1.0
     */
    public class IntegerEqDemo {
    
        public static void main(String[] args) {
            //装箱
            Integer t1 = 100;//其实相当于Integer.valueOf(100);
    
            //拆箱
            int t2 = t1; //其实相当于 t1.intValue();
    
            //难点:通过反编译class文件就可以看到。
            // 1,  Integer的valueOf中判断,若值-128~127,则返回内部类中IntegerCache已经实例的对象(单例),
            //      反之,则重新new Integer()生成新的对象;
            // 2,  Integer和int比较时,自动拆箱。
    
            Integer a1 = 100;
            Integer a2 = 100;
            Integer b1 = 128;
            Integer b2 = 128;
            Integer a3 = new Integer(100);
            Integer a4 = Integer.valueOf(100);
            Integer a5 = new Integer(2000);
            Integer a7 = null;
            int a6 = 2000 ;
    
    
            System.out.println("1、"+ (a1 == a2));//true 自动装箱 调用了valueOf方法,值在-128~127之间,所有指向同一对象;
            System.out.println("2、"+ (b1 == b2));//false 自动装箱 调用了valueOf方法,值不在-128~127之间,通过newInteger()重新实例了,不指向同一对象;
            System.out.println("3、"+ (a1 == a3));//false 因为a3自己重新实例的对象,所以地址指向不一致。
            System.out.println("4、"+ (a1 == a4));//true a1和a4 是一样的,a1默认装箱,调用Integer.valueOf(100),因为值在-128~127之间,所有指向同一对象;
            System.out.println("5、"+ (a5 == a6));//true 两者比较时自动拆箱,a5调用了a5.intValue(),所以两者数值大小相等。
    
            System.out.println("6、"+ (b1.equals(b2)));//true
            System.out.println("7、"+ (b1.intValue() == b2.intValue()));//true
    //        System.out.println("8、"+ (a7 == 2));//异常 调用a7.intValue()时空指针
    
    
            //所以一般涉及到Integer的比较
            //1,保证不为空
            //2,用Intger对象的equals方法比较,这样比较的是值,而不是引用地址
            System.out.println("9、"+ (b1 != null && b1.equals(b2)));
            System.out.println("10、"+ (b1 != null && b1.equals(128)));
    
        }
    
    }
  • 相关阅读:
    httpwebrequest详解【转】
    javascript中event.keycode大全
    public void Delete<T>(List<T> EntityList) where T : class, new() 这是什么意思
    Vue(基础五)_vue中用ref和给dom添加事件的特殊情况
    Vue(基础四)_总结五种父子组件之间的通信方式
    Vue(基础三)_监听器与计算属性
    vue(基础二)_组件,过滤器,具名插槽
    vue(基础一)_基本指令的使用
    JS(基础)_总结获取页面中元素和节点的方式
    node.js(node.js+mongoose小案例)_实现简单的注册登录退出
  • 原文地址:https://www.cnblogs.com/hero123/p/13898447.html
Copyright © 2011-2022 走看看