Lesson 5 of 36Article16 min
Strings & Math
String stores text. Use + to join strings (concatenation). length() returns character count.
Strings — working with text
String stores text. Use + to join strings (concatenation). length() returns character count.
Common methods: toUpperCase(), toLowerCase(), charAt(index), indexOf("text"), substring(start, end).
Real-life example: A String is a necklace of letter beads. length() counts beads. substring() takes beads from position 2 to 5.
String basics
String name = "Asha";
System.out.println(name.length()); // 4
System.out.println(name.toUpperCase()); // ASHA
System.out.println("Hello, " + name + "!"); // Hello, Asha!
System.out.println(name.charAt(0)); // AMath — Math class
Java provides Math class with static methods — no need to create an object.
Math.max, Math.min, Math.sqrt, Math.pow, Math.abs, Math.round are used daily.
Real-life example: Math class is a pocket calculator built into Java — always ready, no install needed.
Math methods
System.out.println(Math.max(10, 20)); // 20
System.out.println(Math.min(10, 20)); // 10
System.out.println(Math.sqrt(16)); // 4.0
System.out.println(Math.pow(2, 3)); // 8.0
System.out.println(Math.abs(-7)); // 7
System.out.println(Math.round(3.6)); // 4