What is the difference between string object and string literal?

0 votes
Feb 28 in Java by Phalguni
• 1,010 points
112 views

2 answers to this question.

0 votes

Difference between string objects and string literals

A string literal in Java is basically a sequence of characters from the source character set used by Java programmers to populate string objects or to display text to a user. These characters could be anything like letters, numbers or symbols which are enclosed within two quotation marks.

A string object is one which lets you work with a series of characters. It basically wraps all Javascript’s string primitive data type which a number of helper methods. The main function of JavaScript is it automatically converts between string primitives and string objects.

The main difference between String objects and string literals is mentioned below:

String Objects

String Literals

A new keyword is used to create an object and the object is created in the heap memory whereas its reference will be pointed to String pool.

Here the reference will be directly referred to String Pool. JVM initially checks for the availability with the same value in the constant pool

Here is an illustration of a Java Program that compares their performances.

  

class ComparePerformance {
      public static void main(String args[])
    {    
        long start1 = System.currentTimeMillis();
                  for (int i = 0; i < 10000; i++)
        {
            String s1 = "Hello World";
            String s2 = "Welcome";
        }
                 long end1 = System.currentTimeMillis();
        long total_time = end1 - start1;
          System.out.println("Time taken to execute"+ 
                " string literal = " + total_time);
                long start2 = System.currentTimeMillis();
                 for (int i = 0; i < 10000; i++)
        {
            String s3 = new String("Hello World");
            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);
    }
}
answered Feb 28 by Avantika
• 1,500 points

edited Mar 15 by Kalgi
0 votes

String literal is a Java language concept. This is a String literal:

"a String literal"

String object is an individual instance of the java.lang.String class.

String s1 = "abcde";
String s2 = new String("abcde");
String s3 = "abcde";

All are valid, but have a slight difference. s1 will refer to an interned String object. This means, that the character sequence "abcde" will be stored at a central place, and whenever the same literal "abcde" is used again, the JVM will not create a new String object but use the reference of the cached String.

s2 is guranteed to be a new String object, so in this case we have:

s1 == s2 // is false
s1 == s3 // is true
s1.equals(s2) // is true
answered Aug 16 by Sirajul
• 25,180 points

Related Questions In Java

0 votes
1 answer

What is the difference between main(String[] args) and main(String... args) in Java?

String[] is used to accept a single parameter ...READ MORE

answered Sep 6 in Java by Piya
18 views
0 votes
2 answers

What is the difference between implements and extends?

Extends : This is used to get attributes ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,190 points
497 views
0 votes
1 answer

What is the difference between jdk and jre?

JRE: It stands for Java Runtime Environment. ...READ MORE

answered Apr 20, 2018 in Java by Akrati
• 3,150 points
131 views
0 votes
2 answers

What is the difference between = and equals()?

The equals() method compares the "value" inside String instances ...READ MORE

answered Aug 13, 2018 in Java by Daisy
• 8,050 points
52 views
0 votes
2 answers

What is the difference between Set and List in java?

LIST SET Duplicates YES NO Order Ordered Depends on Implementation Positional Access YES NO List is an ordered sequence ...READ MORE

answered Aug 14 in Java by Sirajul
• 25,180 points
103 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 7,967 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
1,952 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
5,080 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
482 views
0 votes
1 answer

What is integer boxing in java?

In Java the boxing range for any ...READ MORE

answered Mar 6 in Java by Avantika
• 1,500 points
33 views