String-literal-common-pool-1024x586

What is the difference between String Literal and String Object?

String Literals vs String Objects

String is associated with string literal in the form of double-quoted text. You can assign a string literal directly into a String variable. String objects are created by new operator and String constructor. However, this is not recommended in Java.

Strings are immutable

String is an immutable sequence of Unicode characters. It’s content cannot be modified. Many string functions appear to modify the contents, but they construct and return a new String. Strings are frequently used in programs.

There are two ways to construct a string. Implicit construction by assigning a string literal .Explicitly construction of a String object via the new operator and constructor. Examples below:

1.By assigning a string literal to a String reference
2. By new operator and constructor as shown below.

For example,

String s1 = “I’m IronMan”; // Implicit construction
String s2 = new String(“”); // Explicit construction

String literals are stored in string common pool. String objects allocated via new operator are stored in the program heap memory. In the above example s1 is stored in the string common pool. s2 object is stored in the heap memory.

Storage mechanism

If two string literals have the same contents then they share the same storage in the string common pool.

1.String s1 = “TestingDocs”;

2.String s2 = “TestingDocs”;

 

String literal common pool

String objects created via the new operator are kept in the heap memory. Each String object has its own storage just like any other object. There is no sharing of storage in heap even if two String objects have the same contents as shown below.

String s3 = new String(“TestingDocs”);

String s4 = new String(“TestingDocs”);

 

String Object in Heap

You can use the method equals() of the String class to compare the contents of two Strings. You can use the relational equality operator ‘==’ to compare the references of two objects.

s1 == s2;         // true
s1.equals(s2);    // true
s3 == s4;         // false
s3.equals(s4);    // true

 

Related Posts

Google-Auto-Suggestion-Box-1024x555

Automation

Write a java program to capture auto suggestions list

Tomcat-deployment

Automation

How to deploy a .WAR file in Tomcat using Eclipse?

ArrayList-in-Java-768x497

Automation

What is an ArrayList?

Servlet-Running-on-Tomcat

Automation

How to create a Sample Servlet using Eclipse?

2FA-GitHub-account-1024x363

Automation

How to enable 2FA in GitHub?

Simple-JavaFX-Application-1024x524

Automation

What is JavaFX API?

Navigation

  • Home
  • Selenium
  • JBehave
  • TestNG

Random Questions

  • Tomcat-deploymentHow to deploy a .WAR file in Tomcat using Eclipse?
  • Google-Auto-Suggestion-Box-1024x555Write a java program to capture auto suggestions list
  • Use Mathematical Induction to prove the statement
  • Array Frequency CountWrite a java program to find frequency count for an item in the array?
  • How to fork a repository on GitHub?
  • Singleton Java programHow to create a Singleton class?
  • Finite-State-System-Puzzle1-1024x570Person, Goat, Wolf, and Cabbage Problem
  • Commons CLI libraryWhat is Commons CLI library?
  • StringTokenizer Class HeirarchyWhat is StringTokenizer?
  • Multiplication TableWrite a java program to compute multiplication table?
  • Write a Java Program for Binary Search?Write a Java Program for Binary Search?
  • How to perform code coverage with Eclipse?
  • Vowels in String LocatorWrite a java program to find vowels in a string?
  • javadocHow to Generate Javadoc for a Project?
  • Stacks-with-positive-and-negative-numbers-1024x556Design a Stack to separate positive and negative values
TestingDocs.com
  • Home
  • Selenium
  • JBehave
  • TestNG
© 2016 - 2019 TestingDocs.com All Rights Reserved
TestingDocs.com

TestingDocs.com

Links

  • Home
  • Selenium
  • JBehave
  • TestNG

Search