0% found this document useful (0 votes)
40 views

5 String

The Java String class handles strings in Java using the java.lang package. Strings are immutable and stored in the string literal pool if created with double quotes or through intern(), but are stored in heap memory if created with new. There are three main ways to create strings: using double quotes, the new keyword, or the intern() method, which places the string in the pool. The string pool allows caching and reusability of strings.

Uploaded by

Sumedh Dahiwale
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

5 String

The Java String class handles strings in Java using the java.lang package. Strings are immutable and stored in the string literal pool if created with double quotes or through intern(), but are stored in heap memory if created with new. There are three main ways to create strings: using double quotes, the new keyword, or the intern() method, which places the string in the pool. The string pool allows caching and reusability of strings.

Uploaded by

Sumedh Dahiwale
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

String class

Java String class


🠶 In java String are handled using String class for java.lang
package. It has advantages over tradition character array
🠶 Strings are constant/ immutable; their values cannot be
changed after they are created.
🠶 StringBuffer and StringBuilder classes are used to change
strings.
String str = "ACTS"; //This is called as String Literals.
is equivalent to:
char data[] = {'A', 'C', 'T', 'S'};
String str = new String(data); // String created using new
Java String class
🠶Storage of Strings
o String Literals are stored in the String
Literal/Constant Pool
o String created using new are stored on heap
Java String class
🠶 Three popular ways of creating strings in Java:
o String literal
o Using new keyword
o Using String.intern() method
1. String literal
This is the simplest and mostly used way of creating string. It is done using putting
characters in double quotes.
String str1 = “CDAC"; str1 CDAC
String str2 = "ACTS"; str2 ACTS
String str3 = "ACTS"; str3

String Literal Pool

Stack Heap
Java String class
2. Using new keyword
🠶 We can create new String objects using the new keyword.
🠶 When we create new string literals using the new keyword, memory is allocated to
those String objects in the Java heap memory outside the String Pool.

String str1 = “CDAC"; str1 CDAC


String str2 = "ACTS"; str2 ACTS
String str3 = "ACTS"; str3
String str4 = new String("ACTS“); str4 String Literal Pool
str2 == str3; =>true ACTS
str2 == str4; => false
Stack Heap
Str2.equals(str4); => true
== check reference and equals check contents
Java String class
3. Using String.intern() method
o Creating strings using the new keyword allocates memory to the string
object in the heap but outside the string constant pool.
o When we use the String.intern() method, JVM puts the string literal in
the String Pool.

String str1 = “CDAC"; str1 CDAC

String str2 = "ACTS"; str2 ACTS


str3 intern
String str3 = "ACTS";
str4 String Literal Pool
String str4 = new String("ACTS“);
ACTS
str4.intern();
Stack Heap
Java String class summary
• A string is a set of characters that are always enclosed in double-
quotes.
• Strings in Java are immutable in nature. This immutability is
achieved through String Pool.
• String Pool in Java is a special storage space in Java heap
memory. It is also known as String Constant Pool or String Intern
Pool.
• Whenever a new string is created, JVM first checks the string
pool. If it encounters the same string, then instead of creating a
new string, it returns the same instance of the found string to
the variable.
Java String class summary
• The String.intern() method puts the string in the String pool or
refers to another String object from the string pool having the
same value.
• String Interning is a method that stores only a copy of each
distinct string literal. String Pool is an implementation of the
concept of String Interning.
• Java String Pool allows caching of string and reusability.
Java String class few important methods
• charAt()
• valueOf()
• length()
• toLowerCase(), toUpperCase()
• trim()
• valueOf()
• concat()
• contains()
• equals(), equalsIngoreCase()

You might also like