← Back to Week 2 Hub

Week 2 Cheat Sheet

Strings, objects, loops, and arrays — keep this open while you code

1. String Methods

MethodWhat It DoesExample
length() Returns character count "Hello".length() 5
trim() Removes leading/trailing spaces " Hi ".trim() "Hi"
toUpperCase() All uppercase "abc".toUpperCase() "ABC"
toLowerCase() All lowercase "ABC".toLowerCase() "abc"
charAt(i) Character at index "Hello".charAt(1) 'e'
indexOf(s) Position of substring (-1 if not found) "Hello".indexOf("ll") 2
substring(start) From start to end "Hello".substring(2) "llo"
substring(start, end) From start to end (exclusive) "Hello".substring(1,4) "ell"
split(regex) Split into array "a,b,c".split(",") ["a","b","c"]
equals(s) Compare content s1.equals(s2)
equalsIgnoreCase(s) Compare ignoring case "ABC".equalsIgnoreCase("abc") true
startsWith(s) Starts with prefix? "Hello".startsWith("He") true
endsWith(s) Ends with suffix? "Hello".endsWith("lo") true
isEmpty() Is empty string? "".isEmpty() true
Remember: Always use .equals() to compare Strings — never ==. The == operator compares memory references, not content.

2. StringBuilder

StringBuilder sb = new StringBuilder(); sb.append("Hello"); sb.append(" World"); String result = sb.toString(); // "Hello World"
Tip: You can chain appends: sb.append("A").append("B").append("C");

3. Type Conversion (Parsing)

String to Number
int num = Integer.parseInt("42"); double d = Double.parseDouble("3.14");
String to Date
// Default format: yyyy-MM-dd LocalDate date = LocalDate.parse("2025-07-15"); // Custom format DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate date2 = LocalDate.parse("07/15/2025", fmt);
Watch out: If the String is not a valid number, parseInt() and parseDouble() will throw a NumberFormatException.

4. Classes & Objects

public class Employee { // Fields (instance variables) private String name; private double salary; // Constructor public Employee(String name, double salary) { this.name = name; this.salary = salary; } // Getter public String getName() { return name; } // Setter public void setName(String name) { this.name = name; } // Method overloading — same name, different parameters public void giveRaise(double amount) { salary += amount; } public void giveRaise(double amount, String reason) { salary += amount; System.out.println("Raise for: " + reason); } }
Creating and using objects:
Employee emp = new Employee("Alice", 55000); System.out.println(emp.getName()); // "Alice" emp.giveRaise(3000); // calls first version emp.giveRaise(3000, "promotion"); // calls overloaded version
Tip: Fields are private, accessed through public getters/setters. This is called encapsulation.

5. Loops

while — check condition first
while (count < 5) { System.out.println(count); count++; }
do/while — runs at least once
do { System.out.println("Enter choice: "); choice = scanner.nextInt(); } while (choice != 0);
for — known iteration count
for (int i = 0; i < 10; i++) { System.out.println(i); }
for-each — iterate over array/collection
for (String name : names) { System.out.println(name); }
Loop control:

6. Arrays

Declaring arrays
// With values int[] scores = {90, 85, 92, 78}; // With size (default values: 0 for int, null for objects) String[] names = new String[5];
Accessing elements
scores[0] // first element: 90 scores[3] // last element: 78 scores[1] = 95; // update second element scores.length // 4 (no parentheses!)
Looping through an array
// for loop for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); } // for-each (simpler when you don't need the index) for (int score : scores) { System.out.println(score); }
Useful utility
import java.util.Arrays; Arrays.sort(scores); // sorts in place: [78, 85, 90, 92]
Watch out: Array indices start at 0. Accessing scores[4] on a 4-element array throws ArrayIndexOutOfBoundsException.
Tip: array.length is a field (no parentheses), but String.length() is a method (with parentheses).

Week 2 — Strings, Objects, Loops & Arrays — YearUp Spring 2026

Glossary / Key Terms →