0% found this document useful (0 votes)
9 views6 pages

Java Strings: Key Concepts & Methods

The document provides a comprehensive overview of Java Strings, covering their definition, immutability, creation methods, common methods, performance issues, and formatting with printf(). It emphasizes the use of String Pool for memory optimization and the importance of using .equals() for content comparison. Key takeaways include the immutability of strings and the recommendation to use StringBuilder for efficient string manipulation.

Uploaded by

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

Java Strings: Key Concepts & Methods

The document provides a comprehensive overview of Java Strings, covering their definition, immutability, creation methods, common methods, performance issues, and formatting with printf(). It emphasizes the use of String Pool for memory optimization and the importance of using .equals() for content comparison. Key takeaways include the immutability of strings and the recommendation to use StringBuilder for efficient string manipulation.

Uploaded by

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

💡 Java Strings - Complete Notes

1. Introduction to Strings
●​ A string is a sequence of characters (e.g., "Kunal").​

●​ In Java, strings are objects, not primitive types.​

●​ Strings are stored in heap memory.​

●​ The String class is part of [Link].​

2. String Pool & Immutability


🔹 String Pool
●​ A memory space in the heap that stores unique string literals.​

●​ Purpose: Save memory by reusing identical strings.​

String a = "Kunal";
String b = "Kunal";
[Link](a == b); // true
[Link]([Link](b)); // true

🔹 Immutability
●​ Strings cannot be changed once created.​

●​ Any modification results in a new object.​

String a = "Kunal";
a = "Kushwaha"; // New object is created
3. String Creation
🔸 Using Literal
String s1 = "Hello"; // stored in String Pool

🔸 Using new
String s2 = new String("Hello"); // stored in heap (outside pool)
[Link](s1 == s2); // false

4. Common String Methods


Method Description Example

length() Returns number of "Kunal".length() → 5


characters

charAt(index) Character at specific "Kunal".charAt(0) → 'K'


index

toLowerCase() Converts to lowercase "Kunal".toLowerCase() → "kunal"

toUpperCase() Converts to uppercase "Kunal".toUpperCase() → "KUNAL"

concat(str) Appends another "Kunal".concat(" Kushwaha")


string

indexOf(char) First index of "Kunal".indexOf('a') → 3


character

lastIndexOf(cha Last index of character "Kunal".lastIndexOf('a') → 3


r)

substring(start Extracts substring "Kunal".substring(1,3) → "un"


,end)

replace(old,new Replaces characters "Kunal".replace('a','x') →


) "Kunxl"

trim() Removes spaces from " Kunal ".trim() → "Kunal"


both ends
split(regex) Splits into array "Kunal-Kushwaha".split("-") →
["Kunal", "Kushwaha"]

5. String Performance Issues


🔸 Problem
String series = "";
for (int i = 0; i < 26; i++) {
series += (char)('a' + i); // Inefficient: creates new String each time
}

🔸 Solution: Use StringBuilder


StringBuilder builder = new StringBuilder();
for (int i = 0; i < 26; i++) {
[Link]((char)('a' + i));
}
[Link]([Link]());

6. Pretty Printing with printf() – Complete Guide

✅ Basic Syntax
[Link]("format string", values);

🔸 Common Format Specifiers


Specifie Type Example
r

%d Integer [Link]("%d", 25); → 25

%f Floating [Link]("%.2f",
3.1415); → 3.14

%s String [Link]("%s", "Java");


→ Java

%c Character [Link]("%c", 'A'); → A


%b Boolean [Link]("%b", true); →
true

%% Percent [Link]("%%"); → %
sign

🔸 Floating Point (%f)


Format Description Output

%.2f 2 decimal places 3.14

%6.2f Width 6, 2 decimals '


3.14'

%10.3f Width 10, 3 decimals '


3.142'

🔸 Integer Formatting (%d)


Format Description Output

%d Standard 25

%5d Width of 5, right-align ' 25'

%-5d Width 5, left-align '25 '

%05d Padded with zeros 00025

🔸 String Formatting (%s)


Format Description Output

%s Standard "Hello
"

%10s Width 10, right-align '


Hello'

%-10s Width 10, left-align 'Hello


'
🔸 Character & Boolean
[Link]("Char: %c", 'A'); // Char: A
[Link]("Bool: %b", false); // Bool: false

🔸 Multiple Values
String name = "Alice";
int age = 22;
float score = 91.567f;

[Link]("Name: %s | Age: %d | Score: %.2f", name, age, score);


// Output: Name: Alice | Age: 22 | Score: 91.57

🔸 Escape Sequences
Code Meaning

\n New line

\t Tab

\\ Backslash

\" Double quote

✅ Summary Table
Type Code Example Value Format Output

Integer %d 42 %5d ' 42'

Float %f 3.14159 %.2f '3.14


'

String %s "Java" %-10s 'Java


'

Char %c 'X' %c X

Boolean %b true %b true

Percent %% — %% %
7. Palindrome Check Example
public static boolean isPalindrome(String str) {
if (str == null || [Link]() == 0) return true;
str = [Link]();
for (int i = 0; i <= [Link]() / 2; i++) {
char start = [Link](i);
char end = [Link]([Link]() - 1 - i);
if (start != end) return false;
}
return true;
}

[Link](isPalindrome("abcdcba")); // true

8. Key Takeaways
●​ Java uses String Pool to optimize memory.​

●​ Strings are immutable — use StringBuilder for efficient editing.​

●​ Know the difference between == (reference) and .equals() (value).​

●​ Always use .equals() to compare content.​

You might also like