Introduction to Strings

Strings are most widely used in Java programming.

Strings basically represent a sequence of characters.

In the Java programming language, strings are objects.

The Java platform provides the String class to create and manipulate strings.

Example:

  1. Char[] c = {‘H’,’e’,’l’,’l’,’o’};
Char[] c = {‘H’,’e’,’l’,’l’,’o’};


‘c’ is the character array having sequence of characters and same is represented using string as

  1. String s = “Hello”;
String s = “Hello”;


String object is required to represent any string.

In above example “s” is a reference variable pointing to String object which holds “Hello” in it.

String creation

There are 2 ways to create String in Java

1) By using Literal

2) By using “new”



String creation using Literals

In this case, we just assign String value using double quotes as below

  1. String s=” Hello”;
String s=” Hello”;


This will create the string in String constant pool (SCP) which is a separate memory used within heap.


How String constant pool (SCP) works?

Whenever we create a string literal, the JVM checks the string constant pool first.

If the string already exists in the pool, a reference to the pooled instance is returned.

If string doesn’t exist in the pool, a new string instance is created and placed in the pool.

Example

  1. String s1=”java”;
  2. String s2=”java”;
  3. String s3=”java”;
String s1=”java”;
String s2=”java”;
String s3=”java”;


string_Creation_literal

As we can see in the above memory diagram, initially string “java” is not available in String constant pool and hence it creates one in the pool.

In subsequent lines, it will find the string with the value "java" in the pool, so it will not create new object but will return the reference to the same instance.

For above 3 lines of code,only one string object is created.

Note :

String literals created using double quotes like above will be stored in a separate memory called String constant pool.

String creation using “new


We can create string using “new” as below

  1. String s1=new String(“java”);
String s1=new String(“java”);


We use “new” to create any object in java and here also we are creating String object.

We know that, all objects are stored in Heap memory including String objects.

The only extra thing in case of String is, String literal = “string value” will be placed in the String constant pool if it’s not exist in pool.


But remember, “s1” still points to an object in Heap (not to string literal in pool)

  1. String s1=new String(“java”);
String s1=new String(“java”);


This line creates 2 objects, 1 in the Heap memory and 1 in the String constant pool

string_Creation_1

Note :

Every time we create string using “new”, Java creates new string object in the Heap and it will place the same string in String constant pool if it’s not exist in the pool.

  1. String s2=new String(“java”);
String s2=new String(“java”);


This line creates only one object in Heap as “ java ” is already present in String constant pool placed while creating “s1”.

string_Creation_NEW


Why Java has provided String constant pool mechanism?


We know that, we can create string object using “new”
then,
Why another way of creating String object using literals is provided in Java?

Since we use Strings a lot in the java applications, Java has provided an efficient mechanism where no new objects should be created when we create same string value in multiple places of our application.

This way it helps in saving lot of memory when we refer similar strings in different parts of the application.

Since String is most widely used object in any application, they have introduced this mechanism only for Strings.


Should I use String literal or new to create String in my Project?

Always prefer creating strings using literals as its performance is much faster than creating string using “ new ”.

And also, when we create string using literals, we don’t create extra object in heap rather we refer existing object in string constant pool.


How garbage collection works with Strings?


String literals gets stored in string constant pool and its always required to be in the pool so that it can be used for future references, so string literals stored in constant pool will never be garbage collected.

However, String objects created using “new” will be garbage collected when they don’t have any reference to them.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on