Java Notes
Complete guide to Java data types — all 8 primitive types (byte, short, int, long, float, double, char, boolean), reference types, wrapper classes, memory sizes, and ranges with examples.
Java is a statically-typed language — every variable must have a declared type, and the compiler enforces type rules at compile time. Java has two categories of data types: primitive types (8 built-in value types) and reference types (objects, arrays, interfaces).
The Two Categories
| │ ├── Integer types | byte, short, int, long |
| │ ├── Floating-point | float, double |
| │ ├── Character | char |
| │ └── Boolean | boolean |
| ├── Classes | String, Scanner, ArrayList, etc. |
| ├── Arrays | int[], String[], etc. |
| ├── Interfaces | List, Map, Runnable, etc. |
| └── Enums | Direction, Color, etc. |
Primitive Data Types — Complete Reference
Integer Types
public class IntegerTypes {
public static void main(String[] args) {
// byte: 8 bits, range: -128 to 127
byte smallNumber = 100;
byte minByte = Byte.MIN_VALUE; // -128
byte maxByte = Byte.MAX_VALUE; // 127
// short: 16 bits, range: -32,768 to 32,767
short mediumNumber = 30000;
short minShort = Short.MIN_VALUE; // -32768
short maxShort = Short.MAX_VALUE; // 32767
// int: 32 bits, range: -2.1 billion to 2.1 billion (most common)
int regularNumber = 2_000_000_000;
int minInt = Integer.MIN_VALUE; // -2147483648
int maxInt = Integer.MAX_VALUE; // 2147483647
// long: 64 bits, range: -9.2 quintillion to 9.2 quintillion
long bigNumber = 9_223_372_036_854_775_807L; // note the L suffix!
long minLong = Long.MIN_VALUE;
long maxLong = Long.MAX_VALUE;
System.out.println("=== Integer Type Ranges ===");
System.out.printf("byte: %d to %d (1 byte)%n", minByte, maxByte);
System.out.printf("short: %d to %d (2 bytes)%n", minShort, maxShort);
System.out.printf("int: %d to %d (4 bytes)%n", minInt, maxInt);
System.out.printf("long: %d to %d (8 bytes)%n", minLong, maxLong);
}
}=== Integer Type Ranges === byte: -128 to 127 (1 byte) short: -32768 to 32767 (2 bytes) int: -2147483648 to 2147483647 (4 bytes) long: -9223372036854775808 to 9223372036854775807 (8 bytes)
Integer Literal Formats
public class IntegerLiterals {
public static void main(String[] args) {
// Decimal (base 10) — default
int decimal = 255;
// Binary (base 2) — prefix 0b or 0B
int binary = 0b11111111; // = 255
// Octal (base 8) — prefix 0
int octal = 0377; // = 255
// Hexadecimal (base 16) — prefix 0x or 0X
int hex = 0xFF; // = 255
// Underscores for readability (Java 7+)
int million = 1_000_000;
long creditCard = 1234_5678_9012_3456L;
int binary2 = 0b1010_1100_0011_1111;
System.out.println("All represent 255:");
System.out.println("Decimal: " + decimal);
System.out.println("Binary: " + binary);
System.out.println("Octal: " + octal);
System.out.println("Hex: " + hex);
System.out.println("\nMillion: " + million);
}
}All represent 255: Decimal: 255 Binary: 255 Octal: 255 Hex: 255 Million: 1000000
Floating-Point Types
public class FloatingPointTypes {
public static void main(String[] args) {
// float: 32 bits, ~7 decimal digits precision
float pi_f = 3.14159265f; // MUST use 'f' or 'F' suffix
float minFloat = Float.MIN_VALUE; // smallest positive: 1.4E-45
float maxFloat = Float.MAX_VALUE; // largest: 3.4028235E38
// double: 64 bits, ~15 decimal digits precision (DEFAULT)
double pi_d = 3.141592653589793; // no suffix needed (default)
double minDouble = Double.MIN_VALUE; // 4.9E-324
double maxDouble = Double.MAX_VALUE; // 1.7976931348623157E308
System.out.println("=== Floating-Point Types ===");
System.out.println("float pi: " + pi_f); // loses precision after 7 digits
System.out.println("double pi: " + pi_d); // precise to 15 digits
System.out.printf("float range: %e to %e%n", minFloat, maxFloat);
System.out.printf("double range: %e to %e%n", minDouble, maxDouble);
// Special values
System.out.println("\n=== Special Values ===");
System.out.println("Positive Infinity: " + Double.POSITIVE_INFINITY);
System.out.println("Negative Infinity: " + Double.NEGATIVE_INFINITY);
System.out.println("NaN: " + Double.NaN);
System.out.println("1.0 / 0.0 = " + (1.0 / 0.0)); // Infinity
System.out.println("0.0 / 0.0 = " + (0.0 / 0.0)); // NaN
System.out.println("NaN == NaN: " + (Double.NaN == Double.NaN)); // false!
}
}=== Floating-Point Types === float pi: 3.1415927 double pi: 3.141592653589793 float range: 1.401298e-45 to 3.402823e+38 double range: 4.900000e-324 to 1.797693e+308 === Special Values === Positive Infinity: Infinity Negative Infinity: -Infinity NaN: NaN 1.0 / 0.0 = Infinity 0.0 / 0.0 = NaN NaN == NaN: false
Floating-Point Precision Problem
public class PrecisionProblem {
public static void main(String[] args) {
// IMPORTANT: floats/doubles cannot represent all decimals exactly!
System.out.println("0.1 + 0.2 = " + (0.1 + 0.2));
// Expected: 0.3 — Actual: 0.30000000000000004
System.out.println("1.0 - 0.9 = " + (1.0 - 0.9));
// Expected: 0.1 — Actual: 0.09999999999999998
// SOLUTION: Use BigDecimal for financial calculations
import java.math.BigDecimal;
BigDecimal a = new BigDecimal("0.1");
BigDecimal b = new BigDecimal("0.2");
System.out.println("BigDecimal 0.1 + 0.2 = " + a.add(b)); // 0.3 exactly!
}
}0.1 + 0.2 = 0.30000000000000004 1.0 - 0.9 = 0.09999999999999998 BigDecimal 0.1 + 0.2 = 0.3
char Type
letter: A digit: 7 unicode: A numeric: A === char as number === 'A' as int: 65 'a' as int: 97 '0' as int: 48 Next letter: B Alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
boolean Type
public class BooleanType {
public static void main(String[] args) {
// boolean: only two values — true or false
boolean isJavaFun = true;
boolean isCold = false;
// Boolean expressions
int age = 20;
boolean isAdult = age >= 18; // true
boolean isTeenager = age >= 13 && age <= 19; // false
boolean canVote = isAdult && age >= 18; // true
System.out.println("Is adult: " + isAdult);
System.out.println("Is teenager: " + isTeenager);
System.out.println("Can vote: " + canVote);
// Boolean in conditions
if (isAdult) {
System.out.println("Welcome, adult user!");
}
// IMPORTANT: boolean is NOT numeric in Java
// int x = true; // ERROR: incompatible types
// boolean b = 1; // ERROR: incompatible types
// if (1) {} // ERROR: int cannot be boolean
}
}Is adult: true Is teenager: false Can vote: true Welcome, adult user!
Complete Primitive Type Summary Table
| Type | Size | Default | Range | Wrapper | Use Case |
|---|---|---|---|---|---|
byte | 1 byte | 0 | -128 to 127 | Byte | File I/O, network data |
short | 2 bytes | 0 | -32,768 to 32,767 | Short | Rarely used |
int | 4 bytes | 0 | -2.1B to 2.1B | Integer | General integers |
long | 8 bytes | 0L | ±9.2 quintillion | Long | Large numbers, timestamps |
float | 4 bytes | 0.0f | ±3.4×10³⁸ (~7 digits) | Float | Graphics, when memory matters |
double | 8 bytes | 0.0d | ±1.8×10³⁰⁸ (~15 digits) | Double | General decimals |
char | 2 bytes | \u0000 | 0 to 65,535 | Character | Single characters |
boolean | ~1 bit* | false | true/false | Boolean | Flags, conditions |
*JVM implementation-dependent; often uses 1 byte.
Reference Types
Reference types store a memory address (reference) pointing to an object on the heap:
s1 == s2: false s1.equals(s2): true a = 10 arr1[0] = 99
Wrapper Classes and Autoboxing
import java.util.ArrayList;
import java.util.List;
public class WrapperClasses {
public static void main(String[] args) {
// Wrapper classes: object versions of primitives
Integer intObj = 42; // autoboxing: int → Integer
Double doubleObj = 3.14; // autoboxing: double → Double
Boolean boolObj = true; // autoboxing: boolean → Boolean
int intVal = intObj; // unboxing: Integer → int
double doubleVal = doubleObj; // unboxing: Double → double
// Why wrappers? Collections can't hold primitives
List<Integer> numbers = new ArrayList<>(); // can't use int here!
numbers.add(10); // autoboxing: int 10 → Integer.valueOf(10)
numbers.add(20);
numbers.add(30);
int first = numbers.get(0); // unboxing: Integer → int
System.out.println("List: " + numbers);
System.out.println("First: " + first);
// Useful wrapper methods
System.out.println("\n=== Wrapper Utility Methods ===");
System.out.println("Integer.parseInt(\"123\"): " + Integer.parseInt("123"));
System.out.println("Integer.toBinaryString(255): " + Integer.toBinaryString(255));
System.out.println("Integer.toHexString(255): " + Integer.toHexString(255));
System.out.println("Double.parseDouble(\"3.14\"): " + Double.parseDouble("3.14"));
System.out.println("Integer.MAX_VALUE: " + Integer.MAX_VALUE);
System.out.println("Character.isDigit('5'): " + Character.isDigit('5'));
System.out.println("Character.toUpperCase('a'): " + Character.toUpperCase('a'));
}
}List: [10, 20, 30]
First: 10
=== Wrapper Utility Methods ===
Integer.parseInt("123"): 123
Integer.toBinaryString(255): 11111111
Integer.toHexString(255): ff
Double.parseDouble("3.14"): 3.14
Integer.MAX_VALUE: 2147483647
Character.isDigit('5'): true
Character.toUpperCase('a'): AChoosing the Right Data Type
Common Mistakes
- Using
floatfor money — floating-point types have precision issues. UseBigDecimalfor financial calculations. - Forgetting the
Lsuffix for long literals —long x = 3000000000;fails because3000000000is first treated as an int (overflow). Use3000000000L. - Forgetting the
fsuffix for float —float x = 3.14;fails because3.14is a double literal. Use3.14f. - Integer division surprise —
int result = 5 / 2;gives2, not2.5. Both operands are int, so result is int. Use5.0 / 2for decimal result. - Comparing Wrapper objects with
==—new Integer(5) == new Integer(5)isfalse(compares references). Use.equals()or unbox to primitives. - char is not String —
'A'is a char,"A"is a String. They're different types. - Overflow without warning —
byte b = 127; b++;wraps to -128 silently. Java doesn't throw overflow exceptions for primitives.
Interview Questions
Q1: What are the 8 primitive data types in Java?
Answer: The 8 primitive types are: byte (8-bit integer), short (16-bit integer), int (32-bit integer), long (64-bit integer), float (32-bit floating-point), double (64-bit floating-point), char (16-bit Unicode character), and boolean (true/false). All other types in Java are reference types.
Q2: What is the difference between primitive and reference types?
Answer: Primitive types store values directly in the variable's memory location (stack for locals). Reference types store a memory address (reference/pointer) that points to an object on the heap. Primitives are passed by value (copy), while references pass the reference by value (both variables point to the same object).
Q3: Why does 0.1 + 0.2 != 0.3 in Java?
Answer: Floating-point numbers use IEEE 754 binary representation, which cannot exactly represent many decimal fractions. 0.1 in binary is a repeating fraction (like 1/3 in decimal). The tiny representation errors accumulate, producing 0.30000000000000004. For exact decimal arithmetic, use BigDecimal.
Q4: What is autoboxing and unboxing?
Answer: Autoboxing is the automatic conversion of a primitive to its corresponding wrapper class (int → Integer). Unboxing is the reverse (Integer → int). Java performs these conversions automatically since Java 5, particularly useful with collections that can only hold objects.
Q5: What happens when an integer overflows in Java?
Answer: Java integer arithmetic wraps around silently without throwing an exception. Integer.MAX_VALUE + 1 becomes Integer.MIN_VALUE (-2147483648). This is defined behavior based on two's complement representation. To detect overflow, use Math.addExact() (Java 8+) which throws ArithmeticException, or check bounds manually.
Summary
Java's type system provides 8 primitive types for efficient value storage and reference types for objects. Understanding sizes, ranges, precision limitations (especially for floating-point), and the distinction between primitive and reference behavior is essential. Always choose the appropriate type for your data, use BigDecimal for exact decimal math, and be aware of implicit type promotions and overflow behavior.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Data Types in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, fundamentals, syntax, basics
Related Java Master Course Topics