What is the difference between string object and string literal?

0 votes
Feb 28 in Java by Phalguni
• 790 points
31 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,140 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
352 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
72 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,020 points
25 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
28 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,310 points
64 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,360 points

edited Nov 6, 2018 by Omkar 4,601 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,030 points
1,230 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,030 points
3,516 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,030 points
221 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,140 points
14 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.