0

When reading the Hadoop material, always meet some Java concepts that I am not very familiar. With respect to the following one, what does the concept of "wrapper" mean here? When do we need this kind of wrapper, and what's the role it plays in an objected-oriented language?

enter image description here

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
user785099
  • 5,323
  • 10
  • 44
  • 62
  • 1
    Primitive type wrappers are `Integer` for `int`, `Double` for `double`, `Boolean` for `boolean`, etc. One of their most important usages is with generics: you can only use the wrappers and not the exact primitive types. You might also want to look into *autoboxing*. – Jeroen Vannevel Mar 09 '14 at 14:08

4 Answers4

4

Anytime you need to use a reference type (that is, an object - say Integer) as opposed to a primitive type (say int).

This is used, prominently, in generics where you need to specify a class as opposed to a primitive:

HashMap<String, Integer> foo = new HashMap<String, Integer>();

Here, you might think that:

HashMap<String, int> foo = new HashMap<String, int>();

would work, but it won't, as int is not a reference type (a class) but a primitive.

We have wrapper classes for all primitive types:

Integer for int, Byte for byte, Double for double, Character for char, etc.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
1

Wrapper classes provides a way to use the primitive types as objects like for int we have Integer as a wrapper class

Main use of Wrappers is with generics
like you can have an ArrayList<Integer>, but not an ArrayList<int> same with Other Collections etc. To get type safety we use generics and generics need objects not primitives.

and one more thing I want to add is, You use the wrappers when you need types that fit in the object oriented world.

Girish
  • 1,717
  • 1
  • 18
  • 30
1

The answers until now referred solely to the representations of a primitive type via a reference type.

But one of the main justifications for the existence of these writable wrappers in the context of Hadoop (or other contexts) was not mentioned yet:

You can write them!

That is, they allow some sort of "pass by reference", which usually is not possible in Java. Imagine you have a method like

public void computeSomething(int resultA, int resultB)
{
    resultA = 123;
    resultB = 234;
}

// Used as
int a = 0;
int b = 0;
computeSomething(a, b);
System.out.println(a); // Prints 0
System.out.println(b); // Prints 0

Obviously, modifying the arguments in the method has not effect for the outside world. In contrast to that, you can use the writable wrappers to achieve the desired effect:

public void computeSomething(IntWritable resultA, IntWritable resultB)
{
    resultA.set(123);
    resultB.set(234);
}

// Used as
IntWritable a = new IntWritable(0);
IntWritable b = new IntWritable(0);
computeSomething(a, b);
System.out.println(a); // Prints 123
System.out.println(b); // Prints 234
Marco13
  • 53,703
  • 9
  • 80
  • 159
0

A wrapper class is a class that contains another class or primitive type. The wrapper class is specifically made so that it provides additional functionality to the wrapped class of primitive type. One example of wrapper classes is with the java.lang.[Integer/Character/Float/etc], which can be used with generics, unlike primitive types.

As another example, imagine I want to add additional functionality to a String. String is final, so I can't extend it. However, I can make a wrapper class:

class StringWrapper
{
    public String str;

    public StringWrapper(String str)
    {
        this.str = str;
    }

    public void reverse()
    {
        char[] temp = new char[str.length()];
        for(int i = 0; i < str.length(); i++)
        {
            temp[str.length() - i] = str.charAt(i);
        }
        str = new String(temp);
    }
}

I can then say:

StringWrapper strW = new StringWrapper("abc");
strW.reverse();
System.out.println(strW.str);

Which outputs:

cba

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75