CS & IT
ENGINEERING
Java With OOPs
Java Strings
Lecture No-01 By- Aditya sir
Recap of Previous Lecture
Topic Enum in
java
Topic
Topics to be Covered
Topic java strings
3
Topic : Introduction to Strings
• A String is an immutable sequence of characters strings
• Stored as a char[] internally H
• Implements Serializable, Comparable<String>, CharSequence
• Immutable: once created, content cannot change 3 4
•
Oo
Widely used for text processing and I/O
Aditya Delita
Topic : Creating Strings
mostusedSyn
• Literal: "Hello, World!" (pooled)
• Constructor: new String("text") (distinct object)
• From char array: new String(new char[]{'a','b'}) a
b
• From byte array: new String(bytes, charset)
• Use literals when possible for memory efficiency
at
Topic : String Pool & Immutability
• Java maintains a String Constant Pool in the heap
Bonus
• Literals are interned; duplicates refer to same object
• intern() method adds runtime strings to pool
• Immutability enables safe sharing across threads
Example:
m
• String a = "foo";
• String b = "foo";
• [Link](a == b); // true
Topic : Common String Methods
•
•
length(): number of characters
charAt(int index): character at position
or Aditya FEET
• substring(begin, end): slice of string
o
• indexOf(String/char): first occurrence Jain
• contains(CharSequence), startsWith(), endsWith() S ñ Aditya
GO
ox
W
[Link] 2
i
D
Topic : Comparing Strings
• equals(): content equality IN
1
• equalsIgnoreCase(): ignores case differences
• compareTo(): lexicographical comparison
• Don’t use == for content comparison
Example:
d 91
•
•
G
"abc".equals("ABC"); // false
"abc".equalsIgnoreCase("ABC");// true 2
A I
4
Topic : String Concatenation
• Using + operator (compiler uses StringBuilder)
• concat() method
0
• For loops: prefer StringBuilder for efficiency
Example:
• String result = a + b + c;
• // equivalent to new StringBuilder().append(a).append(b).append(c).toString();
e
Topic : StringBuilder & StringBuffer
• StringBuilder: mutable, not synchronized (faster)
• StringBuffer: thread-safe, synchronized (slower) T
• Methods: append(), insert(), delete(),
ii_ff reverse()
• Use for heavy concatenation/manipulation
Example:
• StringBuilder sb = new StringBuilder();
• [Link]("Hello").append(" ").append("Java");
• String s = [Link]();
AT HaloJant
THANK - YOU