Data Types in Java

In Java, data types are used to define the type of data that can be stored in a variable. There are two main categories of data types in Java: primitive and reference types.

1. Primitive Data Types:

Primitive data types are used to store simple values, such as numbers and characters. There are eight primitive data types in Java:

byte: used to store integer values from -128 to 127

short: used to store integer values from -32,768 to 32,767

int: used to store integer values from -2,147,483,648 to 2,147,483,647

long: used to store integer values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float: used to store floating-point values with a single-precision of 32 bits

double: used to store floating-point values with a double-precision of 64 bits

char: used to store a single character or Unicode value

boolean: used to store a true/false value

Here's an example of using primitive data types to store different values:

                
    byte age = 25;
    short salary = 55000;
    int population = 1000000;
    long distance = 10000000000L; // note the "L" suffix for long
    float pi = 3.1415f; // note the "f" suffix for float
    double gravity = 9.8;
    char gender = 'M';
    boolean isMarried = true;
                
            
Reference Data Types:

Reference data types are used to store objects created from classes or interfaces. Reference variables hold the memory address of the object, rather than the object itself.

                
    String name = "John Doe";
                
            

In addition to the primitive and reference types, Java also supports other data types, such as arrays and enums. Arrays are used to store a collection of values of the same data type, while enums are used to define a set of named constants.

In summary, Java supports a wide range of data types, including primitive, reference, array, and enum types. Understanding the different data types is essential for developing robust and efficient Java programs.