Learn String Literal In Java

Hey!! Welcome to flower brackets. In this post we are going to learn string literal in java.

Well, string is a sequence of characters. In java, string is an object that represents a sequence of characters.

For example:

String str_Literal = "Do you want to learn java!!!?";
String str_Example = "Yes I do!!";

Above we have two string literal and it consists of series of characters inside the double quotation marks.

String literal may contain any Unicode characters like UTF – 16, UTF – 8 characters.

The java.lang.String class is used to create object.

String literal can include escape characters like this,

String str_New = "Heyy guys!!, \"How are you doing?\"";
System.out.println("Well, \n Welcome to Flower Brackets \n");

Above you can see string literal which contains escape character code.

example: Learn String Literal In Java

We will see one example now,

public class StringLiteralExample
{

public static void main(String[] args)
{

String str_Literal = "Do you want to learn java!!!?";
String str_Example = "Yes I do!!";
String str_New = "Heyy guys!!, \"How are you doing?\"";
String learn = "Java is the best programming language.!!";

System.out.println("str_Literal - " + str_Literal); System.out.println("str_Example - " + str_Example); System.out.println("str_New - " + str_New); System.out.println("learn - " + learn); System.out.println("Well, \n Welcome to Flower Brackets \n");

}

}

Output:

Learn String Literal In Java

In above example “slash n” ( \n ) creates a new line in the output.

ways to create string literal:

There are two ways to create string literal. They are

  1. Using double quotes
  2. Using new keyword

For example:

Using double quotes: String str = "Hello World!";
Using new keyword: String str = new String("Hello World!");

Difference between string literal and string object

Basically when we are using string literal string can be interned. When you use new string you get a new string object.

For example:

// String literal

String x = "PQR";
String y = "PQR";

if (x == y)   // using if condition to check
{
System.out.println("String literal x and y are identical.");
}

Above example output will be true. Since they are stored in a common place. So “PQR” will be stored in the string constant pool.

The moment when I create second string variable value “PQR”, it will refer to the string constant pool.

Also Read – Learn Strings In Java

Since “PQR” is already available it will assign the same reference to “y”.

The condition x == y will become true. So in order to reuse the memory we are using string constant pool.

Whereas in string objects we create two different objects and they have different references.

For example:

// String objects

String x = new String("PQR");
String y = new String("PQR");

if(x != y)
{
System.out.println("String object references are not equal.");
}

In the above example though the contents are same they are having different references. String x and string y are created in the heap memory.

So the condition (x != y) will become false.

String object is an individual instance of java.lang.String class. In general you should use a string literal notation whenever possible.

Also it is easier to read and it gives the compiler a chance to optimize your code.

Let’s see another instance

String val1 = "CoreJava";
String val2 = new String("CoreJava");
String val3 = "CoreJava";

In this case String val1 is equal to “CoreJava”. We are creating another string object val2 in the heap. Lastly string val3 is equal to “CoreJava”.

These three contains the same content. But only val1 and val3 are same.

The reference will be same because again they are using string literal. It will be stored in string constant pool.

val1 will refer to an intern string object. This means that the character sequence “CoreJava” will be stored at a central place called string constant pool.

Whenever the same literal “CoreJava” is used the jvm will not create a new string object but use the reference of the cast string.

That’s why string constant pool is used. So whenever we use same string literal it will not create you different reference it will use the same reference.

We are trying to reuse the memory. Whereas here,

// String objects

String x = new String("PQR");
String y = new String("PQR");

We are actually recreating the object in memory and it’s based of memory.

Because contents are same why should I create in the heap memory.

So the best practice is in using string literal.

When we run the above example this is the output we get,

Output:

String literal x and y are identical.
String object references are not equal.

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *