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

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

    In Java there are two basic types in the JVM. 1) Primitive types and 2) Reference Types. int is a primitive type and Integer is a class type (which is kind of reference type).

    Primitive values do not share state with other primitive values. A variable whose type is a primitive type always holds a primitive value of that type.

    int aNumber = 4;
    int anotherNum = aNumber;
    aNumber += 6;
    System.out.println(anotherNum); // Prints 4
    

    An object is a dynamically created class instance or an array. The reference values (often just references) are pointers to these objects and a special null reference, which refers to no object. There may be many references to the same object.

    Integer aNumber = Integer.valueOf(4);
    Integer anotherNumber = aNumber; // anotherNumber references the 
                                     // same object as aNumber
    

    Also in Java everything is passed by value. With objects the value that is passed is the reference to the object. So another difference between int and Integer in java is how they are passed in method calls. For example in

    public int add(int a, int b) {
        return a + b;
    }
    final int two = 2;
    int sum = add(1, two);
    

    The variable two is passed as the primitive integer type 2. Whereas in

    public int add(Integer a, Integer b) {
        return a.intValue() + b.intValue();
    }
    final Integer two = Integer.valueOf(2);
    int sum = add(Integer.valueOf(1), two);
    

    The variable two is passed as a reference to an object that holds the integer value 2.


    @WolfmanDragon: Pass by reference would work like so:

    public void increment(int x) {
      x = x + 1;
    }
    int a = 1;
    increment(a);
    // a is now 2
    

    When increment is called it passes a reference (pointer) to variable a. And the increment function directly modifies variable a.

    And for object types it would work as follows:

    public void increment(Integer x) {
      x = Integer.valueOf(x.intValue() + 1);
    }
    Integer a = Integer.valueOf(1);
    increment(a);
    // a is now 2
    

    Do you see the difference now?

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

    An int and Integer in Java and C# are two different terms used to represent different things. It is one of the the primitive data types that can be assigned to a variable that can store exactly. One value of its declared type at a time.

    For example:

    int number = 7;
    

    Where int is the datatype assigned to the variable number which holds the value seven. So an int is just a primitive not an object.

    While an Integer is a wrapper class for a primitive data type which has static methods. That 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.

    For example:

    Integer number = new Integer(5);
    
    0 讨论(0)
  • 2020-11-22 12:20

    Regarding Java 1.5 and autoboxing there is an important "quirk" that comes to play when comparing Integer objects.

    In Java, Integer objects with the values -128 to 127 are immutable (that is, for one particular integer value, say 23, all Integer objects instantiated through your program with the value 23 points to the exact same object).

    Example, this returns true:

    Integer i1 = new Integer(127);
    Integer i2 = new Integer(127);
    System.out.println(i1 == i2); //  true
    

    While this returns false:

    Integer i1 = new Integer(128);
    Integer i2 = new Integer(128);
    System.out.println(i1 == i2); //  false
    

    The == compares by reference (does the variables point to the same object).

    This result may or may not differ depending on what JVM you are using. The specification autoboxing for Java 1.5 requires that integers (-128 to 127) always box to the same wrapper object.

    A solution? =) One should always use the Integer.equals() method when comparing Integer objects.

    System.out.println(i1.equals(i2)); //  true
    

    More info at java.net Example at bexhuff.com

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

    Java:

    int, double, long, byte, float, double, short, boolean, char - primitives. Used for hold the basic data types supported by the language. the primitive types are not part of the object hierarchy, and they do not inherit Object. Thet can'be pass by reference to a method.

    Double, Float, Long, Integer, Short, Byte, Character, and Boolean, are type Wrappers, packaged in java.lang. All of the numeric type wrappers define constructors that allow an object to be constructed from a given value, or a string representation of that value. Using objects can add an overhead to even the simplest of calculations.

    Beginning with JDK 5, Java has included two very helpful features: autoboxing and autounboxing. Autoboxing/unboxing greatly simplifies and streamlines code that must convert primitive types into objects, and vice versa.

    Example of constructors:

    Integer(int num)
    Integer(String str) throws NumberFormatException
    Double(double num)
    Double(String str) throws NumberFormatException
    

    Example of boxing/unboxing:

    class ManualBoxing {
            public static void main(String args[]) {
            Integer objInt = new Integer(20);  // Manually box the value 20.
            int i = objInt.intValue();  // Manually unbox the value 20
            System.out.println(i + " " + iOb); // displays 20 20
        }
    }
    

    Example of autoboxing/autounboxing:

    class AutoBoxing {
        public static void main(String args[]) {
            Integer objInt = 40; // autobox an int
            int i = objInt ; // auto-unbox
            System.out.println(i + " " + iOb); // displays 40 40
        }
    }
    

    P.S. Herbert Schildt's book was taken as a reference.

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

    In Java int is a primitive data type while Integer is a Helper class, it is use to convert for one data type to other.

    For example:

    double doubleValue = 156.5d;
    Double doubleObject  = new Double(doubleValue);
    Byte myByteValue = doubleObject.byteValue ();
    String myStringValue = doubleObject.toString();
    

    Primitive data types are store the fastest available memory where the Helper class is complex and store in heep memory.

    reference from "David Gassner" Java Essential Training.

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

    In platforms like Java, ints are primitives while Integer is an object which holds a integer field. The important distinction is that primitives are always passed around by value and by definition are immutable.

    Any operation involving a primitive variable always returns a new value. On the other hand, objects are passed around by reference. One could argue that the point to the object (AKA the reference) is also being passed around by value, but the contents are not.

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