0% found this document useful (0 votes)
14 views29 pages

Java Strings- Introduction & String Methods PPT

The document provides an introduction to Java Strings, explaining their characteristics, storage, and methods for manipulation. It covers the immutability of Strings and their significance in terms of security, thread safety, and performance. Additionally, it includes learning activities and practical tasks to reinforce understanding of String methods.

Uploaded by

anamikamahato102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views29 pages

Java Strings- Introduction & String Methods PPT

The document provides an introduction to Java Strings, explaining their characteristics, storage, and methods for manipulation. It covers the immutability of Strings and their significance in terms of security, thread safety, and performance. Additionally, it includes learning activities and practical tasks to reinforce understanding of String methods.

Uploaded by

anamikamahato102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Java Strings- Introduction & String

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).

Faster access due to single index Slightly slower as it requires two


Performance
lookup. indices lookups.

List of product prices, student Chessboard representation, game


Real-Life Application
grades, queue processing. maps, spreadsheet data storage.

Galgotias University 2
Opening:
Suppose you creating a
password!!

Galgotias University 3
Learning Outcomes:

Analyze and manipulate text data


by combining multiple String
methods to solve complex string
processing tasks.

Galgotias University 4
1. Introduction to Java Strings

2. String Methods & Manipulation

3. Think-Pair-Share Activity on String


Session
Outline 4. Hands on Learning Activity: String
Challenges
5. Summary

Galgotias University 5
Introduction to Java Strings

📌 What are Strings in Java? 📌 How Are Strings Stored?


• A String is a sequence of • String Literal:
characters enclosed in double
quotes. String str1 = "Hello"; //
• Immutable → Once created, it Stored in String Pool
cannot be changed. • Using new Keyword:
• Stored in String Pool for
memory efficiency.
String str2 = new
String("Hello"); // Stored in
• Example:
Heap Memory
String message = "Hello, Java!";
💡 Difference: String literals
System.out.println(message); are more memory-efficient.
Galgotias University 6
Introduction to Java Strings

📌 What are Strings in Java? 📌 How Are Strings Stored?


• A String is a sequence of • String Literal:
characters enclosed in double
quotes. String str1 = "Hello"; //
• Immutable → Once created, it Stored in String Pool
cannot be changed. • Using new Keyword:
• Stored in String Pool for
memory efficiency.
String str2 = new
String("Hello"); // Stored in
• Example:
Heap Memory
String message = "Hello, Java!";
💡 Difference: String literals
System.out.println(message); are more memory-efficient.
Galgotias University 7
Java Strings are immutable

Galgotias University 8
Learning Activity 1 - Think-Pair-Share

11️⃣ Think: Students individually reflect on why strings are


immutable
2️⃣ Pair: Discuss their understanding of specific Scenario.
3️⃣ Share: Each pair presents insights, including real-world
applications.

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

📌 What are Strings in Java? 📌 How Are Strings Stored?


• A String is a sequence of • String Literal:
characters enclosed in double
quotes. String str1 = "Hello"; //
• Immutable → Once created, it Stored in String Pool
cannot be changed. • Using new Keyword:
• Stored in String Pool for
memory efficiency.
String str2 = new
String("Hello"); // Stored in
• Example:
Heap Memory
String message = "Hello, Java!";
💡 Difference: String literals
System.out.println(message); are more memory-efficient.
Galgotias University 11
String Methods & Manipulation

Method Description Example

length() Returns string length "Java".length() → 4

charAt(index) Retrieves character at index "Java".charAt(1) → 'a'

substring(start, end) Extracts substring "Hello".substring(1,4) → "ell"

equals(str) Compares two strings "Java".equals("java") → false

toUpperCase()/
Converts to uppercase/ "java".toUpperCase()" →
toLowerCase()
lowecase "JAVA"

Galgotias University 12
String methods

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 13
String methods

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 14
String methods

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

Analyze and manipulate text data by


combining multiple String methods to
solve complex string processing tasks.

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.

Problem 2: Replacing a Word (replace())


Task: Replace "bad" with "good" in the sentence "This is a bad idea" and print the new sentence.

Problem 3: Splitting a CSV String (split())


Task: Split the string "John,25,Engineer" into separate values and print them.

Problem 4: Replacing Multiple Characters (replace())


Task: Replace all spaces in the string "Hello World Java" with "-" and print the result.

Problem 5: Removing Extra Spaces (trim())


Task: Trim extra spaces from " Java Programming " and print the cleaned string.

Problem 6: Using String Pool (intern())


Task: Demonstrate how intern() makes two strings share memory.
Galgotias University 20
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.

✔ split() breaks a string into smaller parts.

✔ trim() removes unnecessary spaces.


✔ intern() optimizes memory usage for string
literals.
Galgotias University 27
Information about the next lesson
• Java Strings Buffer: Advanced Methods and
Manipulations

Galgotias University 28
Review and Reflection
from students

Galgotias University 29

You might also like