Automatically imported into every Java program. Contains the most fundamental classes: String, Math, System, Integer, Object, Thread, and all wrapper types.
Import: import java.lang.*; | Docs: Oracle JavaDoc
| Name | Signature | Description |
|---|---|---|
| System.out.println | static void println(Object x) | Print x followed by newline to stdout |
| System.out.print | static void print(Object x) | Print x without newline |
| System.out.printf | static PrintStream printf(String fmt, Object... args) | Formatted output |
| System.err.println | static void println(Object x) | Print to stderr |
| System.exit | static void exit(int status) | Terminate JVM with status code |
| System.currentTimeMillis | static long currentTimeMillis() | Return current time in milliseconds since epoch |
| System.nanoTime | static long nanoTime() | Return nanoseconds (for elapsed time measurement) |
| System.getenv | static String getenv(String name) | Get environment variable value |
| System.getProperty | static String getProperty(String key) | Get JVM system property |
| String.length | int length() | Return number of characters |
| String.charAt | char charAt(int index) | Return char at index |
| String.substring | String substring(int begin, int end) | Return substring [begin, end) |
| String.indexOf | int indexOf(String str) | Return index of first occurrence |
| String.contains | boolean contains(CharSequence s) | True if string contains s |
| String.startsWith | boolean startsWith(String prefix) | True if starts with prefix |
| String.endsWith | boolean endsWith(String suffix) | True if ends with suffix |
| String.replace | String replace(CharSequence old, CharSequence new) | Replace all occurrences |
| String.split | String[] split(String regex) | Split by regex into array |
| String.trim | String trim() | Remove leading/trailing whitespace |
| String.strip | String strip() | Unicode-aware trim (Java 11+) |
| String.toUpperCase | String toUpperCase() | Return uppercase copy |
| String.toLowerCase | String toLowerCase() | Return lowercase copy |
| String.equals | boolean equals(Object obj) | Content equality check |
| String.equalsIgnoreCase | boolean equalsIgnoreCase(String s) | Case-insensitive equality |
| String.format | static String format(String fmt, Object... args) | Format string (like printf) |
| String.valueOf | static String valueOf(Object obj) | Convert any type to String |
| String.join | static String join(CharSequence delim, CharSequence... elems) | Join with delimiter (Java 8+) |
| String.isEmpty | boolean isEmpty() | True if length() == 0 |
| String.isBlank | boolean isBlank() | True if empty or only whitespace (Java 11+) |
| String.repeat | String repeat(int count) | Return string repeated count times (Java 11+) |
| Math.abs | static double abs(double a) | Absolute value |
| Math.sqrt | static double sqrt(double a) | Square root |
| Math.pow | static double pow(double a, double b) | a raised to power b |
| Math.max | static double max(double a, double b) | Maximum of a and b |
| Math.min | static double min(double a, double b) | Minimum of a and b |
| Math.round | static long round(double a) | Round to nearest long |
| Math.floor | static double floor(double a) | Round down |
| Math.ceil | static double ceil(double a) | Round up |
| Math.random | static double random() | Random double in [0.0, 1.0) |
| Math.PI | static final double PI = 3.14159... | Mathematical constant π |
| Math.E | static final double E = 2.71828... | Euler's number e |
| Integer.parseInt | static int parseInt(String s) | Parse decimal string to int |
| Integer.parseInt | static int parseInt(String s, int radix) | Parse string in given base |
| Integer.toString | static String toString(int i) | Convert int to decimal String |
| Integer.MAX_VALUE | static final int MAX_VALUE = 2147483647 | Maximum int value |
| Integer.MIN_VALUE | static final int MIN_VALUE = -2147483648 | Minimum int value |
| Double.parseDouble | static double parseDouble(String s) | Parse string to double |
| Boolean.parseBoolean | static boolean parseBoolean(String s) | Parse 'true'/'false' string |
| StringBuilder.append | StringBuilder append(Object obj) | Append value to builder |
| StringBuilder.toString | String toString() | Return built string |
| StringBuilder.length | int length() | Return current length |
| StringBuilder.insert | StringBuilder insert(int offset, Object obj) | Insert at position |
| StringBuilder.delete | StringBuilder delete(int start, int end) | Delete [start, end) |
| StringBuilder.reverse | StringBuilder reverse() | Reverse the sequence |
Save as ClassName.java (matching the public class name), compile with javac ClassName.java, run with java ClassName.
public class JavaLangDemo {
// Custom exception
static class AgeException extends RuntimeException {
AgeException(String msg) { super(msg); }
}
static int validateAge(String input) {
int age = Integer.parseInt(input);
if (age < 0 || age > 150)
throw new AgeException("Invalid age: " + age);
return age;
}
public static void main(String[] args) {
// String operations
String s = " Hello, Java World! ";
System.out.println(s.strip());
System.out.println(s.toUpperCase().trim());
System.out.println("Contains 'Java': " + s.contains("Java"));
System.out.println("Split: " + String.join("|", s.trim().split(", ")));
// Math
System.out.printf("sqrt(2) = %.6f%n", Math.sqrt(2));
System.out.printf("pow(2,10) = %.0f%n", Math.pow(2, 10));
System.out.printf("PI = %.10f%n", Math.PI);
// StringBuilder
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 5; i++)
sb.append("item").append(i).append(" ");
System.out.println(sb.toString().strip());
System.out.println(sb.reverse().toString().strip());
// parseInt + custom exception
for (String input : new String[]{"25", "abc", "-1"}) {
try {
System.out.println("Age: " + validateAge(input));
} catch (NumberFormatException e) {
System.out.println("Not a number: " + input);
} catch (AgeException e) {
System.out.println(e.getMessage());
}
}
// System info
System.out.println("Java version: " + System.getProperty("java.version"));
System.out.println("OS: " + System.getProperty("os.name"));
System.out.println("Time ms: " + System.currentTimeMillis());
}
}
// Compile: javac JavaLangDemo.java
// Run: java JavaLangDemo