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

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

    In both languages (Java and C#) int is 4-byte signed integer.

    Unlike Java, C# Provides both signed and unsigned integer values. As Java and C# are object object-oriented, some operations in these languages do not map directly onto instructions provided by the run time and so needs to be defined as part of an object of some type.

    C# provides System.Int32 which is a value type using a part of memory that belongs to the reference type on the heap.

    java provides java.lang.Integer which is a reference type operating on int. The methods in Integer can't be compiled directly to run time instructions.So we box an int value to convert it into an instance of Integer and use the methods which expects instance of some type (like toString(), parseInt(), valueOf() etc).

    In C# variable int refers to System.Int32.Any 4-byte value in memory can be interpreted as a primitive int, that can be manipulated by instance of System.Int32.So int is an alias for System.Int32.When using integer-related methods like int.Parse(), int.ToString() etc. Integer is compiled into the FCL System.Int32 struct calling the respective methods like Int32.Parse(), Int32.ToString().

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

    I'll add to the excellent answers given above, and talk about boxing and unboxing, and how this applies to Java (although C# has it too). I'll use just Java terminology because I am more au fait with that.

    As the answers mentioned, int is just a number (called the unboxed type), whereas Integer is an object (which contains the number, hence a boxed type). In Java terms, that means (apart from not being able to call methods on int), you cannot store int or other non-object types in collections (List, Map, etc.). In order to store them, you must first box them up in its corresponding boxed type.

    Java 5 onwards have something called auto-boxing and auto-unboxing which allow the boxing/unboxing to be done behind the scenes. Compare and contrast: Java 5 version:

    Deque<Integer> queue;
    
    void add(int n) {
        queue.add(n);
    }
    
    int remove() {
        return queue.remove();
    }
    

    Java 1.4 or earlier (no generics either):

    Deque queue;
    
    void add(int n) {
        queue.add(Integer.valueOf(n));
    }
    
    int remove() {
        return ((Integer) queue.remove()).intValue();
    }
    

    It must be noted that despite the brevity in the Java 5 version, both versions generate identical bytecode. Thus, although auto-boxing and auto-unboxing are very convenient because you write less code, these operations do happens behind the scenes, with the same runtime costs, so you still have to be aware of their existence.

    Hope this helps!

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

    "int" is primitive data-type and "Integer" in Wrapper Class in Java. "Integer" can be used as an argument to a method which requires an object, where as "int" can be used as an argument to a method which requires an integer value, that can be used for arithmetic expression.

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

    int is a primitive datatype whereas Integer is an object. Creating an object with Integer will give you access to all the methods that are available in the Integer class. But, if you create a primitive data type with int, you will not be able to use those inbuild methods and you have to define them by yourself. But, if you don't want any other methods and want to make the program more memory efficient, you can go with primitive datatype because creating an object will increase the memory consumption.

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

    One more thing that I don't see in previous answers: In Java the primitive wrappers classes like Integer, Double, Float, Boolean... and String are suposed to be invariant, so that when you pass an instance of those classes the invoked method couldn't alter your data in any way, in opositión with most of other classes, which internal data could be altered by its public methods. So that this classes only has 'getter' methods, no 'setters', besides the constructor.

    In a java program String literals are stored in a separate portion of heap memory, only a instance for literal, to save memory reusing those instances

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

    An int variable holds a 32 bit signed integer value. An Integer (with capital I) holds a reference to an object of (class) type Integer, or to null.

    Java automatically casts between the two; from Integer to int whenever the Integer object occurs as an argument to an int operator or is assigned to an int variable, or an int value is assigned to an Integer variable. This casting is called boxing/unboxing.

    If an Integer variable referencing null is unboxed, explicitly or implicitly, a NullPointerException is thrown.

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