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
| Type | Stores | Size | Example |
|---|---|---|---|
byte | Small whole numbers, −128 to 127 | 1 byte | byte age = 16; |
short | Whole numbers, about ±32,000 | 2 bytes | short year = 2026; |
int | Whole numbers, about ±2.1 billion | 4 bytes | int students = 1240; |
long | Very large whole numbers | 8 bytes | long population = 115_000_000L; |
float | Decimals, about 7 digits accurate | 4 bytes | float rate = 0.12f; |
double | Decimals, about 15 digits accurate | 8 bytes | double avg = 89.75; |
char | One character | 2 bytes | char grade = 'A'; |
boolean | true / 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. 🙂
By Sir Reyel · Last updated 2026-12-04