What is the Difference Between String Literal and String Object in Java

The main difference between String Literal and String Object is that String Literal is a String created using double quotes while String Object is a String created using the new() operator.

String is a set of characters. Generally, it is necessary to perform String operations in most applications. Java is a high level, general-purpose programming language. Therefore, the programmer can write programs in Java to manipulate Strings. Furthermore, there are two types of String in Java as String Literal and String Object. Overall, the programmer can create a String literal by enclosing a set of characters inside double quotes. On the other hand, he can create a String object by using the new operator.

Key Areas Covered

1. What is String Literal in Java
      -Definition, Functionality
2. What is String Object in Java
     -Definition, Functionality
3. Difference Between String Literal and String Object in Java
     -Comparison of key differences

Key Terms

Java, String, String Literal, String Object

Difference Between String Literal and String Object in Java - Comparison Summary

What is String Literal in Java

It is possible to create a String instance inside double quotes. For example, “Hello World” is such a String instance. Thus, this kind of double quoted literal is a String literal. All these String literals are stored in a String pool.

Difference Between String Literal and String Object in Java

An example is as follows.

String s1 = “Hello World”;

Here, the s1 is referring to “Hello World” in the String pool.

Assume that there is another statement as follows.

String s2 = “Hello World”;

As “Hello World” already exists in the String pool, the s2 reference variable will also point to the already existing “Hello World” in the String pool. In other words, now both s1 and s2 refer to the same “Hello World” in the String pool. Therefore, if the programmer writes a statement as follows, it will display true.

System.out.println(s1==s2);

What is String Object in Java

String object is a string created using new operator is a String object. An example is as follows.

String s1 = new (“Hello World”);

String s2 = new (“Hello World”);

Unlike with String literals, in this case, there are two separate objects. In other words, s1 refers to one “Hello World” while s2 refers to another “Hello World”. Here, the s1 and s2 are reference variables that refer to separate String objects. Therefore, if the programmer writes a statement as follows, it will display false.

System.out.println(s1==s2);

Difference Between String Literal and String Object in Java

Definition

String literal in Java is a set of characters that is created by enclosing them inside a pair of double quotes. In contrast, String Object is a Java is a set of characters that is created using the new() operator. Thus, this explains the main difference between string literal and string object.

Functionality

In String literals, if the String already exists, the new reference variable will be pointing to the already existing literal. Even the String already exists or not, a new String object will be created. Hence, this is another difference between string literal and string object.

Syntax

Moreover, String s = “Hello World”; is the syntax for creating a String literal. On the other hand, String s = new String (“Hello World!”); is the syntax for creating a String object.

Conclusion

In brief, there are two ways to create a String in Java which are String Literal and String Object. The main difference between String Literal and String Object is that String Literal is a String created using double quotes while String Object is a String created using the new() operator.

References:

1.“Immutable String in Java – Javatpoint.” Www.javatpoint.com, Available here.

Image Courtesy:

1.”Denoting a single object in the Java environment” By Arun Reginald – Own work (CC BY-SA 3.0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply