Skip to content

Information Technology · Ch 2 — Programming

Common Library Classes — String and Math

4

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";):

MethodWhat it doesExample → result
length()number of characterss.length() → 8
charAt(i)character at index is.charAt(0) → 'C'
substring(a, b)characters from a up to (not incl.) bs.substring(0, 3) → "Com"
toUpperCase()a new all-capitals strings.toUpperCase() → "COMMERCE"
toLowerCase()a new all-small strings.toLowerCase() → "commerce"
indexOf("m")index of first match, or -1s.indexOf("m") → 2
equals(t)true if contents are identicals.equals("Commerce") → true
concat(t) / +joins two stringss.concat(" Stream") → "Commerce Stream"
trim()removes leading/trailing spaces" hi ".trim() → "hi"
replace('m','n')replaces characterss.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.

MethodWhat it doesExample → result
Math.sqrt(x)square rootMath.sqrt(81) → 9.0
Math.pow(x, y)x raised to yMath.pow(2, 5) → 32.0
Math.abs(x)absolute (positive) valueMath.abs(-7) → 7
Math.max(a, b)the largerMath.max(3, 9) → 9
Math.min(a, b)the smallerMath.min(3, 9) → 3
Math.round(x)nearest whole numberMath.round(3.6) → 4
Math.ceil(x)rounds upMath.ceil(3.1) → 4.0
Math.floor(x)rounds downMath.floor(3.9) → 3.0
Definition 1Standard library

The large set of ready-made, tested classes and methods that come with Java, so common tasks need not be …

Definition 2String (immutable)

A library class storing text in double quotes; its methods return a new string rather than changing the original, an …

Definition 3equals() vs ==

equals() compares the contents of two strings; == compares whether they are the same object in memory. Use `equals() …

Definition 4Math class

A library class of static mathematical methods (sqrt, pow, abs, max, min, round, random) and constants (PI), called on th …

Definition 5static method

A method that belongs to the class rather than to any object, so it is called using the class name, e. …