DMCA.com Protection Status Java Programming Lesson 6 Part-2 | String Class & Methods Explained - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson 6 Part-2 | String Class & Methods Explained

LESSON - 6: STRINGS

String Class & Methods (Practical + Clear Concepts)



Working with text is one of the most common tasks in Java, and that’s exactly why Java provides a powerful and feature-rich String class. Whether you're storing a name, reading user input, processing text files, or working with APIs—Strings are everywhere.

In this lesson, we’ll explore how Strings work in Java and the most important String methods that every beginner must know.

✅ 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:

String name = "Java";

✅ Ways to Create Strings:

✅ 1️⃣ Using String Literal (Recommended)
String s1 = "Hello";
✅ 2️⃣ Using new keyword
String s2 = new String("Hello");

✅ Common Useful String Methods

MethodDescriptionExample
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 sensitivitys1.equalsIgnoreCase(s2)
trim()       Removes side spaces" Java ".trim()
substring()       Extract part of strings.substring(1,4)
replace()       Replace characterss.replace('a','o')

✅ Example Program

public class Main { public static void main(String[] args) { String txt = " Java Programming "; System.out.println("Original: '" + txt + "'"); System.out.println("Uppercase: " + txt.toUpperCase()); System.out.println("Lowercase: " + txt.toLowerCase()); System.out.println("Trimmed: '" + txt.trim() + "'"); System.out.println("Length: " + txt.length()); System.out.println("Substring(2, 6): " + txt.substring(2, 6)); System.out.println("Replace: " + txt.replace("Java", "Hello")); System.out.println("CharAt(4): " + txt.charAt(4)); System.out.println("Contains 'Pro': " + txt.contains("Pro")); } }

✅ String Comparison

❌ Wrong: == → compares memory location
✅ Correct: .equals() → compares content

String a = "Java"; String b = "Java"; System.out.println(a == b); // false (sometimes true but unsafe) System.out.println(a.equals(b)); // ✅ true

✅ Mutable Alternatives (StringBuilder / StringBuffer)

When working with many modifications, use:

StringBuilder sb = new StringBuilder("Hello"); sb.append(" Java"); System.out.println(sb);

✅ 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:

Enter name: JITU UPPER: JITU Length: 4 SUBSTRING: JIT Last Char: U

🎯 Summary of What You Learned

  • In this lesson, you learned:
  • What Strings are and why they are immutable
  • How to declare and use Strings

Essential String methods:

  • length(), charAt(), equals()
  • substring(), contains(), indexOf()
  • replace(), trim(), startsWith()

How to work with Strings using Scanner input

These methods form the foundation for text processing in Java—something you'll use in almost every program you write.

✅ Lesson-6 Completed!



➡ NEXT Lesson-7 PART-1 Strings (String class & methods)


Frequently Asked Questions (FAQs)

What is the String class used for in Java?

The String class in Java is used to store and manipulate text-based data. It provides built-in methods for tasks such as searching, splitting, replacing, comparing, and transforming strings.

Are Strings mutable or immutable in Java?

Strings in Java are immutable, meaning once a String object is created, its value cannot be changed. Any modification creates a new String object in memory.

What is the difference between equals() and == for Strings?

equals() checks if two strings have the same content, while == checks if both references point to the same memory location.

How does substring() work in Java?

The substring() method returns a portion of a string based on start and optional end index values. The original string remains unchanged because Strings are immutable.

What does toLowerCase() and toUpperCase() do?

These methods create new strings with all characters converted to lowercase or uppercase. They are useful for formatting, validation, and user input standardization.

How do I check if a string contains a specific word or character?

You can use the contains(), indexOf(), or startsWith() methods to check for words or patterns inside a string.

What is the purpose of the split() method?

The split() method divides a string into multiple parts based on a delimiter such as a space, comma, or special symbol. It returns an array of strings.

No comments

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

Theme images by 5ugarless. Powered by Blogger.