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

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

    have you ever programmed before then (int) is one of the primitive types you can set for your variables (just like char, float, ...).

    but Integer is a wrapper class that you can use it to do some functions on an int variable (e.g convert it to string or vise versa,...) , but keep note that methods in the wrapper classes are static so you can use them anytime without creating an instance of Integer class. as a recap :

    int x;
    Integer y; 
    

    x and y are both variables of type int but y is wrapped by an Integer class and has several methods that you use,but i case you need to call some functions of Integer wrapper class you can do it simply.

    Integer.toString(x);
    

    but be aware that both x and y are corect but if you want to use them just as a primitive type, use the simple form (used for defining x).

提交回复
热议问题