0% found this document useful (0 votes)
3 views11 pages

Java Strings 01 Class Notes

This lecture covers Java Strings, highlighting their immutability, creation methods, and the concept of the String Constant Pool. It discusses common string methods, string comparison techniques, and string concatenation using both the + operator and StringBuilder. Additionally, it contrasts StringBuilder and StringBuffer regarding mutability and synchronization for efficient string manipulation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Java Strings 01 Class Notes

This lecture covers Java Strings, highlighting their immutability, creation methods, and the concept of the String Constant Pool. It discusses common string methods, string comparison techniques, and string concatenation using both the + operator and StringBuilder. Additionally, it contrasts StringBuilder and StringBuffer regarding mutability and synchronization for efficient string manipulation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like