Home > Java > Dont Compute With Wrapper

Dont Compute With Wrapper

As a Java Developer, i got these interview questions a lot… really. What is the difference between int and Integer, which one is better ?

The answer for the difference, can be answered easily, int is a primitive data type and Integer is wrapper object for int, and so on bla bla bla google it.

But, for the better one the answer would be  depends on the purpose of it. For this example, i will put simple demo to show how slow is the Wrapper when we use it for the mathematics computation.

import java.util.Date;
public class Tester {
	public void runInteger() {
		long startTime = new Date().getTime();
		Integer ctr = 0;
		for (int i = 0; i < Integer.MAX_VALUE; i++) {
			ctr += i;
		long endtime = new Date().getTime();
				.println("Time for Integer: " + (endtime - startTime) + " ms");


	public void runint() {
		long startTime = new Date().getTime();

		int i = 0;
		int ctr = 0;
		for (i = 0; i < Integer.MAX_VALUE; i++) {
			ctr += i;

		long endtime = new Date().getTime();

		System.out.println("Time for int: " + (endtime - startTime) + " ms");


	public static void main(String[] args) {
		new Tester().runInteger();
		new Tester().runint();
		new Tester().runInteger();

and the output was:

Time for Integer: 7652 ms

Time for int: 5 ms

Time for Integer: 7541 ms

We can see the interval difference between primitive and wrapper on this example is quite huge ~ 7500ms .  But, unfortunately, many developers today didnt and dont aware of this,  because of the auto boxing-unboxing features since Java 1.5, the process is hidden on the background.

Categories: Java
  1. 12 September 2013 at 11:24

    I an in need of consultancy regarding data format and connectivity of my VB.net program. Can I get?

    • RDeJourney
      24 September 2013 at 10:52

      Hello, what kind of consultancy that you mentioned here?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: