String Literal Vs String Object in Java
Compare string initialization performance for String Literal and String object.
String Literal
String str = “GeeksForGeeks”;
This is string literal. When you declare string like this, you are actually calling intern() method on String. This method references internal pool of string objects. If there already exists a string value “GeeksForGeeks”, then str will reference of that string and no new String object will be created. Please refer Initialize and Compare Strings in Java for details.
String Object
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.
// Java program to compare performance // of string literal and string object class ComparePerformance { 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); } } |
Output:
Time taken to execute string literal = 0 Time taken to execute string object = 2
Recommended Posts:
- Convert Java Object to Json String using GSON
- Convert Java Object to Json String using Jackson API
- Convert Json String to Java Object Using GSON
- PrintWriter printf(String, Object) method in Java with Examples
- PrintWriter format(String, Object) method in Java with Examples
- PrintStream printf(String, Object) method in Java with Examples
- PrintStream format(String, Object) method in Java with Examples
- PrintStream printf(Locale, String, Object) method in Java with Examples
- PrintStream format(Locale, String, Object) method in Java with Examples
- PrintWriter format(Locale, String, Object) method in Java with Examples
- PrintWriter printf(Locale, String, Object) method in Java with Examples
- Convert a List of String to a comma separated String in Java
- Convert an ArrayList of String to a String array in Java
- Convert a Set of String to a comma separated String in Java
- Convert Set of String to Array of String in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



