1

Why does Java have these Integer, Character types and classes while they are also adapting C's int, char etc Sometimes when people are creating arrays, they tend to use Integer[] i = {......} rather than int[] i = {....}; what is the difference then?

krato
  • 1,226
  • 4
  • 14
  • 30

4 Answers4

0

In java,

  • Integer is a wrapper class i.e. it is an object while int is a primitive.
  • Integer default value is null while for int it is 0
  • There is a concept of autoboxing and auto-unboxing for these two types.
  • An integer can be converted to an int and vice versa

Followign program demonstrates this

public class TestClass {

    int num;
    Integer number;

    public static void main(String[] args) {

        TestClass testClass = new TestClass();
        System.out.println(testClass.num);
        System.out.println(testClass.number);
        testClass.autoBoxInt(testClass.num);

    }

    public void autoBoxInt(Integer number){
        System.out.println(number);     
    }
}

The output is

0
null
0

The statement System.out.println(testCkass,num) prints int default value i.e. 0. System.out.println(testClass.number) prints Integer default value i.e. null. When you pass testClass.num to a method with parameter Integer, int is automatically converted into and Integer object. so the method prints out 0.

The java collections framework uses autoboxing to convert primitives into Wrapper classes because they cannot take primitive values. They help fast retrieval and storing of objects into collections using hashing and hashcodes. Following example demonstrates this

Set<Integer> numbers = new HashSet<Integer>();
numbers.add(new Integer(10));
numbers.add(new Integer(4));
numbers.add(6);
numbers.add(-9);
numbers.add(new Integer(65));
System.out.println(numbers);

This prints out the set

[4, 65, 6, -9, 10]

To know what hashing is and how hashcodes are used, you can look these links http://www.thejavageek.com/2013/06/27/what-are-hashcodes/ http://www.thejavageek.com/2013/06/26/what-is-the-significance-of-equals-method-in-java/

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • Your example should also include how autoboxing is used to put primitives into generic classes such as collections and then retrieve them quickly. – nanofarad Jul 08 '13 at 12:04
0

Integer is an object, while int is a primitive. Whenever we pass an int into a function, we pass it as-is.

Integer wraps an int. In its case it is immutable so editing it via reference isn't going to work, but it can be put into generics. This can be set to null while int does not have a possibility of anything beyond 0 or a special value you interpret as a null condition, such as -1 or Integer.MAX_VALUE.

For instance, ArrayList<int> is completely invalid while ArrayList<Integer> must be used with ints being wrapped.

With autoboxing, however, we can immediately add an int to an ArrayList without manually wrapping it, and if we need a primitive when we get() the entry, it'll automatically unwrap it transparently.

In the end if you're doing calculations with a limited number of distinct variables or a fixed array, you should generally use int. When dealing with sets, lists, or maps, you should declare the collection as FooCollection<Integer> then add( an int directly allowing for autoboxing.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

Integer, Character and others like this are Objects while int, char and others like this are primitives.

The biggest difference is that an Object can be null while a primitive value can't.

It's recommended to use primitive values where you can because they use less memory.

JREN
  • 3,572
  • 3
  • 27
  • 45
0

The only difference we can tell generally is Wrapper is objective representation of primitive.

Wrapper classes are used to represent primitive values when an Object is required.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307