Java Programming Lesson 6 Part-2 | String Class & Methods Explained
LESSON - 6: STRINGS
String Class & Methods (Practical + Clear Concepts)
✅ Here is Lesson-6: Strings — String Class & Methods (Practical + Clear Concepts)
📘 Lesson-6: Strings in Java
✅ What is a String?
A String is a sequence of characters.
In Java, String is a Class in java.lang package and is immutable (cannot be changed once created).
Example:
✅ Ways to Create Strings:
✅ 1️⃣ Using String Literal (Recommended)
✅ 2️⃣ Using new keyword
✅ Common Useful String Methods
| Method | Description | Example |
|---|---|---|
length() | Count characters | s.length() |
charAt() | Returns character at index | s.charAt(0) |
toUpperCase() | Convert to uppercase | s.toUpperCase() |
toLowerCase() | Convert to lowercase | s.toLowerCase() |
contains() | Check substring | s.contains("Java") |
equals() | Compares values | s1.equals(s2) |
equalsIgnoreCase() | Compare without case sensitivity | s1.equalsIgnoreCase(s2) |
trim() | Removes side spaces | " Java ".trim() |
substring() | Extract part of string | s.substring(1,4) |
replace() | Replace characters | s.replace('a','o') |
✅ Example Program
✅ String Comparison
❌ Wrong: == → compares memory location
✅ Correct: .equals() → compares content
✅ Mutable Alternatives (StringBuilder / StringBuffer)
When working with many modifications, use:
✅ Faster than creating new Strings every time!
📝 Exercise for You
1️⃣ Take your name as input
2️⃣ Print:
-
Uppercase
-
Length
-
First 3 characters (substring)
-
last character (charAt)
Example Output:

No comments
Thank You For Visit My Website.
I Will Contact As Soon As Possible.