Loading

Data Types in Java

  • Programming data types define the kind of data a variable can hold.
  • They determine the operations that can be performed on the data and how it is stored in memory.

In Java, data types are divided into two main categories: primitive and reference (or non-primitive) data types. 

Primitive Data Types

  • Primitive data types are predefined by the language and named by a keyword. 
  • They are the most basic data types available within the Java language. 

There are eight primitive data types in Java:

boolean:

  • Description: It represents one bit of information, but its "size" isn't precisely defined.
  • Values: true or false.

byte:

  • Description: 8-bit signed integer.
  • Range: -128 to 127.
  • Default Value: 0.

short:

  • Description: 16-bit signed integer.
  • Range: -32,768 to 32,767.
  • Default Value: 0.

int:

  • Description: 32-bit signed integer.
  • Range: -2^31 to 2^31-1.
  • Default Value: 0.

long:

  • Description: 64-bit signed integer.
  • Range: -2^63 to 2^63-1.
  • Default Value: 0L.

float:

  • Description: Single-precision 32-bit IEEE 754 floating point.
  • Range: Approximately ±3.40282347E+38F (6-7 significant decimal digits).
  • Default Value: 0.0f.

double:

  • Description: Double-precision 64-bit IEEE 754 floating point.
  • Range: Approximately ±1.79769313486231570E+308 (15 significant decimal digits).
  • Default Value: 0.0d.

char:

  • Description: 16-bit Unicode character.
  • Range: '\u0000' (or 0) to '\uffff' (or 65,535).
  • Default Value: '\u0000'.

Reference types in Java are any objects created from classes. They are used to access objects and include:

  1. Class Objects: Instances of classes, e.g., String, Scanner.
  2. Array: Objects that store multiple items of the same type.
  3. Interface: Defines a contract that classes can implement.
  4. Enumeration: A special Java type used to define collections of constants.

Key Differences Between Primitive and Reference Data Types:

  • Memory: Primitive data types are stored directly in the memory locations allocated for variables, while reference types store references (or addresses) to the objects in memory.
  • Default Values: Primitive data types have default values (e.g., 0 for integers, false for boolean). Reference types have a default value of null.
  • Methods: Primitive data types do not have methods associated with them, whereas objects of reference types do.

Examples:

Here's a basic example to illustrate the use of primitive and reference data types:

public class DataTypesExample {
    public static void main(String[] args) {
        // Primitive data types
        int age = 25;
        char grade = 'A';
        boolean isJavaFun = true;
        double price = 19.99;

        // Reference data types
        String name = "John";
        int[] scores = {90, 85, 88};

        // Output
        System.out.println("Age: " + age);
        System.out.println("Grade: " + grade);
        System.out.println("Is Java Fun: " + isJavaFun);
        System.out.println("Price: " + price);
        System.out.println("Name: " + name);
        System.out.print("Scores: ");
        for (int score : scores) {
            System.out.print(score + " ");
        }
    }
}

In this example:

  • int, char, boolean, and double are primitive data types.
  • String and int[] (array of integers) are reference data types.