0% found this document useful (0 votes)
2 views11 pages

5.java Strings

The document provides an overview of Java Strings, detailing their characteristics such as immutability and UTF-16 encoding. It explains different ways to create strings, including string literals and the new keyword, as well as the classes related to string manipulation like StringBuffer and StringBuilder. Additionally, it discusses how strings are stored in memory, the migration of the string constant pool to the heap in Java 7, and includes examples of string creation and manipulation.
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)
2 views11 pages

5.java Strings

The document provides an overview of Java Strings, detailing their characteristics such as immutability and UTF-16 encoding. It explains different ways to create strings, including string literals and the new keyword, as well as the classes related to string manipulation like StringBuffer and StringBuilder. Additionally, it discusses how strings are stored in memory, the migration of the string constant pool to the heap in Java 7, and includes examples of string creation and manipulation.
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

Java Strings

Last Updated : 7 Apr, 2026





A String in Java is an object used to store a sequence of characters
enclosed in double quotes. It uses UTF-16 encoding and provides
methods for handling text data.
Each character in a string is stored using 16-bit Unicode
(UTF-16) encoding.
 Strings are immutable, meaning their value cannot be
changed after creation.
 Java provides a rich API for manipulation, comparison, and
concatenation of strings.
Example:
String name = "Geeks";
String num = "1234";

String

public class Geeks {

// Main Function
public static void main(String args[])
{

// creating Java string using a new keyword


String str = new String("Geeks");
[Link](str);
}
}

Output
Geeks

Ways of Creating a Java String


There are two ways to create a string in Java:

1. String literal (Static Memory)


To make Java more memory efficient (because no new objects are
created if it exists already in the string constant pool).
Example:
String str = “GeeksforGeeks”;

2. Using new keyword (Heap Memory)

Using the new keyword creates a new object in heap memory, even
if the same string already exists in the pool.
 One object is created in the heap memory
 The string literal is stored in the string pool (if not already
present)
 The reference variable points to the heap object, not the
pool
Example:
String str = new String (“GeeksforGeeks”);

Interfaces and Classes in Strings in Java

CharSequence Interface

CharSequence Interface is used for representing the sequence of


Characters in Java. Classes that are implemented using the
CharSequence interface are mentioned below and It provides basic
methods such as length(), charAt(), subSequence() and toString().
Classes that implement CharSequence include:

1. String
String is an immutable class in Java, which means that once a
String object is created, its value cannot be changed. If you want to
modify a string a new String object is created and the original
remains unchanged.
Syntax:
// Method 1
String str= "geeks";
// Method 2
String str= new String("geeks");

2. StringBuffer

StringBuffer is a peer class of String, it is mutable in nature and it is


thread safe class and is used in multithreaded environments where
a StringBuffer object is shared among multiple threads.. As it is
thread safe so there is extra overhead, so it is mainly used for
multithreaded program.
Syntax:
StringBuffer demoString = new StringBuffer("GeeksforGeeks");

3. StringBuilder

StringBuilder in Java represents an alternative to String and


StringBuffer Class, as it creates a mutable sequence of characters
and it is not thread safe. It is used only within the thread , so there
is no extra overhead , so it is mainly used for single threaded
program.
Syntax:
StringBuilder demoString = new StringBuilder();
[Link]("GFG");

4. StringTokenizer

StringTokenizer class in Java is used to break a string into tokens.


StringTokenizer

A StringTokenizer object internally maintains a current position


within the string to be tokenized. Some operations advance this
current position past the characters processed. A token is returned
by taking a substring of the string that was used to create the
StringTokenizer object.
Syntax:
StringTokenizer st = new StringTokenizer("Java String Example");

Immutable String in Java

In Java, string objects are immutable. Immutable simply means


unmodifiable or unchangeable. Once a string object is created its
data or state can't be changed but a new string object is created.

import [Link].*;

class Geeks
{
public static void main(String[] args)
{
String s = "Sachin";

// concat() method appends the string at the end


[Link](" Tendulkar");

// This will print Sachin because strings are immutable


objects
[Link](s);
}
}

