目录

Java不同数字对比不同笔记

介绍

Integer 不同大小和不同创建方式equal和等于后产生了不同的结果

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class ObjectEqueal {
    public static void main(String[] args) {
        Integer i1 = 100, i2 = 100, i3 = 150, i4 = 150;//-128,127为界限
        Integer i5 = new Integer(100);
        Integer i6 = new Integer(100);
        Integer i7 = new Integer("100");
        Integer i8 = Integer.parseInt("100");
        System.out.println(i1 == i2);//true
        System.out.println(i3 == i4);//false
        System.out.println(i1==i5);//false
        System.out.println(i6==i5);//false
        System.out.println(i1==i7);//false
        System.out.println(i8==i7);//false
        System.out.println(i1==i8);//true
        System.out.println(i5.equals(i6));//true
        System.out.println(i3.equals(i4));//true

        Double d1 = 100.0, d2 = 100.0, d3 = 150.0, d4 = 150.0;//-128,127为界限
        Double d5 = new Double(100);
        Double d6 = new Double(100);
        Double d7 = new Double("100");
        Double d8 = Double.parseDouble("100");
        System.out.println(d1 == d2);//false
        System.out.println(d3 == d4);//false
        System.out.println(d1==d5);//false
        System.out.println(d6==d5);//false
        System.out.println(d1==d7);//false
        System.out.println(d8==d7);//false
        System.out.println(d1==d8);//false
        System.out.println(d5.equals(d6));//true
        System.out.println(d3.equals(d4));//true

        Float f1 = 100.0f, f2 = 100.0f, f3 = 150.0f, f4 = 150.0f;//-128,127为界限
        Float f5 = new Float(100f);
        Float f6 = new Float(100f);
        Float f7 = new Float("100f");
        Float f8 = Float.parseFloat("100f");
        System.out.println(f1==f2);//false
        System.out.println(f3==f4);//false
        System.out.println(f1==f5);//false
        System.out.println(f6==f5);//false
        System.out.println(f1==f7);//false
        System.out.println(f8==f7);//false
        System.out.println(f1==f8);//false
        System.out.println(f5.equals(f6));//true
        System.out.println(f3.equals(f4));//true
    }
}

结果

在代码中请自行验证