Data Types in Java

Data Types in Java

Java has two categories of data types: primitive types and reference types.

Primitive Data Types

Primitive types store actual values and have predefined sizes.

Numeric Types

NumericTypes.java
public class NumericTypes {
    public static void main(String[] args) {
        // Integer types
        byte myByte = 127;           // 8-bit, -128 to 127
        short myShort = 32767;        // 16-bit, -32,768 to 32,767
        int myInt = 2147483647;       // 32-bit, -2^31 to 2^31-1
        long myLong = 9223372036854775807L; // 64-bit, need L suffix
        
        // Floating-point types
        float myFloat = 3.14f;        // 32-bit, need f suffix
        double myDouble = 3.141592653589793; // 64-bit, default for decimals
        
        System.out.println("Byte: " + myByte);
        System.out.println("Short: " + myShort);
        System.out.println("Int: " + myInt);
        System.out.println("Long: " + myLong);
        System.out.println("Float: " + myFloat);
        System.out.println("Double: " + myDouble);
    }
}

Non-Numeric Primitive Types

NonNumericTypes.java
public class NonNumericTypes {
    public static void main(String[] args) {
        // Boolean type
        boolean isTrue = true;
        boolean isFalse = false;
        
        // Character type
        char grade = 'A';
        char unicodeChar = '\u00A9'; // Copyright symbol
        
        System.out.println("Is true: " + isTrue);
        System.out.println("Is false: " + isFalse);
        System.out.println("Grade: " + grade);
        System.out.println("Unicode: " + unicodeChar);
    }
}

Reference Data Types

Reference types store references to objects in memory.

String Class

StringExample.java
public class StringExample {
    public static void main(String[] args) {
        // String is a reference type, but behaves like a primitive
        String name = "John Doe";
        String greeting = new String("Hello");
        
        // String concatenation
        String message = name + " says " + greeting;
        
        System.out.println(message);
        
        // String methods
        System.out.println("Length: " + name.length());
        System.out.println("Uppercase: " + name.toUpperCase());
        System.out.println("Contains 'John': " + name.contains("John"));
    }
}

Arrays

ArrayExample.java
public class ArrayExample {
    public static void main(String[] args) {
        // Array of integers
        int[] numbers = {1, 2, 3, 4, 5};
        
        // Array of strings
        String[] names = new String[3];
        names[0] = "Alice";
        names[1] = "Bob";
        names[2] = "Charlie";
        
        // Accessing array elements
        System.out.println("First number: " + numbers[0]);
        System.out.println("Second name: " + names[1]);
        System.out.println("Array length: " + numbers.length);
    }
}

Type Conversion

Automatic Type Promotion

TypePromotion.java
public class TypePromotion {
    public static void main(String[] args) {
        int intValue = 100;
        double doubleValue = intValue; // Automatic promotion
        
        System.out.println("Int: " + intValue);
        System.out.println("Double: " + doubleValue);
    }
}

Explicit Type Casting

TypeCasting.java
public class TypeCasting {
    public static void main(String[] args) {
        double doubleValue = 99.99;
        int intValue = (int) doubleValue; // Explicit casting
        
        System.out.println("Original double: " + doubleValue);
        System.out.println("Casted int: " + intValue); // Loss of decimal
    }
}
⚠️
Be careful when casting from larger types to smaller types as you may lose data!

Learn about control flow to make decisions in your programs.

Last updated on