Master Java primitives, wrapper classes, arithmetic and boolean expressions, Math API, precedence rules, type conversions, and casting for the OCP 21 exam.
Table of Contents
1. Primitive Types
Java has eight primitive data types that represent simple values. These types are not objects and are stored directly in memory.
1.1 Numeric Primitives
Integer Types
- byte: 8-bit signed integer (-128 to 127)
- short: 16-bit signed integer (-32,768 to 32,767)
- int: 32-bit signed integer (-2³¹ to 2³¹-1)
- long: 64-bit signed integer (-2⁶³ to 2⁶³-1)
Floating-Point Types
- float: 32-bit IEEE 754 floating-point
- double: 64-bit IEEE 754 floating-point (default for decimal literals)
1.2 Non-Numeric Primitives
- char: 16-bit Unicode character (0 to 65,535)
- boolean: true or false (size is JVM-dependent)
byte b = 100;
short s = 1000;
int i = 100000;
long l = 10000000000L; // L suffix required for long literals
float f = 3.14f; // f suffix required for float literals
double d = 3.14159; // default for decimal literals
char c = 'A';
boolean flag = true;
2. Wrapper Classes
Wrapper classes provide object representations of primitive types. Each primitive type has a corresponding wrapper class in the java.lang package.
| Primitive | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
2.1 Autoboxing and Unboxing
Java automatically converts between primitives and their wrapper classes:
- Autoboxing: Automatic conversion from primitive to wrapper class
- Unboxing: Automatic conversion from wrapper class to primitive
// Autoboxing: int to Integer
Integer i = 42; // Compiler converts: Integer i = Integer.valueOf(42);
// Unboxing: Integer to int
int value = i; // Compiler converts: int value = i.intValue();
// Collections require wrapper classes
List<Integer> numbers = new ArrayList<>();
numbers.add(10); // Autoboxing: int to Integer
2.2 Wrapper Class Methods
Wrapper classes provide useful static methods:
// Parsing strings to primitives
int num = Integer.parseInt("123");
double d = Double.parseDouble("3.14");
boolean b = Boolean.parseBoolean("true");
// Converting primitives to strings
String s1 = Integer.toString(42);
String s2 = String.valueOf(42);
// Getting min/max values
int max = Integer.MAX_VALUE; // 2147483647
int min = Integer.MIN_VALUE; // -2147483648
3. Arithmetic Expressions
Java supports standard arithmetic operations on numeric types.
3.1 Arithmetic Operators
- +: Addition
- -: Subtraction
- *: Multiplication
- /: Division
- %: Modulo (remainder)
- ++: Increment (prefix and postfix)
- --: Decrement (prefix and postfix)
int a = 10;
int b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3 (integer division)
int remainder = a % b; // 1
// Prefix vs Postfix
int x = 5;
int y = ++x; // x = 6, y = 6 (increment then assign)
int z = x++; // x = 7, z = 6 (assign then increment)
3.2 Division and Modulo
int result1 = 10 / 3; // 3 (integer division)
double result2 = 10.0 / 3; // 3.3333333333333335
double result3 = 10 / 3.0; // 3.3333333333333335
double result4 = (double) 10 / 3; // 3.3333333333333335
int mod1 = 10 % 3; // 1
int mod2 = -10 % 3; // -1 (sign follows dividend)
int mod3 = 10 % -3; // 1
4. Boolean Expressions
Boolean expressions evaluate to true or false.
4.1 Relational Operators
- ==: Equal to
- !=: Not equal to
- <: Less than
- <=: Less than or equal to
- >: Greater than
- >=: Greater than or equal to
4.2 Logical Operators
- &&: Logical AND (short-circuit)
- ||: Logical OR (short-circuit)
- !: Logical NOT
- &: Bitwise AND (non-short-circuit)
- |: Bitwise OR (non-short-circuit)
- ^: XOR (exclusive OR)
int a = 5;
int b = 10;
boolean c = true;
boolean d = false;
// Relational operators
boolean eq = (a == b); // false
boolean ne = (a != b); // true
boolean lt = (a < b); // true
// Logical operators
boolean and = c && d; // false
boolean or = c || d; // true
boolean not = !c; // false
// Short-circuit evaluation
boolean result = (a < 10) && (b++ > 5); // b is incremented
boolean result2 = (a > 10) && (b++ > 5); // b is NOT incremented
&& and || use short-circuit evaluation. If the left operand determines the result, the right operand is not evaluated.
5. Math API
The java.lang.Math class provides static methods for common mathematical operations.
5.1 Common Math Methods
// Absolute value
int abs1 = Math.abs(-5); // 5
double abs2 = Math.abs(-3.14); // 3.14
// Maximum and minimum
int max = Math.max(10, 20); // 20
int min = Math.min(10, 20); // 10
// Power and square root
double pow = Math.pow(2, 3); // 8.0
double sqrt = Math.sqrt(16); // 4.0
// Rounding
long round1 = Math.round(3.6); // 4
long round2 = Math.round(3.4); // 3
double ceil = Math.ceil(3.2); // 4.0
double floor = Math.floor(3.8); // 3.0
// Trigonometric functions
double sin = Math.sin(Math.PI / 2); // 1.0
double cos = Math.cos(0); // 1.0
// Random number (0.0 to 1.0)
double random = Math.random();
int randomInt = (int) (Math.random() * 100); // 0 to 99
5.2 Math Constants
Math.PI: π (approximately 3.141592653589793)Math.E: e (approximately 2.718281828459045)
6. Operator Precedence Rules
Operators are evaluated in a specific order based on precedence. Higher precedence operators are evaluated first.
| Precedence | Operators | Associativity |
|---|---|---|
| Highest | () [] . | Left to Right |
| ++ -- ! ~ + - (unary) | Right to Left | |
| * / % | Left to Right | |
| + - | Left to Right | |
| << >> >>> | Left to Right | |
| < <= > >= instanceof | Left to Right | |
| == != | Left to Right | |
| & | Left to Right | |
| ^ | Left to Right | |
| | | Left to Right | |
| && | Left to Right | |
| Lowest | || | Left to Right |
int result = 2 + 3 * 4; // 14 (multiplication first)
int result2 = (2 + 3) * 4; // 20 (parentheses override)
boolean b = 5 > 3 && 10 < 20; // true (relational before logical)
7. Type Conversions
Java performs automatic type conversions in certain situations.
7.1 Widening Conversions (Automatic)
Widening conversions occur automatically when converting to a larger type:
- byte → short → int → long → float → double
- char → int → long → float → double
byte b = 100;
int i = b; // Automatic widening: byte to int
long l = i; // Automatic widening: int to long
float f = l; // Automatic widening: long to float
double d = f; // Automatic widening: float to double
char c = 'A';
int charValue = c; // Automatic widening: char to int (65)
7.2 Narrowing Conversions (Require Casting)
Narrowing conversions require explicit casting and may result in data loss:
- double → float → long → int → short → byte
- double → float → long → int → char
8. Casting
Casting explicitly converts one type to another.
8.1 Primitive Casting
// Narrowing cast (explicit)
double d = 3.14159;
int i = (int) d; // 3 (truncates decimal part)
float f = (float) d; // 3.14159f
// Widening cast (optional, but can be explicit)
byte b = 100;
int i2 = (int) b; // Optional, but valid
// Char to int and back
char c = 'A';
int charCode = (int) c; // 65
char c2 = (char) 65; // 'A'
// Potential overflow
int large = 200;
byte small = (byte) large; // -56 (overflow due to byte range)
8.2 Object Casting
Object casting is used with inheritance hierarchies:
// Upcasting (implicit)
String str = "Hello";
Object obj = str; // Automatic upcast
// Downcasting (explicit, may throw ClassCastException)
Object obj2 = "World";
String str2 = (String) obj2; // Explicit cast required
// Safe casting with instanceof check
if (obj instanceof String) {
String str3 = (String) obj; // Safe cast
}
9. Exam Key Points
Critical Concepts for OCP 21 Exam:
- Primitive Types: Know all 8 primitives, their sizes, ranges, and default values
- Wrapper Classes: Understand autoboxing, unboxing, and when they occur
- Integer Division: Remember that
10 / 3equals3, not3.33 - Modulo Operator: Sign follows the dividend:
-10 % 3is-1 - Short-Circuit Evaluation:
&&and||may not evaluate the right operand - Operator Precedence: Multiplication before addition, relational before logical
- Widening Conversions: Automatic, no data loss (byte → int → long → float → double)
- Narrowing Conversions: Require explicit cast, may cause data loss or overflow
- Math API: Know common methods like
Math.abs(),Math.max(),Math.random() - Prefix vs Postfix:
++xincrements then uses,x++uses then increments - Long and Float Literals: Use
Lsuffix for long,fsuffix for float - Wrapper Class Parsing:
Integer.parseInt(),Double.parseDouble(), etc.
0 Comments