DEV Community

VIDHYA VARSHINI
VIDHYA VARSHINI

Posted on

Data types in Java

What is data type: In Java, data types specify the type of data a variable can store such as text, integers, or decimal numbers and determine the operations that can be performed on that data.
They help Java manage memory efficiently and ensure type safety during compilation.

There are two types of data in Java:
1. Primitive data type
2. Non-primitive data type

Primitive data type: This is the building blocks which stores simple values directly in memory. Examples of primitive data types are boolean, char, byte, short, int, long, float and double.

a) byte: 1 byte = 8 bits
2^8 = 256
Value ranges from -128 to +127.

b) Short: 2 bytes [16 bit]
2^16 = 65536
Value ranges from -32768 to +32767.

c) int: 4 bytes [32 bit]
2^32 = 2147483648
Value ranges from -2,147,483,648 to 2,147,483,647.

d) long: 8 bytes [64 bit]
2^64 = 1.8446744e+19
Value ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

e) float: This stores fractional numbers which is sufficient for storing 6 to 7 decimal digits.

f) double: This stores fractional numbers which is sufficient for storing 15 to 16 decimal digits.

g) char: This stores a single character or letter or ASCII values.

h) boolean: This stores true or false values. This contains 1 bit.

Ex:

class Main {
    public static void main(String[] args) {
        int i = 30;
        float num = 18.00f;
        double number = 12.45;
        long a = 145667632l;
        char value = 'v';
        boolean s = false;
        System.out.println(i);
        System.out.println(num);
        System.out.println(number);
        System.out.println(a);
        System.out.println(value);
        System.out.println(s);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

30
18.0
12.45
145667632
v
false
Enter fullscreen mode Exit fullscreen mode

Non-primitive data type: This will contain a memory address of variable values because the reference types won’t store the variable value directly in memory. Examples are strings, objects, arrays, etc.
a) String: It is defined as an array of characters. The difference between a character array and a string in Java is, that the string is designed to hold a sequence of characters in a single variable whereas, a character array is a collection of separate char-type entities.
Ex: String a = "Geek1";

b) Class: It is an user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.
Ex:

class Car {
    String model;
    int year;

    Car(String model, int year) {
        this.model = model;
        this.year = year;
    }

    void display() {
        System.out.println(model + " " + year);
    }
}

Enter fullscreen mode Exit fullscreen mode

c) Object: Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

Top comments (0)