Why do people still use primitive types in Java?

后端 未结 21 2176
暗喜
暗喜 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:12

    Primitive types have many advantages:

    • Simpler code to write
    • Performance is better since you are not instantiating an object for the variable
    • Since they do not represent a reference to an object there is no need to check for nulls
    • Use primitive types unless you need to take advantage of the boxing features.
    0 讨论(0)
  • 2020-11-22 15:15

    Besides performance and memory issues, I'd like to come up with another issue: The List interface would be broken without int.
    The problem is the overloaded remove() method (remove(int) vs. remove(Object)). remove(Integer) would always resolve to calling the latter, so you could not remove an element by index.

    On the other hand, there is a pitfall when trying to add and remove an int:

    final int i = 42;
    final List<Integer> list = new ArrayList<Integer>();
    list.add(i); // add(Object)
    list.remove(i); // remove(int) - Ouch!
    
    0 讨论(0)
  • 2020-11-22 15:15

    To be brief: primitive types are faster and require less memory than boxed ones

    0 讨论(0)
  • 2020-11-22 15:16
    int loops = 100000000;
    
    long start = System.currentTimeMillis();
    for (Long l = new Long(0); l<loops;l++) {
        //System.out.println("Long: "+l);
    }
    System.out.println("Milliseconds taken to loop '"+loops+"' times around Long: "+ (System.currentTimeMillis()- start));
    
    start = System.currentTimeMillis();
    for (long l = 0; l<loops;l++) {
        //System.out.println("long: "+l);
    }
    System.out.println("Milliseconds taken to loop '"+loops+"' times around long: "+ (System.currentTimeMillis()- start));
    

    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.

    0 讨论(0)
  • 2020-11-22 15:17
    1. You need primitives for doing mathematical operations
    2. Primitives takes less memory as answered above and better performing

    You should ask why Class/Object type is required

    Reason for having Object type is to make our life easier when we deal with Collections. Primitives cannot be added directly to List/Map rather you need to write a wrapper class. Readymade Integer kind of Classes helps you here plus it has many utility methods like Integer.pareseInt(str)

    0 讨论(0)
  • 2020-11-22 15:18

    Autounboxing can lead to hard to spot NPEs

    Integer in = null;
    ...
    ...
    int i = in; // NPE at runtime
    

    In most situations the null assignment to in is a lot less obvious than above.

    0 讨论(0)
提交回复
热议问题