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

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

    In Java, the 'int' type is a primitive, whereas the 'Integer' type is an object.

    In C#, the 'int' type is the same as System.Int32 and is a value type (ie more like the java 'int'). An integer (just like any other value types) can be boxed ("wrapped") into an object.


    The differences between objects and primitives are somewhat beyond the scope of this question, but to summarize:

    Objects provide facilities for polymorphism, are passed by reference (or more accurately have references passed by value), and are allocated from the heap. Conversely, primitives are immutable types that are passed by value and are often allocated from the stack.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 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...}
    
    0 讨论(0)
  • 2020-11-22 12:37

    Well, in Java an int is a primitive while an Integer is an Object. Meaning, if you made a new Integer:

    Integer i = new Integer(6);
    

    You could call some method on i:

    String s = i.toString();//sets s the string representation of i
    

    Whereas with an int:

    int i = 6;
    

    You cannot call any methods on it, because it is simply a primitive. So:

    String s = i.toString();//will not work!!!
    

    would produce an error, because int is not an object.

    int is one of the few primitives in Java (along with char and some others). I'm not 100% sure, but I'm thinking that the Integer object more or less just has an int property and a whole bunch of methods to interact with that property (like the toString() method for example). So Integer is a fancy way to work with an int (Just as perhaps String is a fancy way to work with a group of chars).

    I know that Java isn't C, but since I've never programmed in C this is the closest I could come to the answer. Hope this helps!

    Integer object javadoc

    Integer Ojbect vs. int primitive comparison

    0 讨论(0)
  • 2020-11-22 12:37

    I'll just post here since some of the other posts are slightly inaccurate in relation to C#.

    Correct: int is an alias for System.Int32.
    Wrong: float is not an alias for System.Float, but for System.Single

    Basically, int is a reserved keyword in the C# programming language, and is an alias for the System.Int32 value type.

    float and Float is not the same however, as the right system type for ''float'' is System.Single. There are some types like this that has reserved keywords that doesn't seem to match the type names directly.

    In C# there is no difference between ''int'' and ''System.Int32'', or any of the other pairs or keywords/system types, except for when defining enums. With enums you can specify the storage size to use and in this case you can only use the reserved keyword, and not the system runtime type name.

    Wether the value in the int will be stored on the stack, in memory, or as a referenced heap object depends on the context and how you use it.

    This declaration in a method:

    int i;
    

    defines a variable i of type System.Int32, living in a register or on the stack, depending on optimizations. The same declaration in a type (struct or class) defines a member field. The same declaration in a method argument list defines a parameter, with the same storage options as for a local variable. (note that this paragraph is not valid if you start pulling iterator methods into the mix, these are different beasts altogether)

    To get a heap object, you can use boxing:

    object o = i;
    

    this will create a boxed copy of the contents of i on the heap. In IL you can access methods on the heap object directly, but in C# you need to cast it back to an int, which will create another copy. Thus, the object on the heap cannot easily be changed in C# without creating a new boxed copy of a new int value. (Ugh, this paragraph doesn't read all that easily.)

    0 讨论(0)
  • 2020-11-22 12:38

    01. Integer can be null. But int cannot be null.

    Integer value1 = null; //OK
    
    int value2 = null      //Error
    

    02. Only can pass Wrapper Classes type values to any collection class.

    (Wrapper Classes - Boolean,Character,Byte,Short,Integer,Long,Float,Double)

    List<Integer> element = new ArrayList<>();
    int valueInt = 10;
    Integer  valueInteger = new Integer(value);
    element.add(valueInteger);
    

    But normally we add primitive values to collection class? Is point 02 correct?

    List<Integer> element = new ArrayList<>();
    element.add(5);
    

    Yes 02 is correct, beacouse autoboxing.

    Autoboxing is the automatic conversion that the java compiler makes between the primitive type and their corresponding wrapper class.

    Then 5 convert as Integer value by autoboxing.

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