Skip to content

Java Primitive Data Types

Java is a strongly typed language, and it provides eight primitive data types to represent simple values such as numbers, characters, and logical values. Primitive data types are predefined by the language and serve as the building blocks for data manipulation.


Categories of Primitive Data Types

  1. Numeric Data Types:
    1. For storing numbers (integer and floating-point values).
  2. Non-Numeric Data Types:
    1. For characters and logical values.
CategoryData TypeSizeDefault ValueRange
Integerbyte8 bits0-128 to 127
short16 bits0-32,768 to 32,767
int32 bits0-2^31 to 2^31 – 1
long64 bits0L-2^63 to 2^63 – 1
Floating-Pointfloat32 bits0.0fApproximately ±3.40282347E+38F
double64 bits0.0dApproximately ±1.79769313486231570E+308
Characterchar16 bits (Unicode)\u00000 to 65,535
Booleanboolean1 bit (virtual)falsetrue or false

Detailed Overview

1. Integer Types

Integer data types are used to store whole numbers.

  • byte:
    • Smallest integer type.
    • Useful for saving memory in large arrays.
    • Example:

byte b = 100;

  • short:
    • Larger than byte, used when memory is a constraint.
    • Example:

short s = 20000;

  • int:
    • Default type for integers.
    • Commonly used in programs.
    • Example:

int num = 123456;

  • long:
    • Larger range than int.
    • Use suffix L to denote a long literal.
    • Example:

long distance = 12345678910L;


2. Floating-Point Types

Floating-point types store decimal numbers.

  • float:
    • Single-precision, less accurate but consumes less memory.
    • Use suffix f for float literals.
    • Example:

float price = 10.99f;

  • double:
    • Double-precision, more accurate.
    • Default for floating-point numbers.
    • Example:

double pi = 3.141592653589793;


3. Character Type

  • char:
    • Stores a single 16-bit Unicode character.
    • Enclosed in single quotes.
    • Example:

char grade = ‘A’;


4. Boolean Type

  • boolean:
    • Represents one of two values: true or false.
    • Commonly used in conditional statements.
    • Example:

boolean isJavaFun = true;


Default Values

Each primitive type has a default value when it is a member of a class (uninitialized local variables must be explicitly initialized).

Data TypeDefault Value
byte0
short0
int0
long0L
float0.0f
double0.0d
char\u0000
booleanfalse

Type Conversion

1. Implicit Conversion

  • Automatically performed when converting a smaller type to a larger type.
  • Example:

int num = 10;

long bigNum = num; // Implicit conversion

2. Explicit Casting

  • Required when converting a larger type to a smaller type.
  • Example:

long largeNum = 100L;

int smallNum = (int) largeNum; // Explicit casting


Example Program

public class PrimitiveTypesDemo {

    public static void main(String[] args) {

        // Integer types

        byte smallValue = 10;

        short mediumValue = 1000;

        int integerValue = 100000;

        long largeValue = 100000000L;

        // Floating-point types

        float price = 19.99f;

        double bigDecimal = 12345.6789;

        // Character type

        char grade = ‘A’;

        // Boolean type

        boolean isPassed = true;

        // Displaying values

        System.out.println(“Byte value: ” + smallValue);

        System.out.println(“Short value: ” + mediumValue);

        System.out.println(“Int value: ” + integerValue);

        System.out.println(“Long value: ” + largeValue);

        System.out.println(“Float value: ” + price);

        System.out.println(“Double value: ” + bigDecimal);

        System.out.println(“Char value: ” + grade);

        System.out.println(“Boolean value: ” + isPassed);

    }

}


Output

Byte value: 10

Short value: 1000

Int value: 100000

Long value: 100000000

Float value: 19.99

Double value: 12345.6789

Char value: A

Boolean value: true


Key Points to Remember

  1. Size:
    1. Each type has a fixed size, irrespective of the platform, ensuring portability.
  2. Efficiency:
    1. Choosing the correct type improves memory efficiency and performance.
  3. Default Type:
    1. int is the default for integer literals.
    1. double is the default for floating-point literals.
  4. Overflow:
    1. Exceeding the range of a type results in an overflow, leading to unpredictable values.

By mastering Java’s primitive data types, developers can handle basic data processing effectively and build robust programs.