{"id":1718,"date":"2016-09-22T09:36:28","date_gmt":"2016-09-22T09:36:28","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1718"},"modified":"2016-09-22T10:59:33","modified_gmt":"2016-09-22T10:59:33","slug":"string-interview-questions","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/string-interview-questions\/","title":{"rendered":"String Interview Questions"},"content":{"rendered":"<p><strong>StringEquals.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.apryll.package1;\r\n\r\npublic class StringEquals \r\n{\r\n  public static void main(String&#x5B;] args) \r\n  {\r\n\tjack();\r\n\tjill();\r\n  }\r\n\r\n  public static void jack() \r\n  {\r\n\tString s1 = &quot;hill5&quot;;\r\n\tString s2 = &quot;hill&quot; + &quot;5&quot;;\r\n\tSystem.out.println(s1 == s2);\r\n  }\r\n\r\n  public static void jill() \r\n  {\r\n\tString s1 = &quot;hill5&quot;;\r\n\tString s2 = &quot;hill&quot; + s1.length();\r\n\tSystem.out.println(s1 == s2);\r\n  }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\ntrue\r\n5\r\nfalse\r\n<\/pre>\n<p><strong>Reason<\/strong><br \/>\nBecause, when a string object is instantiated it cross checks with a memory pool if that String is already created. If it is already created then it maps the new variable to the already existing variable. Therefore both these variables are same objects. <strong>In jack() at compile time java knows that both the object<\/strong> are same and uses the same instance for referring to it.<\/p>\n<p>In jill First \u201chill\u201d is created as a String object, then a new StringBuilder object is created which will help in concatenating two values. After the concatenation is done then it is converted back to a new String object. <strong>Real values are known at runtime only<\/strong>.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p><strong>StringClass.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class StringClass \r\n{\r\n\tpublic static void main(String&#x5B;] args) \r\n\t{\r\n\t\tint a = 10 + 20;\r\n\t\tSystem.out.println(a);\r\n\t}\r\n\r\n}\r\n\r\nclass String \r\n{\r\n\tprivate final String str;\r\n\r\n\tpublic String(String str) \r\n\t{\r\n\t\tthis.str = str;\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nError: Main method not found in class StringClass, please define the main method as:\r\n   public static void main(String[] args)\r\n...\r\n<\/pre>\n<p><strong>Reason<\/strong><br \/>\nWe do not have a main method with the expected signature. \u201cmain\u201d method should have a String array as argument, but in our code the String array is compiled to be our custom String class and not the \u201cjava.lang.String\u201d class. Therefore we get the error as main method missing.<\/p>\n<p><strong>Fix<\/strong><br \/>\nChange the main method signature as public static void main(java.lang.String[] args). <\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n","protected":false},"excerpt":{"rendered":"<p>StringEquals.java package com.apryll.package1; public class StringEquals { public static void main(String&#x5B;] args) { jack(); jill(); } public static void jack() { String s1 = &quot;hill5&quot;; String s2 = &quot;hill&quot; + &quot;5&quot;; System.out.println(s1 == s2); } public static void jill() { String s1 = &quot;hill5&quot;; String s2 = &quot;hill&quot; + s1.length(); System.out.println(s1 == s2); } }&hellip; <a href=\"https:\/\/codethataint.com\/blog\/string-interview-questions\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[201],"tags":[],"class_list":["post-1718","post","type-post","status-publish","format-standard","hentry","category-interview-questions-strings"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1718"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1718\/revisions"}],"predecessor-version":[{"id":1727,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1718\/revisions\/1727"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}