• 基本数据类型之自动装箱自动拆箱


    1、基本型和基本型封装型进行“==”运算符的比较,基本型封装型将会自动拆箱变为基本型后再进行比较,因此Integer(0)会自动拆箱为int类型再进行比较,显然返回true;

             int a = 220;

             Integer b = 220;

            System.out.println(a==b);//true
    2、两个Integer类型进行“==”比较, 如果其值在-128至127  ,那么返回true,否则返回false, 这跟Integer.valueOf()的缓冲对象有关,这里不进行赘述。

            Integer c=3;
            Integer h=3;
            Integer e=321;
            Integer f=321;
            System.out.println(c==h);//true
            System.out.println(e==f);//false

    3、两个基本型的封装型进行equals()比较,首先equals()会比较类型,如果类型相同,则继续比较值,如果值也相同,返回true。

            Integer a=1;
            Integer b=2;
            Integer c=3;
            System.out.println(c.equals(a+b));//true

    4、基本型封装类型调用equals(),但是参数是基本类型,这时候,先会进行自动装箱,基本型转换为其封装类型,再进行3中的比较。 

            int i=1;
            int j = 2;
            Integer c=3;
            System.out.println(c.equals(i+j));//true
     5、封装类型不同,不能使用==,例如:
    Integer i = 42;
    Long l = 42l;
    Double d = 42.0;
    System.out.println(i==l);//Operator '==' cannot be applied to 'java.lang.Integer', 'java.lang.Long'

    6、不同封装类型之间equals肯定是false,因为是先比较类型,再比较值,例如“

    Integer i = 42;
    Long l = 42L;
    Double d = 42.0;
    System.out.println(l.equals(42d));//false
    System.out.println(l.equals(i));//false
    System.out.println(d.equals(i));//false

     7、基本数据类型,也称原始数据类型。byte,short,char,int,long,float,double,boolean ,他们之间的比较,应用双等号(==),比较的是他们的值。例如:

    public static void main(String[] args) {
            Integer i = 42;
            Long l = 42L;
            Double d = 42.0;
            System.out.println(42==42l);//true
            System.out.println(42l==42d);//true
            System.out.println(42l==42.0);//true
            System.out.println(42==42.0);//true
        }

    总结:

     
     
     
     
    坚持每天进步,自律改变自己
  • 相关阅读:
    如何 Laravel 中验证 zip 压缩包里的文件?
    PHP7的Yaconf使用教程
    算法与数据结构系列 ( 三 )
    推荐10个优质的Laravel扩展
    如何在利用 Composer 注册全局辅助函数?
    ThinkPHP6新增‘’多应用‘’与ThinkPHP5有啥区别
    基于Laravel开发的在线点播系统MeEdu
    浅述PHP7底层设计01-PHP7语言执行原理
    laravel单文件、多文件上传的实现方法
    在Mac开发环境Laravel Valet中配置运行Flarum论坛系统的方法详解
  • 原文地址:https://www.cnblogs.com/seakyfly/p/13623952.html
Copyright © 2020-2023  润新知