{"id":1331,"date":"2018-05-06T08:33:18","date_gmt":"2018-05-06T08:33:18","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1331"},"modified":"2024-08-09T09:09:09","modified_gmt":"2024-08-09T09:09:09","slug":"what-is-the-difference-between-string-literal-and-string-object","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-is-the-difference-between-string-literal-and-string-object\/","title":{"rendered":"What is the difference between String Literal and String Object?"},"content":{"rendered":"<h2><strong>String Literals vs String Objects<\/strong><\/h2>\n<p><strong>String<\/strong> is associated with a 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 a <strong>new<\/strong> operator and String constructor. However, this is not recommended in Java.<\/p>\n<h3><strong>Strings are immutable<\/strong><\/h3>\n<p><strong>String<\/strong> is an immutable sequence of Unicode characters. It&#8217;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.<\/p>\n<p>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:<\/p>\n<p>1.By assigning a string literal to a String reference<br \/>\n2. By new operator and constructor as shown below.<\/p>\n<p>For example,<\/p>\n<p>String s1 = \u201cI\u2019m IronMan\u201d; \/\/ Implicit construction<br \/>\nString s2 = new String(\u201c\u201d); \/\/ Explicit construction<\/p>\n<p>String literals are stored in string common pool. String objects allocated via the <strong>new<\/strong> 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.<\/p>\n<h3><strong>Storage mechanism<\/strong><\/h3>\n<p>If two string literals have the same contents then they share the same storage in the string common pool.<\/p>\n<p>1.String s1 = \u201cTestingDocs\u201d;<\/p>\n<p>2.String s2 = \u201cTestingDocs\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1462\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-literal-common-pool.png\" alt=\"String literal common pool\" width=\"1059\" height=\"606\" title=\"\"><\/p>\n<p>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 a heap even if two String objects have the same contents as shown below.<\/p>\n<p>String s3 = new String(\u201cTestingDocs\u201d);<\/p>\n<p>String s4 = new String(\u201cTestingDocs\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1463\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/String-Object-in-Heap.png\" alt=\"String Object in Heap\" width=\"1067\" height=\"608\" title=\"\"><\/p>\n<p>You can use the method equals() of the String class to compare the contents of two Strings. You can use the relational equality operator \u2018==\u2019 to compare the references of two objects.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">s1 == s2;         \/\/ true\r\ns1.equals(s2);    \/\/ true\r\ns3 == s4;         \/\/ false\r\ns3.equals(s4);    \/\/ true<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>String Literals vs String Objects String is associated with a 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 a new operator and String constructor. However, this is not recommended in Java. Strings are immutable String is an immutable sequence [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-1331","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1331"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1331\/revisions"}],"predecessor-version":[{"id":23859,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1331\/revisions\/23859"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}