Java Data Types

By | | Updated : 2022-05-31 | Viewed : 45 times

Java Data Types

Everyone knows that each programming language such as Java processes data based on the requirements. As a developer, we must know about all types of data in Java.

In Java, Two types of data types are used to write the different programs. These data types in the Jave are Primary Data Types and Non-Primary Data Types.

Primary Data Types

The primary data types are the crucial data types in Java and without these data types, it is not possible to process the data in Java. Primary data types are predefined by Java and developers can simply use them and are not required to declare them. Primitive data types are the main data blocks for non-primitive data types.

boolean: The boolean data type is used for storing two values only. The possible values for the boolean data type are true and false. The memory to be occupied by the boolean is one byte. These variables are mainly processed in Java condition check. We use this boolean data type for storing the flag typed values such isGameStarted, and isEngineStarted.

Default size: 1 byte
default value: false
boolean example in Java
boolean isJavaRobust = true;

char: char as the name itself indicates that it is used for storing the character values in this data type. It allowed storing 65,535 different types of characters.

Default size: 2 bytes
default value: '\u0000';
char example in Java
char ch = 's';

byte: The smaller data type of int family which can hold the memory of only one byte. byte also can hold inter only but with limitations. -128 to 127 is a range of the byte data type. So you can use the byte data type to manipulate within this range.

Default size: 1 byte
Default value: 0
byte example in Java
byte a= 65;
byte b = -65;

short: short is an inter typed data type which holds the data of integer typed. The range of short is -256 to 255. It can also be used to store the integer typed value but within the range.

Default size: 2 bytes
Default value: 0
short example in Java
short a = 100;
short b = -100;

int: int is the main and smaller integer typed data type than long. It is generally used in a program until no need issue with memory. It can hold the data from - 2,147,483,648and to 2,147,483,647.

Default size: 2^31 to 2^31-1
Default value: 0
int example in Java
int a =  100000;
int a =  -100000;

long: the biggest integer typed data type is used for storing bigger numbers. The range for long is from -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1). It should be preferred when we need to store the bigger values of integers.

Default size: 2^63 to 2^63-1
Default value: 0
long example in Java
int a =  4350000000000l;
int b =  -4350000000000l;

float: It is a decimal typed data type with single precision. Integer values can not be stored in float data typed variables such as things count. We can use float instead of double if there is no constraint of precision.

Default size: infinity
Default value: 0.0
float example in Java
float f = 10.5f;

double: It is also a decimal typed data type in Java. It is a double-precision data type. double data typed variables should not be used for storing integer values.

Default size: infinity
Default value: 0.0
double example in Java
double f = 10.5;

Non-Primary Data Types

Using Primary Data Types, we can define the Non-Primary Data Types to achieve the requirements through the programming. In Java, Non-Primary Data Types are

  • Class

  • Interface

  • String

  • Array

1. Classes In Java:

Classes are User-defined data type that contains different types of primitive and non-primitive data types. The classes are called prototypes of objects.

Class Example In Java
public class BankAccount {

        private int accountNumber;
        private int balance;
        public BankAccount(int accountNumber,int balance){
            this.accountNumber = accountNumber;
            this.balance = balance;
        void withdraw(int amount) {
            this.balance =  this.balance - amount;
            System.out.println("Withdrawing money: "+amount+" in account of : "+this.accountNumber);
            System.out.println("Balance amount: "+this.balance);
        void deposit(int amount) {
            this.balance =  this.balance + amount;
            System.out.println("depositing money"+ amount +" in account of : "+this.accountNumber);
            System.out.println("Balance amount: "+this.balance);

    public static void main(String[] args) {
        BankAccount acc = new BankAccount(1000110002, 20022500);



2. Interfaces In Java

This is similar to classes but the object will not be created for the interface. And interface does have empty body methods. . When implemented class is instantiated then data will be created in that implemented class's object.

Interface Example In Java
public interface Vehicle {
    short wheels= 4;
    public short getNoOfWheels();
    public String getEngineName();

public class Audi implements Vehicle {
    public String getEngineName() {
        return "Engine X555X";
    public short getNoOfWheels() {

        return this.wheels;
    public static void main(String[] args) {
        Audi audi = new Audi();
        System.out.println("Audi has engine" + audi.getEngineName() +"and with wheels: "+ audi.wheels);


3. Strings In Java

A special data type designed for storing the collection of characters. It is a predefined class that has lot of methods used to manipulated the string data type.

String Example In Java
String str = "This is string.";
        String subStr = str.substring(0,4);

4. Arrays in Java

Arrays are collection of homogeneous data type. homogeneous means we can store collection integers, chars or float in one array. Array are index based and index will be started form 0. Arrays is the predefined class which is used to manipulated the array data type.

Array Example In Java
int inArray[] = new int[10];
        char charArray[] = new char[20];
        float floatArray[] = new float[30];


Please find example code repo here Java-Data-Types-Example-App

Leave A Reply