0% found this document useful (0 votes)
5 views7 pages

Java CAT Flow Level 3

The document provides an overview of Strings in programming, explaining their definition, creation, and memory allocation. It details the differences between String types, mutable and immutable Strings, and includes various practice questions and methods for manipulating Strings. Additionally, it covers String parsing and formatting for effective data handling and output presentation.

Uploaded by

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

Java CAT Flow Level 3

The document provides an overview of Strings in programming, explaining their definition, creation, and memory allocation. It details the differences between String types, mutable and immutable Strings, and includes various practice questions and methods for manipulating Strings. Additionally, it covers String parsing and formatting for effective data handling and output presentation.

Uploaded by

Ramya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Level 3 Strings

What is String?
A String is an object that stores a sequence of characters enclosed with double quotes(“ “

).

Why String is needed?


Computers mainly understand numbers (0s and 1s)
But humans use text (names, messages, data)

So we need String to store and handle text.

[Link] Store Text Data.

[Link] Input Handling

[Link] Processing

[Link] Between Systems

Creation of Strings
[Link] a String

String variableName;

[Link] a String

Using Literal (most common)


String name = " Strings ";

Using new keyword

String name = new String("Strings");

Getting Input from

String a = [Link]();

String a = [Link]();

Memory Allocation of String


How string literals are saved in memory (Stack, Heap and String Constant Pool)

String objects are stored in two places:

1. String Pool (inside Heap)

2. Heap Memory

Difference between CharArray , String(Literal) and String(new Keyword).

Practice Questions:
[Link] a string variable and assign your name
[Link] a string and print it

[Link] multiple strings in one line

[Link] a string with a sentence and print it

[Link] a string without initializing and assign value later

[Link] a string using literal method

[Link] a string using new keyword

[Link] both and observe output

[Link] two strings with same value using literal — check if they refer same memory

[Link] two strings using new keyword — check memory behavior

Inbuilt Methods: refer program


1. Length
int len = [Link]();
2. Access Character
char ch = [Link](index);
3. Compare Strings
boolean res = [Link](str2);
4. Search
int pos = [Link]('a');
int pos = [Link]('a');
boolean res = [Link]("text");
5. Modify
String s = [Link]('a','b');
String s = [Link]();
6. Case Conversion
String s = [Link]();
String s = [Link]();
7. Substring
String s = [Link](start);
String s = [Link](start, end);
8. Join Strings
String s = [Link](str2);
9. Split String
String[] arr = [Link](",");
10. Check Start/End
boolean res = [Link]("text");
boolean res = [Link]("text");
11. Check Empty
boolean res = [Link]();
12. Convert
char[] arr = [Link]();

Practice Question:
[Link] the length of a string

[Link] all characters using charAt()

[Link] first and last character

[Link] number of vowels

[Link] number of uppercase and lowercase letters

String Comparison:
String comparison means checking whether two strings are equal or which one is greater.
1. Using == (Reference Comparison)

Compares memory addresses (reference)

String s1 = "Java";

String s2 = "Java";

[Link](s1 == s2);

Output: true (same reference from String Pool)

[Link] equals():

Compares actual content

String s1 = "Java";

String s2 = "Java";

Practice Questions:
[Link] if two strings are equal

[Link] if two strings are equal ignoring case

[Link] two strings and print which is greater

[Link] if string is palindrome using equals()

Without in-built :
Length, Reverse, Uppercase, Lowercase, Replace, Substring
Practice Question:
1. Find the length of a string without using length().
2. Print characters between two given positions.
3. Reverse a string without using any built-in method.
4. Convert all lowercase characters to uppercase manually.
5. Replace all occurrences of a given character with another character.
6. Extract substring from given start and end index manually.

String Mutable, Immutable


Immutable

Immutable String (String Class)


Once created, cannot be changed
String s = "Hello";
s = s + " World";

Mutable

Mutable String (StringBuilder / StringBuffer)

Can be modified without creating new object

Types

String Builder

String Buffered

StringBuilder
 Fast

 Used when only one thread is working

 No safety mechanism

StringBuffer
 Safe

 Used when multiple threads access data

 Slightly slow due to synchronization

Note: Synchronization is a process that ensures only one thread can access a resource at
a time.

Parsing String
Parsing means converting a String into another data type (like int, double, etc.)

Why Parsing is Needed

In Java, most inputs (keyboard, command line) come as String


To perform calculations → we must convert them into numbers

1. String → int
String s = "100";
int num = [Link](s);
2. String → double
String s = "10.5";
double d = [Link](s);
3. String → float
String s = "5.5";
float f = [Link](s);
4. String → long
String s = "1000";
long l = [Link](s);
5. String → Boolean
String s = "true";
boolean b = [Link](s);

String Formatting:
String formatting means creating a string with values inserted in a specific format.

Why It is Used

✔️To display output neatly


✔️To control format (decimal, spacing, alignment)
✔️Used in reports, bills, UI

Using printf()
[Link]("format string", values);

Example with Multiple Types:

[Link]("Int: %d, Float: %.2f, String: %s", 10, 5.678, "Java");

You might also like