What is the difference between string object and string literal?

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

1 answer 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

Related Questions In Java

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
408 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
98 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,040 points
34 views
0 votes
1 answer

What is the difference between Set and List in java?

List is an ordered sequence of elements. ...READ MORE

answered Apr 26, 2018 in Java by Akrati
• 3,150 points
63 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered Apr 30, 2018 in Java by sharth
• 3,320 points
79 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 6,156 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,050 points
1,619 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,050 points
4,317 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,050 points
334 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
22 views