String literal in java

Let’s learn string literal in java.

String literal in java

String literal is a sequence of unicode characters like numbers, symbols and letters. For example,

String literal in java

String str1 = “Hello world java”;

String str2 = “Hello world java”;

In the above example we have created a string without using new keyword and characters are enclosed between double quotation mark.

We know that String in java is an object and it is stored in heap. While objects are created on heap and they are referencing variables are on stack.

Now when we create string (as shown in the above example) then the string is created in string literal pool.

String literal pool is a memory area on heap where distinct string objects are stored.

click here to learn more on string pool in java.

NOTE:

  • Both string literal and string objects are created on heap.
  • But string literal belongs to special memory area where only unique strings are allowed to store.
  • use string literal when you know value of string at compile time.
  • String literal pool is called/referred as String pool or String Constant pool.

We can use escape sequence like this,

\n – newline

\t – inserting tab

\b – backspace

\r – carriage return

\’ – single quotation mark

\\ – backslash

\” – double quotation mark

\f – form feed

\d – octal

\xd – hexadecimal

\ud – Unicode character


Now let’s a java program on string literals,

public class StringLiteralDemo 
{
   public static void main(String[] args) 
   {
      String str = "Hello World Java";
      System.out.println(str);
      String name = "ViratKohli";
      System.out.println(name);
      String message = "Hey Sachin, \"How are you?\"";
      System.out.println(message);
      System.out.println("Hii, \n Welcome to flower brackets blog\n");
      String welcome = "Earn while you sleep\u2122";
      System.out.println(welcome);
   }
}



Output:

Hello World Java
ViratKohli
Hey Sachin, “How are you?”
Hii,
Welcome to flower brackets blog

Earn while you sleep™

NOTE:

Below is incorrect string literal in java.

"Hello
world java"  // incorrect way

"Hello world java"  // correct way

It’s because string literals in java should begin and end in the same line.

Why string is immutable in java?

Because once string object is created can’t be changed. It cannot be modified. For example,

class ImmutableStringExample
{
   public static void main(String[] args)
   {
      String str = "hello";
      str.concat("world");
      System.out.println(str);
   }
}


Output:

hello

As you can see the output it is not possible to concatenate string ‘world’ using concat() method of String class. Because strings are immutable.

How to make string mutable in java?

String can be modified using StringBuilder class and StringBuffer class also called as mutable strings. Let’s see an example on how to make string mutable in java using StringBuilder class,

public class MutableStringExample
{
   public static void main(String[] args) 
   {
      StringBuilder obj = new StringBuilder("hello");
      System.out.println("Before appending: " + obj);
      obj.append(" world");
      System.out.println("After appending: " + obj);
   }
}


Output:

Before appending: hello
After appending: hello world

Now let’s see an example on how to make string mutable in java using StringBuffer class,

public class MutableStringExample 
{
   public static void main(String[] args) 
   {
      StringBuffer obj = new StringBuffer("hello");
      System.out.println("Before appending: " + obj);
      // memory address are same
      System.out.println(obj.hashCode());
      obj.append(" world");
      System.out.println("After appending: " + obj);
      // memory address are same
      System.out.println(obj.hashCode());
   }
}


Output:

Before appending: hello
366712642
After appending: hello world
366712642


Difference between string literal and string object in java

String literalString object
Java by default call intern() method of java.lang.String class when String literal is created.String object created using new keyword always creates a new object.
Here java doesn’t call intern() method by default.

Let’s see few examples,

// string literal example
String str1 = "helloworld";
String str2 = "helloworld";
System.out.println(str1 == str2);  // returns true


Let’s create string objects using new keyword and compare,

String str1 = new String("helloworld");
String str2 = new String("helloworld");
System.out.println(str1 == str2);  // false. because different references.


Now let’s compare string literal with string object using equality operator.

String str1 = "helloworld";
String str2 = new String("helloworld");
System.out.println(str1 == str2);  // false


NOTE: compare string objects using equals() method. Never using equality operator.