tag:blogger.com,1999:blog-8712770457197348465.post2825044899940058438..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: When to use intern() method of String in Java? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-8712770457197348465.post-30465114033851946612021-05-16T18:59:32.468-07:002021-05-16T18:59:32.468-07:00Further, to @Samlan-Khandu,
If there is space bet...Further, to @Samlan-Khandu,<br /><br />If there is space between "Hello World", then the result is true, strangely:<br /><br /><br /> String s1 = "Hello".concat("World");<br /> String s3 = new String("Hello World"); // Line-2<br /> String s2 = s1.intern();<br /> System.out.println(s1 == s2); // true<br /> Lokeshwarhttps://www.blogger.com/profile/02951562658560392932noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72172459232791231062020-12-03T05:36:44.308-08:002020-12-03T05:36:44.308-08:00Two objects "abc" first object created i...Two objects "abc" first object created in pool and "s" a new object created in heap. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69939072901787834912020-12-03T01:38:12.686-08:002020-12-03T01:38:12.686-08:00How many object are created by this line String s ...How many object are created by this line String s = new String("abc");? Can anyone please explain it clearly?jdamithttps://www.blogger.com/profile/03284346410612725344noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-68912587367632037882019-11-01T06:59:06.454-07:002019-11-01T06:59:06.454-07:00Don't use it in java 8+, it clutters up intern...Don't use it in java 8+, it clutters up internal JDK tables https://shipilev.net/jvm/anatomy-quarks/10-string-intern/Anonymoushttps://www.blogger.com/profile/15911775120327701568noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-41599718037298401722018-09-27T02:41:04.461-07:002018-09-27T02:41:04.461-07:00if use string.intern() in web frame, how to releas...if use string.intern() in web frame, how to release the string constant, or the constant pool become very large.Anonymoushttps://www.blogger.com/profile/06976949201254974016noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-31311108501418839312018-09-22T06:01:19.455-07:002018-09-22T06:01:19.455-07:00Hello @Ana, if you programatically create string e...Hello @Ana, if you programatically create string e.g. "abc" + "def" then it won't be created in the pool. In that case, if you want, you can add them into pool by using intern() method. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14967558143622449162018-09-21T23:54:43.188-07:002018-09-21T23:54:43.188-07:00In such case, why do i even need intern()? The bes...In such case, why do i even need intern()? The best way to create a String object is by declaring it this way-><br /><br />String a="abc";<br /><br />instead of <br />String a = new String("abc");Anonymoushttps://www.blogger.com/profile/11972124267243655726noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33812318804525953462017-07-12T06:18:01.316-07:002017-07-12T06:18:01.316-07:00Hello salman, intern() method put a String into St...Hello salman, intern() method put a String into String pool if it doesn't exist and return the reference of same String, but if a similar string exists e.g. which is equal using equals() method then reference of that String is returned from pool.<br /><br />In first case, because you have "HelloWorld" as literal, it already exists in pool before you call s1.intern() that's why javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11929487735486605522017-07-11T21:49:10.875-07:002017-07-11T21:49:10.875-07:00String s1 = "Hello".concat("World&q...String s1 = "Hello".concat("World"); <br />String s3 = new String("HelloWorld"); //Line-2 <br />String s2 = s1.intern(); <br />System.out.println(s1 == s2); //false <br />System.out.println(s1 == s3); //false <br />System.out.println(s2 == s3); //false <br /><br />If I removed Line-2 and compare s1==s2, it will return true. Could anyone explain me what exactly Salman Khanduhttps://www.blogger.com/profile/17045952676327560478noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-35210529989642720222015-12-15T19:55:11.507-08:002015-12-15T19:55:11.507-08:00String interning is intended for saving memory if ...String interning is intended for saving memory if you have many strings with the same content in you application. By using String.intern() the application will only have one instance in the long run and a side effect is that you can perform fast reference equality comparison instead of ordinary string comparison (but this is usually not advisable because it is really easy to break by forgetting SARAL SAXENAhttps://www.blogger.com/profile/01084233786047386880noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12108828681164860732015-12-15T19:54:10.758-08:002015-12-15T19:54:10.758-08:00Just to Add : -
When would I use this function in...Just to Add : -<br /><br />When would I use this function in favor to String.equals()<br />when you need speed since you can compare strings by reference (== is faster than equals )SARAL SAXENAhttps://www.blogger.com/profile/01084233786047386880noreply@blogger.com