String Literal Vs String Object in Java Last Updated : 05 May, 2025 Comments Improve Suggest changes Like Article Like Report Understanding the difference between String Literals and String Objects in Java plays a very important role. The main difference between String Literal and String Object is listed below:String Literal: A sequence of characters inside double quotes is known as a String Literal. String Literals are stored in a special area, and it is known as the String Pool. String Literals are immutable.String Object: String Objects are created with the help of the new keyword, and they can be stored anywhere in memory. String Objects are not stored in the String Pool area.Difference Between String Literals and String ObjectFeaturesString LiteralString ObjectMemory LocationIt is stored in the String Pool area.It is stored in the heap area.CreationIt is created directly, we just need to assign a value to a variableFor example: String s = "GeeksforGeeks"It is created with the help of a new keyword.For example: s = new String ("GeeksforGeeks")ImmutablitiyIt is immutable, it simply means it can not be changed once created.It is also immutable but can be modified indirectly with the help of StringBuilder or other methodsPerformanceIt is faster.It is slower.ComparisonTwo string literals with the same value point to the same object in memory.Two string objects with the same value may point to different objects in memory.Now, we are going to understand how String Literal and String Objects actually works in Java.String LiteralsIn Java, we can use String Literals to initialize a string. A sequence of characters enclosed in double quotes is known as a String Literal.Declaration:String s = "Hello GeeksforGeeks"This string is going to be stored in a special area known as String pool area. The JVM checks if the string already exist in the string pool area or not. If it is already present it will not add and if it is not present, it adds the new string to the pool. This helps avoiding the multiple copies of same string in the memory.String ObjectIn Java, we can use String Object to initialize a string. With the help of new keyword we can create string object.Declaration:String str = new String("GeeksForGeeks")This is string object. In this method JVM is forced to create a new string reference, even if "GeeksForGeeks" is in the reference pool. Therefore, if we compare performance of string literal and string object, string object will always take more time to execute than string literal because it will construct a new string every time it is executed. Note: Execution time is compiler dependent. Below is the Java program to compare their performances. Example: Java // Java program to compare performance // of string literal and string object class Geeks { public static void main(String args[]) { // Initialization time for String // Literal long start1 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { String s1 = "GeeksForGeeks"; String s2 = "Welcome"; } long end1 = System.currentTimeMillis(); long total_time = end1 - start1; System.out.println("Time taken to execute"+ " string literal = " + total_time); // Initialization time for String // object long start2 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { String s3 = new String("GeeksForGeeks"); String s4 = new String("Welcome"); } long end2 = System.currentTimeMillis(); long total_time1 = end2 - start2; System.out.println("Time taken to execute"+ " string object=" + total_time1); } } OutputTime taken to execute string literal = 0 Time taken to execute string object=5 Comment More infoAdvertise with us Next Article String Literal Vs String Object in Java N NishuAggarwal Follow Improve Article Tags : Java DSA Java-Strings Practice Tags : JavaJava-Strings Similar Reads Object toString() Method in Java Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth, it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object, and if it extends another class, then it is 3 min read Storage of String in Java In Java, we know both Stack and Heap space are part of Java Virtual Machine (JVM). However, these memory spaces are used for different purposes. Stack space contains specific values that are short-lived whereas Heap space is by Java Runtime to allocate memory to objects and JRE classes. In Java, str 3 min read equals() on String and StringBuffer objects in Java Consider the following codes in java: Java // This program prints false class GFG { public static void main(String[] args) { StringBuffer sb1 = new StringBuffer("GFG"); StringBuffer sb2 = new StringBuffer("GFG"); System.out.println(sb1.equals(sb2)); } } Output: false Java // This 1 min read String vs StringBuilder vs StringBuffer in Java A string is a sequence of characters. In Java, String objects are immutable, which simply means once created, their values can not be changed. In Java, String, StringBuilder, and StringBuffer are used for handling strings. The main difference is:String: Immutable, meaning its value cannot be changed 6 min read StringBuffer vs StringBuilder in Java Java provides three classes to represent the sequence of characters i.e. String, StringBuffer, and StringBuilder. A string is immutable in Java whereas, both StringBuffer and StringBuilder are mutable. This means they allow modifications to the character sequence without creating new objects.The mai 4 min read Overriding toString() Method in Java Java being object-oriented only deals with classes and objects so do if we do require any computation we use the help of object/s corresponding to the class. It is the most frequent method of Java been used to get a string representation of an object. Now you must be wondering that till now they wer 3 min read Convert Set of String to Array of String in Java Given a Set of Strings, the task is to convert the Set into an Array of Strings in Java. Examples: Input: Set<String>: ["ForGeeks", "A Computer Portal", "Geeks"] Output: String[]: ["ForGeeks", "A Computer Portal", "Geeks"] Input: Set<String>: ["G", "e", "k", "s"] Output: String[]: ["G", 6 min read String toString() Method in java with Examples String toString() is the built-in method of java.lang which return itself a string. So here no actual conversion is performed. Since toString() method simply returns the current string without any changes, there is no need to call the string explicitly, it is usually called implicitly. Syntax : publ 1 min read When to use Rope over StringBuilder in Java? What are Ropes? Rope is a binary tree data structure where each node except the leaf contains the number of characters present to the left of the node. They are mainly used by text editors to store and manipulate large strings. It provides different string operations such as append, insert and delet 3 min read StringBuffer toString() method in Java with Examples The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer object and then String is returned by toString(). Sub 2 min read Like