WikiGalaxy

Personalize

Java Data Types

Primitive Data Types

Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean. These types serve as the building blocks of data manipulation in Java.

byte

The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127. It is used to save memory in large arrays.


public class ByteExample {
    public static void main(String[] args) {
        byte b = 100;
        System.out.println("Byte Value: " + b);
    }
}
    

Console Output:

Byte Value: 100

short

short

The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767. It can also be used to save memory as a byte data type.


public class ShortExample {
    public static void main(String[] args) {
        short s = 10000;
        System.out.println("Short Value: " + s);
    }
}
    

Console Output:

Short Value: 10000

int

int

The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2^31 and a maximum value of 2^31-1. Generally used as the default data type for integral values unless there is a concern about memory.


public class IntExample {
    public static void main(String[] args) {
        int i = 100000;
        System.out.println("Int Value: " + i);
    }
}
    

Console Output:

Int Value: 100000

long

long

The long data type is a 64-bit two's complement integer. It has a minimum value of -2^63 and a maximum value of 2^63-1. Used when a wider range than int is needed.


public class LongExample {
    public static void main(String[] args) {
        long l = 100000L;
        System.out.println("Long Value: " + l);
    }
}
    

Console Output:

Long Value: 100000

float

float

The float data type is a single-precision 32-bit IEEE 754 floating point. Primarily used to save memory in large arrays of floating point numbers.


public class FloatExample {
    public static void main(String[] args) {
        float f = 234.5f;
        System.out.println("Float Value: " + f);
    }
}
    

Console Output:

Float Value: 234.5

double

double

The double data type is a double-precision 64-bit IEEE 754 floating point. Generally used as the default data type for decimal values.


public class DoubleExample {
    public static void main(String[] args) {
        double d = 123.4;
        System.out.println("Double Value: " + d);
    }
}
    

Console Output:

Double Value: 123.4

char

char

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).


public class CharExample {
    public static void main(String[] args) {
        char c = 'A';
        System.out.println("Char Value: " + c);
    }
}
    

Console Output:

Char Value: A

boolean

boolean

The boolean data type has only two possible values: true and false. This data type is used for simple flags that track true/false conditions.


public class BooleanExample {
    public static void main(String[] args) {
        boolean isJavaFun = true;
        System.out.println("Boolean Value: " + isJavaFun);
    }
}
    

Console Output:

Boolean Value: true

String (Non-primitive)

String

Although not a primitive data type, the String class is widely used in Java. Strings are objects that represent sequences of characters and are used to store text.


public class StringExample {
    public static void main(String[] args) {
        String greeting = "Hello, World!";
        System.out.println("String Value: " + greeting);
    }
}
    

Console Output:

String Value: Hello, World!

Array (Non-primitive)

Array

Arrays are objects in Java that store multiple values of the same type. They are useful for organizing data and performing operations on collections of data.


public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        System.out.println("Array Value: " + numbers[0]);
    }
}
    

Console Output:

Array Value: 1

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025