Data Types and Wrapper Classes in Java

Code Java

In the previous article, we had a look at Access Specifier and Access Modifier. In this article, I will discuss what are the data types and wrapper classes available in Java and the concept of enums. Java supports both predefined as well as user-defined data types.

Broadly data types can be divided into 2 categories:

Primitive Data Types:

I have already mentioned,  that Java supports primitive data types due to which it does not qualify the category of being a fully object-oriented language. Because if a language supports primitive data types, then the language is not a pure object-oriented language. The primitive data-types that Java supports are:

Boolean:

Boolean in Java only supports true or false. If true is assigned, then the only output is true. Any other value in Java is false.

Header file for boolean – <stdbool.h>

  • boolean

Character:

Character in Java support Unicode. Java supports all characters supported by C, C++ plus some special constants and local constants. It also supports local language alphabets like Oriya and Hindi, in the form of Unicode. ASCII value of any character is positive, so it holds only positive values and no negative values.

  • char

Integer types:

There are many ways to store numerical values. Java has 4 primitive data types to store whole numbers of both positive and negative value. These data types have different ranges and different storage capacity.

  • byte
  • short
  • int
  • long

Floating Points:

The floating points store fractional values along with numbers within its predefined range. Real constant values by default are double in nature. Java supports 2 data types for representing floating points.

  • float:
  • double:

Wrapper Classes:

  • For each data type, Java provides a predefined class called Wrapper Class.
  • Wrapper classes wrap primitive data type value into a class object. It is this wrapper class that helps to make Java object-oriented.
  • All the Wrapper classes present in Java are present inside java.lang package. And java.lang package is the default package in Java.

The table sums up all the characteristics of the above primitive data types:

DataTypeDescriptionDefault ValueSize ExampleWrapper Class
booleantrue/falsefalse1 bittrue, falseBoolean
bytetwo's complement integer08 bits
(1 byte)
100, 60, -90Byte
charunicode character\u000016 bits
(2 byte)
'a', '\u0023', '#', '2', 'X'Character
shorttwo's complement integer016 bits
(2 byte)
6000, 18, -10000Short
inttwo's complement integer032 bits
(4 bytes)
600000000, -1000000000Integer
longtwo's complement integer064 bits
(8 bytes)
-3L, -1L, 2000000000000000000LLong
floatfloating point0.032 bits
(4 bytes)
1.34e100f, -1.32e-100fFloat
doublefloating point0.064 bits
(8 bytes)
1.345e400d, -1.32456eDouble

The below program shows all the default values held by these primitive data types.

package test;

public class PrimitiveDataTypes {
  //data type variables have not been initialized to display the default values.
  boolean b;
  byte by;
  char c; char ch1='\u0023'; char ch2 = 'X';
  short s;
  int i;
  long l; 
  float f;
  double d;
  
  public static void main(String[] args) {
    
    PrimitiveDataTypes ob = new PrimitiveDataTypes();
    
    System.out.println("Default value of boolean is "+ ob.b);
    System.out.println("Default value of byte is "+ ob.by);
    System.out.println("Default value of char is "+ ob.c + "Blank character having unicode as '\u0000'"); 
	System.out.println("value of char(unicode format given as input) is "+ ob.ch1); 
    System.out.println("value of char is "+ ob.ch2); 
    System.out.println("Default value of short is "+ ob.s);
    System.out.println("Default value of int is "+ ob.i);
    System.out.println("Default value of long is "+ ob.l);
    System.out.println("Default value of float is "+ ob.f);
    System.out.println("Default value of double is "+ ob.d);
  }
    
}

 

Output :

data types result

 

Note: 

You can calculate the range of the data types by using the below formula.

Formula:

-2^(n-1) to (2^(n-1)-1) where n = no. of bits in the primitive datatype.

For example:

For int datatype, n is 32 ( refer to the above table).

So, the range of int is : -2^(32-1) to (2^(32-1)-1) = 2^31 to 2^(31)-1

This way you can calculate the storage range for all the 8 data types.

Non Primitive Data Types:

  • Non Primitive Data Types refers to objects. These are defined by the user or programmer according to the needs of the code.
  • Basically, the main difference between primitive and non-primitive data type is that Non-primitive data types do not store values. They store a reference or address to value.
  • When you create objects of a class, that object is a non-primitive data type.
    package test;
    
    public class Student {
      
      public static void main(String[] args) {
        
        Student student1 = new Student(); //student1 is a variable of type Student 
      }
    }
    
  • Some examples of non-primitive data types are Arrays, Strings, Collections, classes. Out of all these String is a class already present in Java.

Enum:

  • Sometimes programmers need more than just primitive and object variables. So in order to aid this situation, Java supports a new type of the variable i.e. enum.
  • Enum stands for Enumerated data types.
  • This helps to create and define totally new data types.
  • The enum can have only a fixed number of possible values which are defined at the time of its creation.
  • The syntax to define enums:

enum enum-type-name  { (list-of-enum-values) }

package test;

public class Enum {
  
  //declaration of enum named months
  enum months {JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,AUGUST, SEPTMEBER};
  
  public static void main(String[] args) {
    
    months month1 = months.APRIL; //assign values to variables of enum type		
    System.out.println("The value of month 1 " + month1);
    
    months month2 = months.MARCH;
    System.out.println("The value of month 2 " +month2);
  }

}

Output:

 

 

So this was all about the data types present in Java. We saw that Java provides immense flexibility for declaring and defining data types.

In the next article, we will discuss the types and scope of blocks in Java.