[Java] if(a==1 && a==2 && a==3),为true,你敢信???

看了一个帖子,我觉得滑天下之大稽,离天下之大谱。然后我试了一下,结果。。。

 

  正常情况下,上述这种操作肯定结果为false,不过帖子里边提供了一种办法可以在不改变这段代码的前提下迫使他为true。那就是改变 Integer 下的缓存数组,把需要验证成立的数值全改成变量a的值。

 获取Integer下的缓存数组

    /**
     获取 Integer.IntegerCache 下的cache数组
     @return        长度为256,值区间-128~127
     */
    public static Integer[] getIntegerCacheArrays() {
        Class cache = Integer.class.getDeclaredClasses()[0];
        Integer[] array = null;
        try {
            //获取Integer.IntegerCache下的cache数组。长度为256,值区间-128~127
            Field c = cache.getDeclaredField("cache");
            c.setAccessible( true );
            array = (Integer[]) c.get(cache);
        }catch(NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return array;
    }

cache的取值范围在 -128 ~ 127,数组长度为:256

cache的全部值,其中数值1~4在下标 129~132之间

 

 调用

在保证代码不变的情况下,我们在他之前把缓存的值修改掉。

    public static void main(String[] args) {
        //长度为256,值区间-128~127
        Integer[] array = getIntegerCacheArrays();
        //[129:1], [130:2], [131:3], [132:4]
        array[ 130 ] = array[ 129 ];
        array[ 131 ] = array[ 129 ];
        array[ 132 ] = array[ 129 ];

        Integer a = 1;
        System.out.println( a == (Integer)1 && a == (Integer)2 && a == (Integer)3 && a == (Integer)4 );
    }

    //运行结果:true

额外

到现在为止,我好像懂了,又好像没完全懂。接着我修改了下代码,尝试看看其他值的情况。

 我把a变量改为4,对应的下标为132,为了偷懒,我稍微改了下,但不影响最终效果

public static void main(String[] args) {
        //长度为256,值区间-128~127
        Integer[] array = getIntegerCacheArrays();
        //[129:1], [130:2], [131:3], [132:4]
        //篡改值的下标
        int tamperIndex = 132;

        //等同于 130
        array[ tamperIndex + 1 ] = array[ tamperIndex ];
        //等同于 131
        array[ tamperIndex + 2 ] = array[ tamperIndex ];
        //等同于 132
        array[ tamperIndex + 3 ] = array[ tamperIndex ];
        
        Integer a = 4;

        System.out.println( a == (Integer)1 && a == (Integer)2 && a == (Integer)3 && a == (Integer)4 );

        //(Integer)a 等同于 (Integer)1
        boolean b1 = a == (Integer)a;
        System.out.println( "b1 -> " + a );

        //(Integer)( a + 1 ) 等同于 (Integer)2
        boolean b2 = a == (Integer)( a + 1 );
        System.out.println( "b2 -> " + a );

        //(Integer)( a + 2 ) 等同于 (Integer)3
        boolean b3 = a == (Integer)( a + 2 );
        System.out.println( "b3 -> " + a );

        //(Integer)( a + 3 ) 等同于 (Integer)4
        boolean b4 = a == (Integer)( a + 3 );
        System.out.println( "b4 -> " + a );

        System.out.println( b1 && b2 && b3 && b4 );
    }
    //运行结果:
    //    true
    //    b1 -> 4
    //    b2 -> 4
    //    b3 -> 4
    //    b4 -> 4
    //    true

到了这里,我只能理解为Integer是从cache中取值,而不是我们填写的值。

这让我想起了 100和1000的问题

    public static void main(String[] args) {
        Integer i_100 = 100;
        Integer i2_100 = 100;

        Integer i_1000 = 1000;
        Integer i2_1000 = 1000;
        System.out.println( "100 => " + ( i_100 == i2_100 ) );
        System.out.println( "1000 => " + ( i_1000 == i2_1000 ) );
    }
    //运行结果
    //100 => true
    //1000 => false

引起结果不一致的问题在于只要取值范围在-128~127之内就没问题,超出就会不成立。具体原因其他帖子有详细描述,这里不做开展。

我在想如果在实际开发中,验证会员状态的时候加入这段代码岂不是...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
String a="hello world"; //在java中有一个常量池,当创建String 类型的引用变量给它赋值时,java会到它的常量池中找"hello world"是不是在常量池中已存在。如果已经存在则返回这个常量池中的"hello world"的地址(在java中叫引用)给变量a 。注意a并不是一个对象,而是一个引用类型的变量。它里面存的实际上是一个地址值,而这个值是指向一个字符串对象的。在程序中凡是以"hello world"这种常量似的形式给出的都被放在常量池中。 String b=new String("hello world"); //这种用new关键字定义的字符串,是在堆中分配空间的。而分配空间就是由new去完成的,由new去决定分配多大空间,并对空间初始化为字符串"hello world" 返回其在堆上的地址。 通过上面的原理,可以做如下实验: String a="hello world"; String b="hello world"; String c=new String("hello world"); String d=new String("hello world"); if(a==b) System.out.println("a==b"); else System.out.println("a!=b"); if(c==d) System.out.println("c==d"); else System.out.println("c!=d"); //输出结果: a==b c!=d 为什么会出现上面的情况呢? String a="hello world"; String b="hello world"; 通过上面的讲解可以知道,a和b都是指向常量池的同一个常量字符串"hello world"的,因此它们返回的地址是相同的。a和b都是引用类型,相当于c语言里面的指针。java里面没有指针的概念,但是实际上引用变量里面放的确实是地址值,只是java为了安全不允许我们对想c语言中的那样对指针进行操作(如++ 、--)等。这样就有效的防止了指针在内存中的游离。 而对于 String c=new String("hello world"); String d=new String("hello world"); 来说是不相等的,他们是有new在堆中开辟了两块内存空间,返回的地址当然是不相等的了。如果我们要比较这两个字符串的内容怎么办呢?可以用下面的语句: if(c.equals(d)) System.out.println("c==d"); else System.out.println("c!=d"); //输出 c==d

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米歪(MiWi)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值