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

Java String Guide: Creation & Methods

The document is a comprehensive guide to Java Strings, detailing their creation, immutability, and the String Constant Pool. It covers various string methods for information analysis, comparison, searching, and transformation, as well as mutable alternatives like StringBuilder and StringBuffer. Additionally, it addresses advanced concepts such as string interning and security practices for handling sensitive data like passwords.

Uploaded by

Sahith Ravan
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)
5 views2 pages

Java String Guide: Creation & Methods

The document is a comprehensive guide to Java Strings, detailing their creation, immutability, and the String Constant Pool. It covers various string methods for information analysis, comparison, searching, and transformation, as well as mutable alternatives like StringBuilder and StringBuffer. Additionally, it addresses advanced concepts such as string interning and security practices for handling sensitive data like passwords.

Uploaded by

Sahith Ravan
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

■ The Java String Master Compendium (Complete Guide)

In Java, a String is not a primitive data type (like int or char); it is an Object that represents a sequence of
characters. Understanding Strings is crucial because they are handled differently than any other object in Java to
optimize memory and performance. Here is a comprehensive guide to Java Strings from scratch.

1. Creating Strings
There are two primary ways to create a String object: • String Literal: The most common way. Java uses a "String
Pool" to reuse these. String s1 = "Hello"; • Using new Keyword: This forces Java to create a new object in the heap
memory, even if the value already exists in the pool. String s2 = new String("Hello");

2. String Immutability
In Java, Strings are immutable. Once a String object is created, its value cannot be changed. • If you "modify" a
string (e.g., s1 = s1 + " World"), Java actually creates a new String object in memory and updates the reference. •
Why? Security (passwords), Caching (String Pool), and Thread Safety.

3. The String Constant Pool (SCP)


The String Pool is a special memory area in the Heap where Java stores unique string literals. • When you create a
literal String s = "Java", the JVM checks if "Java" exists in the pool. • If yes, it returns the reference. If no, it creates
it. • intern() method: You can manually move a String object from the heap to the pool using [Link]().

4. Exhaustive String Method Library (Basic to Advanced)


A. Information & Content Analysis
length() -> "Java".length(); // 4
isEmpty() -> "".isEmpty(); // true
isBlank() -> " ".isBlank(); // true
charAt(int i) -> "Sky".charAt(1); // 'k'
hashCode() -> "A".hashCode(); // 65

B. Comparison Methods
equals(obj) -> "Hi".equals("hi"); // false
equalsIgnoreCase(s) -> "Hi".equalsIgnoreCase("hi"); // true
compareTo(s) -> "A".compareTo("B"); // -1
startsWith(prefix) -> "Format".startsWith("For"); // true
endsWith(suffix) -> "Ending".endsWith("ing"); // true

C. Search & Localization


indexOf(str) -> "Banana".indexOf("a"); // 1
lastIndexOf(str) -> "Banana".lastIndexOf("a"); // 5
contains(seq) -> "Hello".contains("ell"); // true

D. Transformation (Returns NEW String)


substring(s,e) -> "Unhappy".substring(2,5); // "hap"
toLowerCase() -> "JAVA".toLowerCase(); // "java"
toUpperCase() -> "java".toUpperCase(); // "JAVA"
trim() -> " hi ".trim(); // "hi"
replace() -> "cat".replace('c','r'); // "rat"
repeat(int n) -> "No".repeat(2); // "NoNo"

5. Mutable Alternatives: StringBuilder & StringBuffer


StringBuilder
StringBuilder sb = new StringBuilder("Start");
[Link](" Middle");
[Link](5, "-Added-");
[Link](5, 12);
[Link]();
[Link](0, 'E');
String finalStr = [Link]();

StringBuffer
StringBuffer provides the same API as StringBuilder but is synchronized, making it thread-safe but slower.

6. Advanced Concepts & Security


String Interning
String s1 = new String("Java");
String s2 = [Link]();
String s3 = "Java";
[Link](s2 == s3); // true

Security: Why Passwords use char[]


char[] pass = {'1','2','3'};
[Link](pass, '0');

You might also like