Useful Java Built-in Methods
1. Math Class Methods
- Math.abs(x): Absolute value. Example: Math.abs(-5) => 5
- Math.max(a, b): Larger of two values. Example: Math.max(3, 8) => 8
- Math.min(a, b): Smaller of two values. Example: Math.min(3, 8) => 3
- Math.pow(x, y): Power. Example: Math.pow(2, 3) => 8.0
- Math.sqrt(x): Square root. Example: Math.sqrt(9) => 3.0
- Math.cbrt(x): Cube root. Example: Math.cbrt(27) => 3.0
- Math.round(x): Rounds to nearest int. Example: Math.round(2.6) => 3
- Math.ceil(x): Rounds up. Example: Math.ceil(2.1) => 3.0
- Math.floor(x): Rounds down. Example: Math.floor(2.9) => 2.0
- Math.random(): Random number [0.0, 1.0)
2. String Class Methods
- length(): Length of string. Example: "hello".length() => 5
- charAt(i): Char at index i. Example: "hello".charAt(1) => 'e'
- substring(i, j): Part of string i to j-1. Example: "hello".substring(1, 3) => "el"
- toLowerCase(): Convert to lower. Example: "HeLLo".toLowerCase() => "hello"
- toUpperCase(): Convert to upper. Example: "hello".toUpperCase() => "HELLO"
- equals(): Compare strings. Example: "a".equals("A") => false
- equalsIgnoreCase(): Case-insensitive compare. Example: "a".equalsIgnoreCase("A") => true
- contains(): Check if contains. Example: "abc".contains("b") => true
- replace(): Replace chars. Example: "apple".replace("p", "b") => "abble"
3. Arrays Utility Methods
- Arrays.sort(arr): Sort the array.
- Arrays.toString(arr): Convert array to string format.
- Arrays.copyOf(arr, n): Copy first n elements.