DMCA.com Protection Status Java Programming Lesson 7 Part-1 | String class & methods for Beginners - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson 7 Part-1 | String class & methods for Beginners

LESSON-7 STRINGS IN JAVA

Strings (String class & methods) 

✅ What is String?

A String is a sequence of characters stored as an object in Java.

Example:

String name = "Java";

Property ✅

✔ String is immutable (cannot be changed once created)


✅ String Declaration Methods

String s1 = "Hello"; // Recommended String s2 = new String("World"); // Object form

✅ String Input from User

Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.println("Welcome " + name);

✅ Most Important String Methods

MethodMeaningExample Output
length()  Count characters     "Java".length() → 4
toUpperCase()  Convert to capital     "java".toUpperCase()
toLowerCase()  Convert to small     "HELLO".toLowerCase()
charAt(i)  Get character     "Java".charAt(1) → 'a'
equals()  Compare strings     "A".equals("a") → false
contains()  Substring present?     "coding".contains("cod")
replace()  Replace characters     "java".replace('a','o')
substring()  Cut part     "Hello".substring(1,4)

✅ Practical Example

public class StringExample { public static void main(String[] args) { String s = "Java Programming"; System.out.println("Length: " + s.length()); System.out.println("Uppercase: " + s.toUpperCase()); System.out.println("Character at index 5: " + s.charAt(5)); System.out.println("Contains 'Program': " + s.contains("Program")); System.out.println("Substring(5, 16): " + s.substring(5, 16)); } }

✅ Output:

Length: 16 Uppercase: JAVA PROGRAMMING Character at index 5: P Contains 'Program': true Substring(5, 16): Programming

✅ Compare Strings: equals() vs ==

String a = "Java"; String b = "Java"; String c = new String("Java"); System.out.println(a == b); // true (same memory pool) System.out.println(a == c); // false System.out.println(a.equals(c)); // true (same content)

📌 Use equals() to compare text values ✔


📝 Exercise for You ✅

Create a String program:

Input from user:
✅ Full Name

Program should:
✔ Print total characters
✔ Print uppercase version
✔ Print first character
✔ Check whether name contains "a" or "A"

Example Output:

Enter Name: Jitu Hasan Total Characters: 10 Uppercase: JITU HASAN First Letter: J Contains 'a'? true

Reply with your:
✅ Code
✅ Output

I’ll check & give grading ✅🔥


🎯 Quick Quiz

1️⃣ String is:
A) Primitive
B) Non-Primitive

2️⃣ Which method compares text values?
A) ==
B) equals()
C) compare()

Reply like:
1 → ?, 2 → ?


✅ Lesson-7 Part-1 Completed!


➡ NEXT Lesson-7 Part-2: Functions/Methods

No comments

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

Theme images by 5ugarless. Powered by Blogger.