☕ Java
Strings in Java
Strings are everywhere — usernames, emails, messages, API responses, file paths. Java treats String as a special class, not just a primitive, and understanding how it works (especially immutability) will save you from subtle bugs.
String Basics and Common Methods
String in Java is an object, not a primitive. It's immutable — once created, the characters inside can't be changed. Any operation that "modifies" a String actually creates a new one.
This might seem weird, but it has real benefits: Strings are thread-safe, they can be safely shared, and Java can cache them in a "String Pool" for memory efficiency.
Java
String s = "Hello, Java!";
System.out.println(s.length()); // 12 — number of characters
System.out.println(s.toUpperCase()); // HELLO, JAVA!
System.out.println(s.toLowerCase()); // hello, java!
System.out.println(s.charAt(0)); // H — character at index 0
System.out.println(s.substring(7, 11)); // Java — chars from index 7 to 10
System.out.println(s.contains("Java")); // true
System.out.println(s.replace("Java", "World")); // Hello, World!
System.out.println(s.trim()); // removes leading/trailing whitespace
System.out.println(s.startsWith("Hello")); // true
System.out.println(s.indexOf("Java")); // 7 — position where "Java" startsThe Golden Rule: Never Use == to Compare Strings
This is one of the most common Java bugs. == checks if two variables point to the same object in memory. .equals() checks if the content is the same. You almost always want .equals().
Java
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); // false! — different objects in memory
System.out.println(a.equals(b)); // true — same content ✓
System.out.println(a.equalsIgnoreCase("HELLO")); // true — case-insensitive compare
// String literals from the pool CAN match with ==, but don't rely on it
// Always use .equals() for content comparison — make it a habitStringBuilder — When You Need Mutable Strings
Remember, String is immutable. If you're building a string in a loop (like joining 1000 names), using + operator creates a new String object on every iteration — which hammers performance and memory.
StringBuilder is mutable. It lets you modify the same object in place, making it dramatically faster for string building operations.
Java
// Bad — creates a new String on every iteration (slow for large loops)
String result = "";
for (int i = 0; i < 1000; i++) {
result += i + ","; // 1000 new String objects created!
}
// Good — StringBuilder modifies in place
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++) {
sb.append(i).append(",");
}
String result2 = sb.toString(); // Convert to String only at the end
// Other StringBuilder methods
StringBuilder name = new StringBuilder("Hello");
name.append(" World"); // Hello World
name.insert(5, ","); // Hello, World
name.replace(0, 5, "Hi"); // Hi, World
name.delete(2, 4); // Hi World
name.reverse(); // dlroW iHRelated Topics in Java Basics
Variables in Java
A variable is just a named box in memory that holds a value. Java is strict about what goes in each box — you tell it the type upfront. Once you get this, the rest of Java clicks into place.
Data Types in Java
Java needs to know exactly what kind of data it's dealing with before it can store or process it. Integers, decimals, characters, true/false — each has its own type. Knowing which to use (and why) makes your programs efficient and bug-free.
Operators in Java
Operators are the symbols that tell Java what to do with your data — add it, compare it, flip it, combine it. You use operators in literally every line of real code, so getting comfortable with them early pays off immediately.
Control Flow Statements
Without control flow, every program would just run from top to bottom in a straight line. Control flow lets your program make decisions, repeat actions, and skip code — the difference between a calculator and an actual application.