Weird Integer boxing in Java

前端 未结 12 1954
广开言路
广开言路 2020-11-22 00:15

I just saw code similar to this:

public class Scratch
{
    public static void main(String[] args)
    {
        Integer a = 1000, b = 1000;
        System.o         


        
12条回答
  •  迷失自我
    2020-11-22 00:59

    In Java the boxing works in the range between -128 and 127 for an Integer. When you are using numbers in this range you can compare it with the == operator. For Integer objects outside the range you have to use equals.

提交回复
热议问题