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

前端 未结 26 1518
生来不讨喜
生来不讨喜 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:36

    (Java Version) In Simple words int is primitive and Integer is wrapper object for int.

    One example where to use Integer vs int, When you want to compare and int variable again null it will throw error.

    int a;
    //assuming a value you are getting from data base which is null
    if(a ==null) // this is wrong - cannot compare primitive to null
    {
    do something...}
    
    Instead you will use,
    Integer a;
    //assuming a value you are getting from data base which is null
    if(a ==null) // this is correct/legal
    { do something...}
    

提交回复
热议问题