Java Tips: Wherever Possible try to Use Primitive Types Instead of Wrapper Classes

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » Java Tips: Wherever Possible try to Use Primitive Types Instead of Wrapper Classes

Wherever possible try to use Primitive types instead of Wrapper classes

Not everything in Java is an object. There is a special group of data types (also known as primitive types) that will be used quite often in your programming. For performance reasons, the designers of the Java language decided to include these primitive types.

Creating an object using new isn’t very efficient because new will place objects on the heap. This approach would be very costly for small and simple variables. Instead of create variables using new, Java can use primitive types to create automatic variables that are not references. The variables hold the value, and it’s place on the stack so its much more efficient.

Java determines the size of each primitive type. These sizes do not change from one machine architecture to another (as do in most other languages). This is one of the key features of the language that makes Java so portable.

Take note that all numeric types are signed. (No unsigned types).

Wrapper classes are great. But at same time they are slow. Primitive types are just values, whereas Wrapper classes are stores information about complete class.

Sometimes a programmer may add bug in the code by using wrapper due to oversight. For example, in below example:

int x = 10;
int y = 10;

Integer x1 = new Integer(10);
Integer y1 = new Integer(10);

System.out.println(x == y);
System.out.println(x1 == y1);

The first Println will print true whereas the second one will print false. The problem is when comparing two wrapper class objects we cant use == operator. It will compare the reference of object and not its actual value.

Also if you are using a wrapper class object then never forget to initialize it to a default value. As by default all wrapper class objects are initialized to null.

Boolean flag;

if(flag == true) {
    System.out.println("Flag is set");
} else {
    System.out.println("Flag is not set");

The above code will give a NullPointerException as it tries to box the values before comparing with true and as its null.

Handy Reference:

Data Types and Data Structures - Crunchify

7 thoughts on “Java Tips: Wherever Possible try to Use Primitive Types Instead of Wrapper Classes”

  1. there has some wrong, because of cache, x1==y1 is true also when the value between -128 – 127

    Integer x1 = new Integer(10);
    Integer y1 = new Integer(10);

    System.out.println(x1 == y1);

  2. To convert simple data types into objects, that is, to give object form to a data type;To convert strings into data types (known as parsing operations).This is what you want to explain.If you explain the difference between primitive data type and data in object form in terms of a java program for use in List,Map etc., that will be much better explaination.Because people who have learned programming using C++ or C, for them wrapper class is not of much use.In C or C++ just use atoi for converting string to integer without going through un-necessary wrapper class. There are many great things in Java and more than that there are un-necesary things which could be simplified.The best thing you said is “avoid wrapper class”.

  3. Found your site today and already I am liking it. I am new to Java programming due to a critical need to a program at work and a very short time to make it happen. I have a question about ‘date’ – in all my help-seeking I find creating ‘date = new Date();’ and not using a primitive. Might this apply for a date as well as examples above? The date variable is later outputted using ‘SimpleDateFormat’ function.

    • Hi Dave,

      Sorry for late reply. Java provides the Date class available in java.util package, this class encapsulates the current date and time. As you already mentioned, below generates new Date.

      import java.util.Date;
      public class CrunchifyDate {
         public static void main(String args[]) {
             // Instantiate a Date object
             Date date = new Date();
             // display time and date using toString()

      I believe, Date doesn’t fit into primitive type.

  4. Hi,

    Nice piece of information provided.I wanted to do my 6-weeks online training on java ,so i have searched a lot and came across this. I hope it works for me and also wanted to know weather anyone has studied from this website please let me know or tell me any other guidance…

    would really appreciate help…

  5. I am basically not a programmer and I am comparatively new to Java technology , so I was wondering what all topics should be covered up if i have to start java from the start and has any one
    studied or got any info regarding this 6 week java training online course and should we also have knowledge of C language before we further move on to Advance Java topics??


Leave a Comment