Data Types in Java With Examples

Data types define the type and size of data that a variable can store. In this tutorial, we will get a basic overview of primitive data types in Java, as well as non-primitive data types such as arrays and objects.


Data type of a variable defines the size and type of values that can be stored in a variable. Every single bit of data which is processed, can be categorized into some data type. Java has a rich collection of data types. These data types can be classified into two categories:

  1. Primitive Data Types
  2. Non-primitive Data Types

Data Types in Java

1. Primitive Data Types

Primitive data types are the most basic data types in Java. They are built into the Java language and are not objects. Java defines 8 primitive data types, which are following:


(i). int Data Type:
  • The int data type is used to store whole numbers from -2147483648 to 2147483647.
  • It is a 32-bit signed two's complement integer.
  • Its default value is 0.

Java int Data Type Example:

java
import java.io.*;
public class IntExample {
  public static void main(String[] args) {
    // int data type
    int num = 100;
    System.out.println(num);
  }
}

(ii). long Data Type:
  • The long data type is used to store those integer values that cannot be fit into the int data type.
  • It can store whole numbers from -9223372036854775808 to 9223372036854775807.
  • It is a 64-bit two's complement integer.
  • Its default value is 0L.

Java long Data Type Example:

java
import java.io.*;
public class LongExample {
  public static void main(String[] args) {
    // long data type
    long number = 12000000000L;
    System.out.println(number);
  }
}

(iii). float Data Type:
  • The float data type is used to store fractional numbers such as 1.1, 5.32, or 9.98754321.
  • It is a single-precision 32-bit IEEE 754 floating point.
  • It can store fractional numbers from 3.4e-038 to 3.4e+038.
  • Its default value is 0.0f.

Java float Data Type Example:

java
import java.io.*;
public class FloatExample {
  public static void main(String[] args) {
    // float data type
    float num = 6.66f;
    System.out.println(num);
  }
}

(iv). boolean Data Type:
  • The boolean data type represents only one bit of information either true or false.
  • It is used for simple flags.
  • Default value of boolean data type is false.

Java boolean Data Type Example:

java
import java.io.*;
 public class BooleanExample {
   public static void main(String[] args) {
     // boolean data type
     boolean isAvailable = true;
     System.out.println(isAvailable);
   }
 }

(v). byte Data Type:
  • The byte is an 8-bit signed two's complement integer.
  • It can store whole numbers from -128 to 127.
  • Its default value is 0.
  • It saves space because a byte is 4 times smaller than an integer.

Java byte Data Type Example:

java
import java.io.*;
 public class ByteExample {
   public static void main(String[] args) {
     // byte data type
     byte b = 50;
     System.out.println(b);
   }
 }

(vi). char Data Type:
  • The char data type is used to store single 16-bit Unicode character.
  • It is used to store a single character.
  • It can store whole numbers from '\u0000' (or 0) to '\uffff'

Java char Data Type Example:

java
import java.io.*;
 public class CharExample {
   public static void main(String[] args) {
     // char data type
     char firstChar = 'A';
     System.out.println(firstChar);
   }
 }

(vii). short Data Type:
  • The short data type is a 16-bit signed two's complement integer.
  • It can store whole numbers from -32768 to 32767.
  • Its default value is 0.
  • A short data type is 2 times smaller than an integer.

Java short Data Type Example:

java
import java.io.*;
 public class ShortExample {
   public static void main(String[] args) {
     // short data type
     short age = 72;
     System.out.println(age);
   }
 }

(viii). double Data Type:
  • The double data type is a double-precision 64-bit IEEE 754 floating point.
  • It can store fractional numbers from 1.7e-308 to 1.7e+308.
  • It is used to store fractional numbers with more precision then float data type.
  • Its default value is 0.0d.

Java double Data Type Example:

java
import java.io.*;
 public class DoubleExample {
   public static void main(String[] args) {
     // double data type
     double d = 99.55d;
     System.out.println(d);
   }
 }



2. Non-Primitive Data Types

Non-primitive data types, also known as reference data types, are not built into the Java language. Instead, they are created by the programmer and are derived from the primitive data types. Non-primitive data types include arrays, objects, and strings.


(i). Arrays:

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. Arrays are useful for storing large amounts of data of the same data type.

Here's an example of creating and accessing an array in Java:

java
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[0]); // Outputs 1
System.out.println(numbers[4]); // Outputs 5

(ii). Objects:

An object is a user-defined data type that contains both data and behavior. Objects are created from class templates, which define their properties and methods. Objects can be used to model real-world entities and their characteristics and actions.

Here's an example of creating and using an object in Java:

java
class Dog {
  String breed;
  int age;

  void bark() {
    System.out.println("Woof!");
  }
}

Dog myDog = new Dog();
myDog.breed = "Labrador";
myDog.age = 5;
myDog.bark(); // Outputs "Woof!"

(iii). Strings:

A string is a non-primitive data type that represents a sequence of characters. Strings are immutable, meaning they cannot be changed once created. Strings are often used to store text data or messages.

Here's an example of creating and using a string in Java:

java
String greeting = "Hello, World!";
System.out.println(greeting); // Outputs "Hello, World!"

(iv). Classes: A class is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is a template for objects. Objects have member variables and have behavior associated with them. In Java, an object is created from a class.

For example:

java
class Car {
  // data members
  int modelYear;
  String modelName;

  // member function
  public void startEngine() {
    System.out.println("Engine started");
  }
}

To use the above class, we need to create an object of it and then we can access the members of the class using the dot (.) operator.

java
Car myCar = new Car();
myCar.modelYear = 1969;
myCar.modelName = "Mustang";
myCar.startEngine();

(v). Interfaces:

An interface is a collection of abstract methods and constants that form a common set of base rules. A class can implement an interface, in which case it must implement all the methods defined in the interface. An interface can be used to achieve polymorphism.

An interface is declared using the interface keyword, and the methods in an interface are declared with the abstract keyword.

For example:

java
interface Animal {
  public void makeSound();
  public void eat();
}

A class that implements the Animal interface must implement both the makeSound() and eat() methods.

java
class Cat implements Animal {
  public void makeSound() {
    System.out.println("Meow");
  }

  public void eat() {
    System.out.println("Cat is eating");
  }
}

To use the above class, we need to create an object of it and then we can access the members of the class using the dot (.) operator.

java
Cat myCat = new Cat();
myCat.makeSound();
myCat.eat();