Java Strings- Introduction & String Methods PPT
Java Strings- Introduction & String Methods PPT
Methods
Session No.: 24
Course Name: OOPs using JAVA
Course Code: R1UC201C
Instructor Name: Rani Singh
Duration: 50 min
Date of Conduction of Class:
Galgotias University 1
Recap of Previous session
Feature 1D Array 2D Array
Grid-like (rows & columns, similar
Structure Linear list (single row of elements).
to a table).
Declaration int[] arr = new int[5]; int[][] matrix = new int[3][3];
Accessing Elements arr[index] matrix[row][column]
Stored as a contiguous block in Stored as an array of arrays, with
Storage Format
memory. rows stored sequentially.
Used for storing lists, marks of Used for matrices, images (pixel
Usage Examples students, or single-dimensional data), tables, or multi-level
datasets. structures.
Initialization {10, 20, 30, 40, 50} {{1,2}, {3,4}, {5,6}}
Can be accessed using a single Requires nested loops for traversal
Looping Mechanism
loop (for or foreach). (for inside another for).
Galgotias University 2
Opening:
Suppose you creating a
password!!
Galgotias University 3
Learning Outcomes:
Galgotias University 4
1. Introduction to Java Strings
Galgotias University 5
Introduction to Java Strings
Galgotias University 8
Learning Activity 1 - Think-Pair-Share
Galgotias University 9
Reflection-
Learning
Activity 1
Why Strings Are Immutable in Java
Security: Strings are used in sensitive places like file paths, database URLs, and network
connections. If they were mutable, someone could change them and cause serious security
issues.
Thread safety: Since strings can't be changed, multiple threads can safely share them
without worrying about synchronization.
Caching and performance: Immutable strings can be cached in the String pool, reducing
memory usage and improving speed.
Galgotias University 10
Java Strings Methods
toUpperCase()/
Converts to uppercase/ "java".toUpperCase()" →
toLowerCase()
lowecase "JAVA"
Galgotias University 12
String methods
Galgotias University 13
String methods
Galgotias University 14
String methods
Galgotias University 15
Learning Activity 2 – Hands-on
coding
String clue = "The Quick Brown Fox";
Expected String
Task Description
Method
Print the total
1. Length Check number of length()
characters.
Print the character
2. Hidden Letter charAt()
at index 4.
Extract the word
3. Word Grab "Quick" from the substring()
string.
Replace "Brown"
4. Code Swap replace()
with "Red".
Convert the entire
5. Secret Code string to toUpperCase()
uppercase.
Compare the clue
to "THE QUICK compareToIgnoreC
6. Compare Clues
BROWN FOX" and ase()
print if they match.
Check if the word
contains()
Galgotias University or 16
7. Find Clue "Fox" exists in the
Reflection-
Learning
Activity 2
Galgotias University 17
Ensure attainment of LOs in
alignment to the learning
activities: :
Galgotias University 18
Lets Conclude the session!!!!!!!
Galgotias University 19
Post Session Activity
Problem 1: Comparing Two Strings (compareTo())
Task: Write a Java program to compare two strings "apple" and "banana" using compareTo() and print the result.
Galgotias University 21
Problem 2: Replacing a Word (replace())
Task: Replace "bad" with "good" in the sentence "This is a bad idea" and
print the new sentence.
Galgotias University 22
Problem 3: Splitting a CSV String (split())
Task: Split the string "John,25,Engineer" into separate values and print
them.
Galgotias University 23
Problem 4: Replacing Multiple Characters (replace())
Task: Replace all spaces in the string "Hello World Java" with "-" and print
the result.
Galgotias University 24
Problem 5: Removing Extra Spaces (trim())
Task: Trim extra spaces from " Java Programming " and print the cleaned
string.
Galgotias University 25
Problem 6: Using String Pool (intern())
Task: Demonstrate how intern() makes two strings share memory.
Galgotias University 26
✔ compareTo() helps in sorting and ordering
strings.
✔ replace() modifies specific parts of a string.
Galgotias University 28
Review and Reflection
from students
Galgotias University 29