Integer.class vs int.class

前端 未结 4 1192
悲&欢浪女
悲&欢浪女 2020-12-01 05:15

What is the difference between Integer.class, Integer.TYPE and int.class?

acc to me

  1. Integer.class i
4条回答
  •  盖世英雄少女心
    2020-12-01 05:38

    In plain terms :

    int -- > Are primitives..for simple math operations. You cannot add them to a collection.

    Integer --> Are objects in themselves.. are wrappers to ints. i.e, they can be used with collections (as they are objects). They are collected as normal objects by the GC.

    EDIT :

    public static void main(String[] args) {
        int i = 5;
        System.out.println(int.class);
    
        Integer i1 = new Integer(5);
        System.out.println(Integer.TYPE);
    
    }
    
    O/P : int
          int
    

    So, basically, both return an int. Integer.TYPE just returns the primitive type of the Integer class. It is true for any wrapper class

提交回复
热议问题