Data Types
Primitive Types
The Building Blocks
You're building a user profile: age (whole number), height (decimal), first initial (single letter), premium member (yes/no). Each piece of data needs the right container - that's what primitive types are for.
Personal profile data
Store different types of information about a person.
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
public class Profile {
public static void main(String[] args) {
int age = ;
double height = ;
char initial = ;
boolean isStudent = ;
System.out.println("Age: " + age);
System.out.println("Height: " + height + "m");
System.out.println("Initial: " + initial);
System.out.println("Student: " + isStudent);
}
}
Each type serves a purpose: int for count, double for measurement,
char for single character, boolean for yes/no.
Integer overflow
What happens when a number gets too big for its type?
public class Overflow {
public static void main(String[] args) {
int bigNumber = 2000000000;
int doubled = bigNumber * 2;
System.out.println("Original: " + bigNumber);
System.out.println("Doubled: " + doubled);
}
}
int can only hold values up to about 2 billion. Beyond that, it wraps around!
Floating point precision
Decimal numbers aren't always exact. This can surprise you.
public class Precision {
public static void main(String[] args) {
double a = 0.1;
double b = 0.2;
double sum = a + b;
System.out.println("0.1 + 0.2 = " + sum);
System.out.println("Equals 0.3? " + (sum == 0.3));
// For money, use integer cents instead
int cents1 = 10;
int cents2 = 20;
int totalCents = cents1 + cents2;
System.out.println("10 + 20 cents = " + totalCents);
}
}
0.1 + 0.2 isn't exactly 0.3 - floating point has tiny rounding errors.
Boolean flags
Booleans represent states and conditions - very useful for tracking status.
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
public class Booleans {
public static void main(String[] args) {
boolean isStudent = ;
boolean hasLicense = ;
int age = ;
boolean isAdult = age >= 18;
boolean canDrive = isAdult && hasLicense;
boolean getsDiscount = isStudent || age < 18;
System.out.println("Adult: " + isAdult);
System.out.println("Can drive: " + canDrive);
System.out.println("Gets discount: " + getsDiscount);
}
}
Use descriptive names: isStudent, hasLicense, isPremiumMember.
Type ranges
Each type has minimum and maximum values it can hold.
public class Ranges {
public static void main(String[] args) {
System.out.println("=== Integer Types ===");
System.out.println("byte: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
System.out.println("short: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
System.out.println("int: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("long: " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
System.out.println("\n=== Floating Types ===");
System.out.println("float max: " + Float.MAX_VALUE);
System.out.println("double max: " + Double.MAX_VALUE);
System.out.println("\n=== Character ===");
System.out.println("char: 0 to " + (int)Character.MAX_VALUE);
}
}
Choose the smallest type that fits your data to save memory in large programs.
Exercise: OtherIntTypes.java
Explore other integer types: byte (tiny), short (small), long (huge)