Aral Code

Basics Lesson 5 of 21

Java Data Types and Type Casting

Java's eight primitive types (byte, short, int, long, float, double, char, boolean), the String reference type, and converting between types with casting and parsing.

Java has two families of types: primitive types, which store simple values directly, and reference types, like String and arrays, which refer to objects. Choosing the right type matters in Java, because each type has a fixed size and range.

The eight primitive types

TypeStoresSizeExample
byteSmall whole numbers, −128 to 1271 bytebyte age = 16;
shortWhole numbers, about ±32,0002 bytesshort year = 2026;
intWhole numbers, about ±2.1 billion4 bytesint students = 1240;
longVery large whole numbers8 byteslong population = 115_000_000L;
floatDecimals, about 7 digits accurate4 bytesfloat rate = 0.12f;
doubleDecimals, about 15 digits accurate8 bytesdouble avg = 89.75;
charOne character2 byteschar grade = 'A';
booleantrue / false—boolean passed = true;

In practice, you'll mostly use int, double, boolean and char, plus long for very big numbers. Note the L after long values and f after float values.

Integer overflow

public class Main {
    public static void main(String[] args) {
        int big = 2_147_483_647;       // the largest int
        System.out.println(big + 1);

        long bigger = 2_147_483_647L;
        System.out.println(bigger + 1);
    }
}

Output

-2147483648
2147483648

When an int goes past its limit, it "wraps around" to a large negative number, with no error. That's called overflow. Use long when numbers can get very large, like the Philippine population or amounts in centavos.

Integer division

public class Main {
    public static void main(String[] args) {
        System.out.println(7 / 2);
        System.out.println(7.0 / 2);
        System.out.println(7 % 2);
    }
}

Output

3
3.5
1

Strings are objects

String (capital S) isn't a primitive; it's a class. That's why it has methods like length() and toUpperCase(). Strings get their own lesson.

Type casting

Widening (small type → bigger type) happens automatically, because no information is lost:

int score = 95;
double d = score;      // 95.0, automatic

Narrowing (bigger → smaller) needs an explicit cast, written as the type in parentheses, because data might be lost:

public class Main {
    public static void main(String[] args) {
        double average = 89.99;
        int whole = (int) average;          // cuts off the decimals
        System.out.println(whole);

        int total = 264;
        double avg = (double) total / 3;    // cast before dividing
        System.out.println(avg);

        System.out.println(Math.round(89.5));   // proper rounding
        char letter = (char) 65;
        System.out.println(letter);
    }
}

Output

89
88.0
90
A

Notice that (int) 89.99 gives 89, not 90. Casting truncates (cuts off); use Math.round() to round. Characters are stored as numbers, so (char) 65 is 'A'.

Converting text to numbers

Text like "85" can't be cast to a number. Use the parse methods:

public class Main {
    public static void main(String[] args) {
        int a = Integer.parseInt("85");
        double b = Double.parseDouble("12.50");
        String c = String.valueOf(2026);     // number to text
        String d = "" + 99;                  // quick trick, also text

        System.out.println(a + 1);
        System.out.println(b * 2);
        System.out.println(c + 1);
        System.out.println(d.length());
    }
}

Output

86
25.0
20261
2

If the text isn't a valid number, like Integer.parseInt("abc"), Java throws a NumberFormatException. You'll learn to handle it in the Exceptions lesson.

Wrapper classes

Each primitive has a matching class: Integer, Double, Character, Boolean and so on. They provide helpful methods and constants, and they're needed for collections like ArrayList<Integer>:

System.out.println(Integer.MAX_VALUE);       // 2147483647
System.out.println(Character.isDigit('7'));  // true
System.out.println(Character.toUpperCase('a'));  // A

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What is 10 / 4 in Java?
2. What does (int) 7.9 give?
3. Which converts the text "42" to an int?
4. Which type should you use for a number larger than 3 billion?

By Sir Reyel · Last updated 2026-12-04