Java自动装箱测试

Java自动装箱测试

测试代码

import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

@Slf4j
public class AutoBoxMain {
    public static void main(String[] args) {

        {
            int i = 1;
            int i2 = 1;
            long l = 1L;
            float f = 1F;
            double d = 1D;
            log.info("int 1 equals int 1 is {}", Objects.equals(i, i2));
            log.info("int 1 equals long 1 is {}", Objects.equals(i, l));
            log.info("int 1 equals float 1 is {}", Objects.equals(i, f));
            log.info("int 1 equals double 1 is {}", Objects.equals(i, d));
        }

        log.info("");

        {
            int i = 1;
            int i2 = 1;
            long l = 1L;
            float f = 1F;
            double d = 1D;
            log.info("int 1 == int 1 is {}", i == i2);
            log.info("int 1 == long 1 is {}", i == l);
            log.info("int 1 == float 1 is {}", i == f);
            log.info("int 1 == double 1 is {}", i == d);
        }

        log.info("");

        {
            int i = 200;
            int i2 = 200;
            long l = 200L;
            float f = 200F;
            double d = 200D;
            log.info("int 200 equals int 200 is {}", Objects.equals(i, i2));
            log.info("int 200 equals long 200 is {}", Objects.equals(i, l));
            log.info("int 200 equals float 200 is {}", Objects.equals(i, f));
            log.info("int 200 equals double 200 is {}", Objects.equals(i, d));
        }

        log.info("");

        {
            int i = 200;
            int i2 = 200;
            long l = 200L;
            float f = 200F;
            double d = 200D;
            log.info("int 200 == int 200 is {}", i == i2);
            log.info("int 200 == long 200 is {}", i == l);
            log.info("int 200 == float 200 is {}", i == f);
            log.info("int 200 == double 200 is {}", i == d);
        }

        log.info("");

        {
            Integer i = 1;
            Integer i2 = 1;
            Long l = 1L;
            Float f = 1F;
            Double d = 1D;
            log.info("Integer 1 equals Integer 1 is {}", Objects.equals(i, i2));
            log.info("Integer 1 equals Long 1 is {}", Objects.equals(i, l));
            log.info("Integer 1 equals Float 1 is {}", Objects.equals(i, f));
            log.info("Integer 1 equals Double 1 is {}", Objects.equals(i, d));
            log.info("Integer 1 == Integer 1 is {}", i == i2);
        }

        log.info("");

        {
            Integer i = 200;
            Integer i2 = 200;
            Long l = 200L;
            Float f = 200F;
            Double d = 200D;
            log.info("Integer 200 equals Integer 200 is {}", Objects.equals(i, i2));
            log.info("Integer 200 equals Long 200 is {}", Objects.equals(i, l));
            log.info("Integer 200 equals Float 200 is {}", Objects.equals(i, f));
            log.info("Integer 200 equals Double 200 is {}", Objects.equals(i, d));
            log.info("Integer 200 == Integer 200 is {}", i == i2);
        }

    }

}

打印结果

int 1 equals int 1 is true
int 1 equals long 1 is false
int 1 equals float 1 is false
int 1 equals double 1 is false

int 1 == int 1 is true
int 1 == long 1 is true
int 1 == float 1 is true
int 1 == double 1 is true

int 200 equals int 200 is true
int 200 equals long 200 is false
int 200 equals float 200 is false
int 200 equals double 200 is false

int 200 == int 200 is true
int 200 == long 200 is true
int 200 == float 200 is true
int 200 == double 200 is true

Integer 1 equals Integer 1 is true
Integer 1 equals Long 1 is false
Integer 1 equals Float 1 is false
Integer 1 equals Double 1 is false
Integer 1 == Integer 1 is true

Integer 200 equals Integer 200 is true
Integer 200 equals Long 200 is false
Integer 200 equals Float 200 is false
Integer 200 equals Double 200 is false
Integer 200 == Integer 200 is false
posted @ 2020-09-04 10:25  风真天  阅读(124)  评论(0编辑  收藏  举报