Strings in Java
Strings in Java
Strings in Java
In the given example only one object will be created. Firstly JVM will
not find any string object with the value “Welcome” in the string
constant pool, so it will create a new object. After that it will find the
string with the value “Welcome” in the pool, it will not create a new
object but will return the reference to the same instance. In this article,
we will learn about Java Strings.
Example:
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Below is an example of a String in Java:
Got It !
Java
// Main Function
public static void main(String args[])
{
String str = new String("example");
// creating Java string by new keyword
// this statement create two object i.e
// first the object is created in heap
// memory area and second the object is
// created in String constant pool.
System.out.println(str);
}
}
Output
example
String Literal
Using new Keyword
Syntax:
1. String literal
Example:
Example:
CharSequence Interface
1. String
2. StringBuffer
3. StringBuilder
1. String
Syntax
Syntax:
3. StringBuilder
Syntax:
StringTokenizer
Example:
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:
Here the JVM checks the String Constant Pool. If the string does not
exist, then a new string instance is created and placed in a pool. If the
string exists, then it will not create a new object. Rather, it will return
the reference to the same instance. The cache that stores these string
instances is known as the String Constant pool or String Pool. In
earlier versions of Java up to JDK 6 String pool was located inside
PermGen(Permanent Generation) space. But in JDK 7 it is moved to the
main heap area.
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.
Java
class GFG {
public static void main(String[] args)
{
String s = "Sachin";
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 you 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.
For Example:
Java
class GFG {
public static void main(String[] args)
{
String name = "Sachin";
name = name.concat(" Tendulkar");
System.out.println(name);
}
}
Output
Sachin Tendulkar
Example:
The string can also be declared using a new operator i.e. dynamically
allocated. In case of String are dynamically allocated they are assigned
a new memory location in the heap. This string will not be added to
the String constant pool.
Example:
String demoString = new String("Geeks");
If you want to store this string in the constant pool then you will need
to “intern” it.
Example:
Java
class Test {
public static void main(String[] args)
{
// Declare String without using new operator
String name = "GeeksforGeeks";
Output
For example:
Let us have a look at the concept with a Java program and visualize the
actual JVM memory structure:
Java
Output
TAT
TAT
TAT
TAT
Java
// Driver Class
class GFG {
// main function
public static void main(String args[])
{
byte ascii[] = { 71, 70, 71 };
Output
GFG
FG
Example 2:
Java
class GFG {
public static void main(String args[])
{
Output
Gfg
Gfg
Because java uses the concept of string literal. Suppose there are 5
reference variables, all refer to one object “Sachin”. If one reference
variable changes the value of the object, it will be affected by all the
reference variables. That is why string objects are immutable in Java.
DSA In JAVA/C++
DSA In Python
DSA In JavaScript
Trusted by Millions, Taught by One- Join the best DSA Course Today!
Previous Next
Similar Reads
Number of common base strings for two strings
Check whether two strings can be made equal by reversing substring of equal
length from both strings
Print all Strings from array A[] having all strings from array B[] as subsequence
Append digits to the end of duplicate strings to make all strings in an array
unique
R RishabhP…
10 Best Free Social Media Management and Marketing Apps for Android - 2024
10 Best Customer Database Software of 2024
How to Delete Whatsapp Business Account?
Discord vs Zoom: Select The Efficienct One for Virtual Meetings?
30 OOPs Interview Questions and Answers (2024)
A-143, 9th Floor, Sovereign Corporate
Tower, Sector-136, Noida, Uttar Pradesh -
201305
Company Explore
About Us Hack-A-Thons
Legal GfG Weekly Contest
Careers DSA in JAVA/C++
In Media Master System Design
Contact Us Master CP
Advertise with us GeeksforGeeks Videos
GFG Corporate Solution Geeks Community
Placement Training Program
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets
Tutorials Archive