Strings in Java- Different Ways of creating Strings

Strings A Class without which any of the Java project would be incomplete and we can’t even think about programming if we don’t know how to use Strings in our project. So what are these Strings and how we can use them in our classes and projects.

If you don’t agree with me then I hope you might have seem the main method which take the String arguments… 🙂

Strings in Java is basically a sequence of characters and it is the way to handle the text in java or any other programming language. In java the encoding used is UTF-16 i.e it uses 2 Bytes to store any character in the memory.

Note: Strings are IMMUTABLE objects in Java i.e. once created their value can’t be changed.


Ways of creating string in Java

There are basically two ways of creating in Java-

  1. Using “new” keyword.
  2. Using String Literal.

Using “new” keyword

In this a new Sting object is created every time whether we are using the same value for the string or a different value. In this case the object is created in the heap.

Example of String creation using “new”

String new1 = new String(“Codingeek”);

Using String Literal

It is a special case of creating the Strings as in this the String objects are created in a special memory area called The String Constant Pool in which only one object of a particular value is created i.e. whenever we create a object in java using String literal then the JVM checks String Pool to check whether there is an object with the same value and if it finds one then it does not create any new object and the variable is referenced to the existing object. And if no object exists with that value then it creates a new object and reference the variable to a newly created object.

Example of String creation using “new”

String literal1= “Codingeek”;

Before we start our actual tutorial lets see the methods of comparing String

  1. “==” – is a test for reference i.e. it checks whether the two variables are referring the same object or not
  2. .equals() – is a test to check values of the comparing variables. it returns true if the value is same and false otherwise.

Now lets see what happens when we create Strings using New and using String Literals(See the highlighted LOC)

package codingeekStringTutorials;

public class CreatingStrings {

	public static void main(String[] args) {

		// Example with new Keyword
		String new1 = new String("Codingeek");
		String new2 = new String("Codingeek");

		System.out.println("Checking Strings with "new" ");
		System.out.println("Is reference same - " + (new1 == new2));
		System.out.println("Is Value same - " + new1.equals(new2));

		// Example with String Literal
		String literal1 = "Codingeek";
		String literal2 = "Codingeek";

Checking Strings with "literal" ");
		System.out.println("Is reference same - " + (literal1 == literal2));
		System.out.println("Is value same - " + literal1.equals(literal2));

 Checking Strings with "new"
 Is reference same - false
 Is Value same - true

Checking Strings with "literal"
 Is reference same - true
 Is value same - true

In the above exaple we can see how everytime a new object is created with new keyword( Because reference is different – false) and how we reference to the same object in the String Constant Pool( because reference is same).

If you have any problem then please comment and I would try to help you in the best possible way I can.

Recommended -

Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Kim Ji-Sik
Kim Ji-Sik
8 years ago

I reading your article. Different ways of creating string, Strings in Java are Immutable or Final- Why, Why to use Char Array instead of String for storing password in Java – Security etc .
everything is valuable and nice thing.
Thanks for your post !

Would love your thoughts, please comment.x