CS & IT
ENGINEERING
Java With OOPs
Java Strings
Lecture No-01 02 By- Aditya sir
Recap of Previous Lecture
basics
Topic Jana Strings
Topic
Topics to be Covered
Topic java strings contd
3
Topic : Introduction to Strings
• A String is an immutable sequence of characters
• Stored as a char[] internally
• Implements Serializable, Comparable<String>, CharSequence
• Immutable: once created, content cannot change
• Widely used for text processing and I/O
Topic : Creating Strings
• Literal: "Hello, World!" (pooled)
• Constructor: new String("text") (distinct object)
• From char array: new String(new char[]{'a','b'})
• From byte array: new String(bytes, charset)
• Use literals when possible for memory efficiency
Topic : String Pool & Immutability
• Java maintains a String Constant Pool in the heap
• Literals are interned; duplicates refer to same object
• intern() method adds runtime strings to pool
• Immutability enables safe sharing across threads
Example:
• String a = "foo";
• String b = "foo";
• [Link](a == b); // true
Topic : Common String Methods
• length(): number of characters
• charAt(int index): character at position
• substring(begin, end): slice of string
• indexOf(String/char): first occurrence
• contains(CharSequence), startsWith(), endsWith()
Topic : Comparing Strings
• equals(): content equality
• equalsIgnoreCase(): ignores case differences
• compareTo(): lexicographical comparison
• Don’t use == for content comparison
Example:
• "abc".equals("ABC"); // false
• "abc".equalsIgnoreCase("ABC");// true
Topic : String Concatenation
• Using + operator (compiler uses StringBuilder)
• concat() method
• For loops: prefer StringBuilder for efficiency
Example:
• String result = a + b + c;
• // equivalent to new StringBuilder().append(a).append(b).append(c).toString();
Topic : StringBuilder & StringBuffer
• StringBuilder: mutable, not synchronized (faster)
• StringBuffer: thread-safe, synchronized (slower)
• Methods: append(), insert(), delete(), reverse()
• Use for heavy concatenation/manipulation
Example:
• StringBuilder sb = new StringBuilder();
• [Link]("Hello").append(" ").append("Java");
• String s = [Link]();
Topic : Immutable vs. Mutable
• Immutable String: thread-safe, simple sharing
• Mutable builders: reduce GC churn for many edits
• Don’t convert to StringBuilder unless heavy edits
• Avoid excessive substring() on large strings
Topic : Substring and Slicing
2
• substring(int beginIndex) 5 substring
• substring(int beginIndex, int endIndex)
• Returns new String who b
2M
• In Java 7+, substrings copy data (no shared char[])
Example:
• String s = "HelloWorld";
indusive
•
e
String sub = [Link](5); // "World" 5 subst 2,5
• String mid = [Link](2, 7); // "lloWo" wed
HWiid
ktnexcud[Link]
Topic : Searching and Matching
•
•
indexOf(), lastIndexOf() for simple search
matches(regex): entire string vs. pattern
okinsBinanyteerchtrx
Search
• split(regex): divide into array
• replace(old, new), replaceAll(regex, repl), replaceFirst()
Example:
• String[] parts = "a,b,c".split(",");
1 ID
criterg
lead I
Topic : Regular Expressions
• Java uses [Link] package Revisions
• Pattern and Matcher classes
• Meta-characters: \d, \w, ., *, +, ?, [], ()
Example: validate email: subjective
• Pattern p = [Link]("[\\w\\.]+@[\\w\\.]+");
• Matcher m = [Link](email);
• if ([Link]()) { … }
Lm groups
Topic : Case Conversion & Trimming
• toLowerCase(), toUpperCase() for case
• Beware of locale: use toUpperCase(Locale)
• trim(): removes leading/trailing whitespace
• strip(), stripLeading(), stripTrailing() (Java 11+)
at
Example:
• String name = " Alice ".trim(); // "Alice"
3ʳᵈ April
Stre
H fed
5
IF
WT
WT 2
Jain 6
Aditya
Topic : Formatting Strings m
• [Link]() similar to printf To 2.93796
• Format specifiers: %s, %d, %.2f, %n
1 2F
Example:
• String msg = [Link]("User %s has %d points%n", user, points);
• Java 5+ supports Formatter, MessageFormat for i18n
6 To
Hello Aditya Good Money
x̅
x̅
7 6
good
a
time
Hello name
Sop
S name time
Format Hello s Good
string
i
Topic : Joining and Splitting
• Java 8: [Link](delim, elems…) Ifresting
• Java 8 Streams: [Link]()
• Splitting with regex: split()
Splith joint
Example:
• String joined = [Link]("-", "2025", "04", "28"); // "2025-04-28"
drive
I githublink
delimiter a g
split
2025 04 28
Aditya Jain Sir PW
Topic : Conversion Between Types
• [Link]() for primitives and objects
• [Link](), [Link]() etc. HE
• toString() override for custom classes
Example:
6 123
• int x = [Link]("123");
• String s = [Link](3.14); Coverage
allcoded
lecturel
THANK - YOU