Creation date:

Wrapper objects

There is a wrapper class for every primitive and all wrapper objects are immutable. Let's explain it on the following example

public static void main(String[] args) {
  Integer x = 15;
  Integer y = x;
  System.out.println("Before increment "+(x==y));
  System.out.println("After increment "+(x==y));

The first comparision will print true, because x and y are keeps the address of the same object in memory. But what will occur during x++ increment. As we said, wrapper classes are immutable. Hence JVM will autounbox x, increment it and creates the new Integer wrapper class. x now holds the address of the newly created class in memory and these new address is different. But y still holds the reference of old object. The output will be:

Before increment true
After increment false

Now look, how equality(==) works with wrapper objects.

Integer x1 = 1000;
Integer y1 = 1000;
System.out.println("x1 == y1 is "+(x1==y1));
Integer x2 = 100;
Integer y2 = 100;
System.out.println("x2 == y2 is "+(x2==y2));

This example will produce the following result

x1 == y1 is false
x2 == y2 is true

We know, that x1 and y1 are the different objects, hence equality x1 == y1 returns false. So why x2 == y2 returns true. In order to save memory, Java allocates one memory piece for the following wrapper objects

Now lets change the code

Integer x2 = new Integer(100);
Integer y2 = new Integer(100);
System.out.println("x2 == y2 is "+(x2==y2));

It will produce

x2 == y2 is false

It means the above mentioned rule regards only to autoboxing, but if you create wrapper objects by regular way, two objects will be created in different memory pieces.

Lets do one more change in this code

int x2 = 100;
Integer y2 = new Integer(100);
System.out.println("x2 == y2 is "+(x2==y2));

It'll produce

x2 == y2 is true

because when == used to compare primitive to wrapper, the wrapper will be unwrapped and the comparision will be primitive to primitive.

equals() with wrapped objects

equals() of two wrapped objects claims, that two objects are equal if they are of the same type and have the same value. But how about equivalence of wrapped and primitive. Lets try to clarify it on the following example

Short x = 100;
Integer y = 100;
System.out.println("Short to Integer is "+(x.equals(y)));

Short x1 = 100;
short y1 = 100;
System.out.println("Short to short is "+(x1.equals(y1)));

Short x2 = 100;
int y2 = 100;
System.out.println("Short to int is "+(x2.equals(y2)));

It'll produce

Short to Integer is false
Short to short is true
Short to int is false

First line is obvious. On the second comparision short is wrapped to Short and comparision as made against Short to Short. Hence the result is true. On the third comparision int is wrapped to Integer and comparision made against Short to Integer. As we said before, they're not equal.

Author: Jafar N.Aliyev (Jsoft)

Read also

Static methods can not be overriden

Here I explain, why static methods can not be overriden

Use instanceof carefully

Usage of 'instanceof' method in some situations will give you a compilation error

Java access and nonaccess modifiers

Look at this cheat sheet for Java access and nonaccess modifiers

© Copyright

All articles in this site are written by Jafar N.Aliyev. Reproducing of any article must be followed by the author name and link to the site of origin(this site). This site also keeps the same rules relative to the articles of other authors.