Data Types in Java Programming Language

In Java programming language, a data type is an identity that specifies the type of value a variable can store.  It's an important and basic concept that every beginner should learn because data type is fundamental to understanding Java codes. Data type makes it easy to understand the data variable type, size, what it can store and the inbuilt methods or functions of this data variable.

How to use data type?
Syntax: <data-type> <variable>;
To use data type just add the required data type with variables to use it in your program. When you declare the variable along with the data type the variable becomes strongly typed which means the variable can hold only a specific type of value and it can use only the method associated with the data type we declared. 

Why data-type is important in Java?

Java is a strictly/strongly typed programming language because all the data types are predefined and the variable declared in the program must require a data type. This will help the Java compiler to know about the variable and its data type(integer, text, decimal... etc.) and the methods associated with the data type.

Types of data types:
  • Primitive data types
  • Non-primitive data types
Primitive data types: They are the fundamentals of Java programming language. Java provides eight primitive data types and each data type has a unique purpose and properties( storage size, default value..etc.). Each primitive data type in Java has it's own Wrapper class which helps to convert from a primitive data type to an object. The data are directly stored in the variable itself in Java programming. The eight primitive data types are,
  • boolean
  • byte
  • short
  • char
  • int
  • float
  • long
  • double

Important key point :
  1. Data types are keywords (predefined words) in Java 
  2. Data types are stored in stack memory
  3. A variable must have a data type in Java
  4. A variable can't have more than one data type
  5. Once a variable is declared with a data type then the variable  is not allowed to change the data type


Data Type Size(bytes) Default Value Example Range
boolean 1 bit false boolean b = true; true, flase
byte 1 byte 0 byte b = 1; -128 to 127
short 2 bytes 0 short s = 24; -32,768 to 32,767
char 2 bytes \u0000 char c = 'a'; characters representation of ASCII values 0 to 255
int 4 bytes 0 int i = 1999; -2,147,483,648 to 2,147,483,647
float 4 bytes 0.0 float f = 1.8; 1 to 7 decimal digits
long 8 bytes 0 long l = 9962694447; -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
double 8 bytes 0.0 double d = 10.88; 1 to 16 decimal digits


Non-Primitive Data Types: These are programmer-defined data types not pre-defined like primitive data types which means a programmer actually creates these data types. Here the variables are called reference variable. A non-primitive data type can hold multiple user-defined methods and values in a single reference.


For example, Consider a car class that is created by a programmer and this class has multiple variables like window, tire, gear..etc. and multiple methods like start(), stop()..etc. Similar to primitive data type we can declare this Car type along with a variable. Now the variable can hold all the property of the car you can relate this example with any primitive data type.
Car myCar //myCar reference variable holds multiple value and methods of car class;

Types of Non-Primitive Data Type:
  • Class
  • Array
  • Interface
  • Object
  • String
Why Non-Primtive Types are known as Reference Types?

These data types are reference types in Java and so the variables of this type are called reference variables because they don’t directly store the data itself instead they hold the reference or memory address that tells where the data is stored in heap memory but the primitive data types stores the data directly itself.

/*Primtive example*/

int a=10;//a stores value 539 directly


/*Non-Primitive example*/

String str="Hello World"; // value "Hello World" stored in heap memory and str stores the memory address of it



From this example, we can understand the difference between a simple primitive type variable and the reference variable of a non-primitive type.

Post a Comment

Previous Post Next Post
B