Java Strings π¶
Mentor's Note: A String is just a Chain of Characters. Imagine it like a Toy Trainβeach letter is a cabin, and they are all hooked together to form a word or sentence! π‘
π The Scenario: The Toy Train π¶
Imagine you are building a toy train set.
- The Characters (The Cabins): Each letter ('H', 'e', 'l', 'l', 'o') is an individual Cabin. π¦
- The Index (The Seat Number): Every cabin has a number, but computers start counting from 0. So the first cabin is at Seat 0, the second is at Seat 1, and so on. π’
- Concatenation (The Hook): You can "Hook" two trains together using the
+operator. π - The Result: You have a complete word that you can read, measure, and even cut into pieces (Substrings)! β
π¨ Visual Logic: The Index Map¶
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Character | J | a | v | a | ! |
graph LR
A[0: J] --- B[1: a]
B --- C[2: v]
C --- D[3: a]
D --- E[4: !]
π Concept Explanation¶
1. Essential String Methods π οΈ¶
Strings are Objects, which means they come with their own "Tools" (Methods).
- length(): Tells you how many cabins are in the train. π
- toUpperCase() / toLowerCase(): Changes the look of the cabins. π¨
- indexOf("a"): Tells you the seat number of a specific letter. π
- substring(start, end): Cuts out a specific part of the train. βοΈ
2. Concatenation (Joining) π¶
You can join strings using + or the .concat() method.
- Rule: String + Number = String (e.g., "Age: " + 20 becomes "Age: 20").
3. Immutability (Unchangeable) π¶
Once a String is created, it CANNOT be changed. If you try to change it, Java actually builds a New Train and gives it to you. The old one stays exactly the same.
π» Implementation: The Train Lab¶
// π Scenario: Building a "Welcome" train
// π Action: Using common string methods
public class Main {
public static void main(String[] args) {
String text = "Hello Java!";
// π Length (Total Cabins)
System.out.println("Total: " + text.length()); // 11
// π Finding a Seat (Index)
System.out.println("Seat of 'J': " + text.indexOf("J")); // 6
// βοΈ Cutting a piece (Substring)
// Note: Start at 6, end BEFORE 10
System.out.println("Part: " + text.substring(6, 10)); // Java
// π Concatenation (Hooking)
String name = "Vishnu";
System.out.println("Welcome " + name + "! π");
}
}
π Sample Dry Run (Methods)¶
| Method | Logic | Computer's Action | Result |
|---|---|---|---|
length() |
Count cabins | 1, 2, 3, 4, 5... | 11 π |
indexOf("e") |
Find seat for 'e' | Look at 0, 1 (Found!) | 1 π |
toUpperCase() |
Paint all cabins | Replace letters | "HELLO JAVA!" π¨ |
π Technical Analysis: The String Pool π¶
To save memory, Java has a "Shared Yard" (The String Pool). If you create two strings with the same word ("Hello"), Java doesn't build two trainsβit points both labels to the Same Train! This is why Strings are immutableβso one person doesn't accidentally change the train for everyone else! π§
π― Practice Lab π§ͺ¶
Task: The Email Formatter
Task: Create a variable for userName. Convert it to all lowercase, remove any spaces using .trim(), and add @gmail.com at the end.
Goal: Use multiple string methods in a row! π‘
π‘ Interview Tip π¶
"Interviewers often ask: 'How do you compare two strings?' Answer: Never use
==. Always use.equals(). Why? Because==checks if they are the same object, but.equals()checks if they have the same characters. β οΈ"
π‘ Pro Tip: "Indices start at 0, but lengths start at 1. This is the #1 cause of bugs in beginner codeβbe careful!" - Vishnu Damwala