What is the difference between an int and an Integer in Java and C#?

前端 未结 26 1419
生来不讨喜
生来不讨喜 2020-11-22 12:00

I was reading More Joel on Software when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an i

相关标签:
26条回答
  • 2020-11-22 12:44

    int is used to declare primitive variable

    e.g. int i=10;
    

    Integer is used to create reference variable of class Integer

    Integer a = new Integer();
    
    0 讨论(0)
  • 2020-11-22 12:45

    There are many reasons to use wrapper classes:

    1. We get extra behavior (for instance we can use methods)
    2. We can store null values whereas in primitives we cannot
    3. Collections support storing objects and not primitives.
    0 讨论(0)
提交回复
热议问题