Why do people still use primitive types in Java?

后端 未结 21 2174
暗喜
暗喜 2020-11-22 15:11

Since Java 5, we\'ve had boxing/unboxing of primitive types so that int is wrapped to be java.lang.Integer, and so and and so forth.

I see

21条回答
  •  有刺的猬
    2020-11-22 15:16

    int loops = 100000000;
    
    long start = System.currentTimeMillis();
    for (Long l = new Long(0); l

    Milliseconds taken to loop '100000000' times around Long: 468

    Milliseconds taken to loop '100000000' times around long: 31

    On a side note, I wouldn't mind seeing something like this find it's way into Java.

    Integer loop1 = new Integer(0);
    for (loop1.lessThan(1000)) {
       ...
    }
    

    Where the for loop automatically increments loop1 from 0 to 1000 or

    Integer loop1 = new Integer(1000);
    for (loop1.greaterThan(0)) {
       ...
    }
    

    Where the for loop automatically decrements loop1 1000 to 0.

提交回复
热议问题