Output
Sachin
Here, Sachin is not changed but a new object is created with
“Sachin Tendulkar”. That is why a string is known as immutable.
As we can see in the given figure that two objects are created but s
reference variable still refers to "Sachin" and not to "Sachin
Tendulkar". But if we explicitly assign it to the reference variable, it
will refer to the "Sachin Tendulkar" object.
Example: Java program to assign the reference explicitly in String
using [Link]() method.
import [Link].*;

class Geeks
{
public static void main(String[] args)
{
String name = "Sachin";
name = [Link](" Tendulkar");
[Link](name);
}
}

Output
Sachin Tendulkar

How Strings are Stored in Java Memory

String literal
Whenever a String Object is created as a literal, the object will be
created in the String constant pool . This allows JVM to optimize the
initialization of String literal. The string constant pool is present in
the heap.
Example 1: Using String literals to assigning char sequence value.
String str1 = "Hello";

string constant pool

Example 2: When we initialize the same char sequence using


string literals.
String str1 = "Hello";
String str2 = "Hello";
string constant pool

Using new Keyword

Strings can also be created using the new keyword, which allocates
a new object in heap memory. However, the string literal inside it is
still stored in the String Constant Pool (if not already present).
Example 1: Using new keyword to assign a char sequence to a
String object.
String str1 = new String("John"); String str2 = new String("Deo");

string constant pool


The intern() method returns a reference from the string constant
pool. If the string is not already present in the pool, it is added.
Otherwise, the existing reference from the pool is returned.
Example 2: Using .intern() to add a string object in string constant
pool.
// this will add the string to string constant pool.

String internedString = [Link]();


It is preferred to use String literals as it allows JVM to optimize
memory allocation.
If we notice if we use new keyword or string literals both store the
values in the string but the difference is if we use the string literals
or intern() the string object it will store the values in the string
constant pool which is present inside the heap as shown in the
image.
Example that shows how to declare a String:
import [Link].*;
import [Link].*;

class Daffodil
{
public static void main(String[] args) {

// Declare String without using new operator


String name = "Daffodil ";

// Prints the String.


[Link]("String name = " + name);

// Declare String using new operator


String newString = new String("Daffodil ");

// Prints the String.


[Link]("String newString = " + newString);
}
}

Output
String name = GeeksforGeeks
String newString = GeeksforGeeks
Note: All String objects are stored in heap memory. The String
Constant Pool is a special area inside the heap used to store unique
string literals.

String Pool Migration from PermGen to the


Normal Heap
Before Java 7, the String Constant Pool was stored in PermGen
space, which had limited memory. From Java 7 onwards, it was
moved to the heap to overcome these limitations and improve
memory management.
 PermGen space had limited size, causing issues with many
string objects
 Moving the pool to heap provided more memory and
flexibility
 Using new always creates a new object in heap, even if the
same string exists in the pool

For example:
String demoString = new String("Bhubaneswar");
Let us have a look at the concept with a Java program and visualize
the actual JVM memory structure:
Below is the implementation of the above approach:
class Geeks
{
public static void main(String args[])
{

// Declaring Strings using String literals


String s1 = "TAT";
String s2 = "TAT";

// Declaring Strings using new keyword


String s3 = new String("TAT");
String s4 = new String("TAT");

// Printing all the Strings


[Link](s1);
[Link](s2);
[Link](s3);
[Link](s4);
}
}

Output
TAT
TAT
TAT
TAT
JVM Memory Area

Note: All objects in Java are stored in a heap. The reference


variable is to the object stored in the stack area or they can be
contained in other objects which puts them in the heap area also.
Example 1:
class Daffodil
{
public static void main(String args[]) {

// Creating Byte ASCII Array


byte ascii[] = { 71, 70, 71 };

// Creating String using byte array


String firstString = new String(ascii);
[Link](firstString);

// Creating String using byte array with Start index


to End Index
String secondString = new String(ascii, 1, 2);
[Link](secondString);
}
}

Output
CIS
IS
Example 2:
class Geeks
{
public static void main(String args[]) {

// Character Array
char characters[] = { 'C', 'I', 'S' };

// Creating new String using Character Array


String firstString = new String(characters);

// Creating new String using another String


String secondString = new String(firstString);

[Link](firstString);
[Link](secondString);
}
}

Output
CIS
CIS

You might also like