Why to avoid wrapper classes in java?

Hey developers, I am back again with another interesting stuff called Wrapper Classes .

What is a wrapper class ?

As per WikipediaA wrapper class is a class that encapsulates types, so that those types can be used to create object instances and methods in another class that need those types

Java has dual type system, so that an individual can use primitives or objects(wrappers). There are 8 primitives java supports i.e. boolean,  float, double, int, long, char, byte, short and their wrappers are Boolean, Float, Double, Integer etc.

There are lots of conflicts about usage of primitives. But in this article I am going to tell you certain reasons to use primitives unless you need to use wrappers.

Why to use primitive types ?

  1. Memory utilization

A primitive type holds value where as the wrapper or object holds the reference of the value. Due to this wrapper will end up occupying more space than the primitive one. I just ran a benchmark test to see the result. Here is a sample code

import java.util.*;

public class HelloWorld{

     public static void main(String []args){
        System.out.println("bytes ="+getBytes(500));
     }
     
     public static long getBytes(int count)
  {
    long memStart = Runtime.getRuntime().freeMemory();
    Double[][] arr = new Double[count][count];
    Random random = new Random();
    // added some random values
    for (int i = 0;  i < count;  ++i)
      {
        for (int j = 0; j < count;  ++j)
            arr[i][j] = random.nextDouble();
      }

    long memEnd = Runtime.getRuntime().freeMemory();

    return memStart - memEnd;
  }
}

This is using 2 dimensional array of Double (wrapper class)  and here is another sample code with primitive

import java.util.*;

public class HelloWorld{

     public static void main(String []args){
        System.out.println("bytes ="+getBytes(500));
     }
     
     public static long getBytes(int count)
  {
    long memStart = Runtime.getRuntime().freeMemory();
    double[][] arr = new double[count][count];
    Random random = new Random();
    // added some random values
    for (int i = 0;  i < count;  ++i)
      {
        for (int j = 0; j < count;  ++j)
            arr[i][j] = random.nextDouble();
      }

    long memEnd = Runtime.getRuntime().freeMemory();

    return memStart - memEnd;
  }
}

After execution of both from an online java compiler, here is what I got

case Primitivebytes =2045456

case Wrapper : bytes =6918800

So based on this test, primitive is the hero!!!

2. Runtime Performance

Now let’s have a look on how much processing time primitives and wrappers take.

Again I am sharing sample code similar to the above one with addition to time calculation

import java.util.*;

public class HelloWorld{

     public static void main(String []args){
        long time1 = System.currentTimeMillis( );
        System.out.println("bytes ="+getBytes(500));
        long time2 = System.currentTimeMillis( );
        long result = time2 - time1;
        System.out.println("duration ="+result);
     }
     
     public static long getBytes(int count)
  {
    long memStart = Runtime.getRuntime().freeMemory();
    Double[][] arr = new Double[count][count];
    Random random = new Random();
    // added some random values
    for (int i = 0;  i < count;  ++i)
      {
        for (int j = 0; j < count;  ++j)
            arr[i][j] = random.nextDouble();
      }

    long memEnd = Runtime.getRuntime().freeMemory();

    return memStart - memEnd;
  }
}

This one is with wrappers and here is the one with primitives –

import java.util.*;

public class HelloWorld{

     public static void main(String []args){
        long time1 = System.currentTimeMillis( );
        System.out.println("bytes ="+getBytes(500));
        long time2 = System.currentTimeMillis( );
        long result = time2 - time1;
        System.out.println("duration ="+result);
     }
     
     public static long getBytes(int count)
  {
    long memStart = Runtime.getRuntime().freeMemory();
    double[][] arr = new double[count][count];
    Random random = new Random();
    // added some random values
    for (int i = 0;  i < count;  ++i)
      {
        for (int j = 0; j < count;  ++j)
            arr[i][j] = random.nextDouble();
      }

    long memEnd = Runtime.getRuntime().freeMemory();

    return memStart - memEnd;
  }
}

Now after execution here are the results :

Output with primitive:  duration =19

Output with wrapper class: duration =33 

So again primitive type’s performance beats wrapper class.

 

I hope this two benchmarks are enough for all of you to start using primitives over wrapper classes.

Happy coding!!!

Leave a Reply

Your email address will not be published. Required fields are marked *