Information Technology · Ch 2 — Programming
Common Library Classes — String and Math
Common Library Classes — String and Math
Java ships with a large collection of ready-made classes called the standard library. Instead of writing common operations yourself, you call methods that are already written and tested. Two library classes you will use constantly are String and Math.
The String class
A String stores a sequence of characters (text), written in double quotes: "Odisha". Strings are immutable — a method never changes the original string; it returns a new string. Positions (indexes) start at 0.
Common String methods (assume String s = "Commerce";):
| Method | What it does | Example → result |
|---|---|---|
length() | number of characters | s.length() → 8 |
charAt(i) | character at index i | s.charAt(0) → 'C' |
substring(a, b) | characters from a up to (not incl.) b | s.substring(0, 3) → "Com" |
toUpperCase() | a new all-capitals string | s.toUpperCase() → "COMMERCE" |
toLowerCase() | a new all-small string | s.toLowerCase() → "commerce" |
indexOf("m") | index of first match, or -1 | s.indexOf("m") → 2 |
equals(t) | true if contents are identical | s.equals("Commerce") → true |
concat(t) / + | joins two strings | s.concat(" Stream") → "Commerce Stream" |
trim() | removes leading/trailing spaces | " hi ".trim() → "hi" |
replace('m','n') | replaces characters | s.replace('m','n') → "Connerce" |
String s = "Commerce";
System.out.println(s.length()); // 8
System.out.println(s.substring(0, 3)); // Com
System.out.println(s.toUpperCase()); // COMMERCE
System.out.println(s.indexOf("m")); // 2
Important: to compare the contents of two strings, always use equals(), not ==. The == operator checks whether they are the same object in memory, which is not what you usually want.
The Math class
The Math class provides mathematical methods and two constants. Its methods are static, so you call them on the class name directly — Math.sqrt(...) — without creating an object.
| Method | What it does | Example → result |
|---|---|---|
Math.sqrt(x) | square root | Math.sqrt(81) → 9.0 |
Math.pow(x, y) | x raised to y | Math.pow(2, 5) → 32.0 |
Math.abs(x) | absolute (positive) value | Math.abs(-7) → 7 |
Math.max(a, b) | the larger | Math.max(3, 9) → 9 |
Math.min(a, b) | the smaller | Math.min(3, 9) → 3 |
Math.round(x) | nearest whole number | Math.round(3.6) → 4 |
Math.ceil(x) | rounds up | Math.ceil(3.1) → 4.0 |
Math.floor(x) | rounds down | Math.floor(3.9) → 3.0 |
The large set of ready-made, tested classes and methods that come with Java, so common tasks need not be …
A library class storing text in double quotes; its methods return a new string rather than changing the original, an …
equals() compares the contents of two strings; == compares whether they are the same object in memory. Use `equals() …
A library class of static mathematical methods (sqrt, pow, abs, max, min, round, random) and constants (PI), called on th …
A method that belongs to the class rather than to any object, so it is called using the class name, e. …