Strings in Java — Quick
Presentation
Key concepts, examples, common
pitfalls
Prsented by: Dev
Outline
• What is a String?
• Immutability & String Pool
• Creating Strings
• Common Methods (length, charAt, substring, etc.)
• Concatenation and StringBuilder/StringBuffer
• Comparison (== vs equals)
• Important methods and examples
• Common pitfalls & best practices
• Interview-style quick questions
What is a String?
• An object that represents a sequence of characters.
• In Java, [Link] is immutable and widely used.
Immutability & String Pool
• String objects cannot be changed after creation.
• Operations produce new String objects.
• String pool (intern pool) stores literals to reuse memory.
Creating Strings
• Using string literal: String s = "hello"; // stored in pool
• Using constructor: String s2 = new String("hello"); // new
object
• Using valueOf, concat, substring, etc. all create new Strings
Common Methods
• length(), charAt(int), substring(start,end)
• indexOf(...), lastIndexOf(...), contains(...), startsWith(...),
endsWith(...)
• toUpperCase(), toLowerCase(), trim(), replace()
Concatenation & Performance
• Using + operator creates new String objects (compiler may
optimize constants).
• Use StringBuilder for many appends: faster and mutable.
• StringBuffer is thread-safe (synchronized) but slower.
Example:
StringBuilder sb = new StringBuilder();
[Link]("Hello");
[Link](" World");
String result = [Link]();
Comparison: == vs equals()
• == compares references (are they the same object?).
• equals() compares content (characters) of the String.
• Use equalsIgnoreCase() to compare ignoring case.
Example:
String a = "test";
String b = new String("test");
boolean refEqual = (a == b); // false
boolean contentEqual = [Link](b); // true
Important Methods (examples)
// length and charAt
String s = "Java";
int n = [Link](); // 4
char c = [Link](1); // 'a'
// substring
String sub = [Link](1,3); // "av"
// split
String[] parts = "a,b,c".split(","); // ["a","b","c"]
// replace
String r = "hello".replace("l", "L"); // heLLo
Immutability Example
• Methods like toUpperCase() return new String; original
unchanged.
String s = "hello";
String t = [Link]();
// s is still "hello"; t is "HELLO"
String Pool & intern()
• Literal strings go to the pool. new String(...) creates a separate
object.
• intern() returns a pooled reference for equal content.
String a = "hey";
String b = new String("hey");
String c = [Link]();
// a == c is true, a == b is false
Common Pitfalls
• Using == to compare content (buggy).
• Assuming String is mutable — causes extra allocations.
• Heavy concatenation in loops without StringBuilder causes
slow code.
• NullPointerException when calling methods on null Strings.
Best Practices
• Use equals() for content comparison.
• Prefer StringBuilder for repetitive concatenation.
• Avoid unnecessary new String(...) calls.
• Check for null or use [Link](a, b) to avoid NPEs.
Interview Quick Questions
• 1) Why is String immutable in Java?
• 2) How does string pool improve performance?
• 3) Difference between String, StringBuilder, StringBuffer?
• 4) What does intern() do?
Summary
• Strings are immutable objects representing character
sequences.
• Use correct comparison methods and efficient concatenation.
• Be mindful of memory (string pool) and performance.