Skip to content

Datatypes

In Java, data types define the type of a variable and the kind of data it can store. Java is a strongly typed language, which means each variable must be declared with a data type. There are two main categories of data types in Java:

  1. Primitive Data Types
  2. Reference Data Types

1. Primitive Data Types

Java has 8 built-in primitive data types. These are the most basic types of data and hold their values directly in memory.

The 8 Primitive Data Types in Java:

Data TypeSizeDefault ValueDescription
byte1 byte0Represents a very small integer (-128 to 127).
short2 bytes0Represents a small integer (-32,768 to 32,767).
int4 bytes0Represents a standard integer (-2^31 to 2^31-1).
long8 bytes0LRepresents a large integer (-2^63 to 2^63-1).
float4 bytes0.0fRepresents a single-precision floating-point number.
double8 bytes0.0dRepresents a double-precision floating-point number.
char2 bytes'\u0000'Represents a single Unicode character.
boolean1 bitfalseRepresents a value that can be either true or false.

Examples of Primitive Data Types:

java
public class DataTypesExample {
    public static void main(String[] args) {
        byte b = 100; // byte data type
        short s = 10000; // short data type
        int i = 100000; // int data type
        long l = 100000L; // long data type

        float f = 10.5f; // float data type
        double d = 20.99; // double data type

        char c = 'A'; // char data type
        boolean isJavaFun = true; // boolean data type

        System.out.println("Byte value: " + b);
        System.out.println("Short value: " + s);
        System.out.println("Int value: " + i);
        System.out.println("Long value: " + l);
        System.out.println("Float value: " + f);
        System.out.println("Double value: " + d);
        System.out.println("Char value: " + c);
        System.out.println("Boolean value: " + isJavaFun);
    }
}

2. Reference Data Types

Reference data types refer to objects or arrays and hold the reference (or memory address) where the data is located rather than the data itself. These types are used to store complex data structures.

Types of Reference Data Types:

  • Classes: A class is a blueprint for creating objects that contain data and methods.
  • Interfaces: An interface defines a contract or behavior that a class must implement.
  • Arrays: An array is a collection of data items of the same type, stored in contiguous memory locations.

Example of Reference Data Types:

java
public class ReferenceExample {
    public static void main(String[] args) {
        // Reference type for a String object
        String name = "Java Programming";
        
        // Reference type for an array of integers
        int[] numbers = {1, 2, 3, 4, 5};

        // Reference type for an object of a custom class
        Person person = new Person("Alice", 25);

        System.out.println("String value: " + name);
        System.out.println("Array values: ");
        for (int num : numbers) {
            System.out.println(num);
        }
        System.out.println("Person name: " + person.name + ", Age: " + person.age);
    }
}

class Person {
    String name;
    int age;

    // Constructor
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

Key Differences Between Primitive and Reference Data Types:

FeaturePrimitive Data TypesReference Data Types
StorageStore actual dataStore reference (memory address)
Default ValuePredefined (e.g., 0, false)null (no reference)
Memory SizeFixed size (based on type)Depends on the object size
Examplesint, float, char, booleanString, arrays, custom classes

Type Conversion in Java:

In Java, type conversion is the process of converting one data type to another. There are two types:

  1. Implicit (Automatic) Type Conversion: When a smaller type is automatically converted to a larger type (e.g., int to long).

  2. Explicit (Manual) Type Conversion (Casting): When a larger type is manually converted to a smaller type (e.g., double to int), which requires casting.

Example of Implicit Conversion:

java
public class ImplicitConversion {
    public static void main(String[] args) {
        int num = 100;
        long bigNum = num; // int to long (implicit conversion)

        System.out.println("Long value: " + bigNum);
    }
}

Example of Explicit Conversion (Casting):

java
public class ExplicitConversion {
    public static void main(String[] args) {
        double pi = 3.14159;
        int piInt = (int) pi; // double to int (explicit casting)

        System.out.println("Pi as integer: " + piInt);
    }
}

Conclusion:

Java provides a rich set of primitive and reference data types that allow developers to work with a wide range of data. Primitive data types are used for simple data storage, while reference types are used for storing more complex data structures like objects and arrays. Understanding these data types and their characteristics is essential for writing efficient and type-safe Java programs.

J2J Institute private limited