tag:blogger.com,1999:blog-694855878384792308.post5086152910667818979..comments2024-03-21T06:26:49.387-07:00Comments on Java67: Difference between String literal and New String object in Javajavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger51125tag:blogger.com,1999:blog-694855878384792308.post-21190453595856177082021-04-26T00:10:36.405-07:002021-04-26T00:10:36.405-07:00Yes, heap object is eligible to garbage collection...Yes, heap object is eligible to garbage collection if there is no reference, object inside pools are not, but thankfully string pool from java7 is migrated to heap which means they can be garbage collected. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-60894040391842070862021-03-04T04:41:05.757-08:002021-03-04T04:41:05.757-08:00Hello Javin,
So, string literal "abc" i...Hello Javin,<br /><br />So, string literal "abc" is now eligible for garbage collection if we do not use inter() explicitly as no reference to it by str. Correct me if I'm going in a wrong directions please.Anonymoushttps://www.blogger.com/profile/06871819776379873527noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-74326746415846547432020-10-24T06:23:41.820-07:002020-10-24T06:23:41.820-07:00why both false, first should be true, second shoul...why both false, first should be true, second should be false as first will do content matching while second will check if both are same object using memory reference. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-10929057809412776442020-10-24T06:22:57.776-07:002020-10-24T06:22:57.776-07:00I think 5, "a", "b", "c&q...I think 5, "a", "b", "c", "abc" and new String()javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-57746092578889678212020-10-19T12:42:05.110-07:002020-10-19T12:42:05.110-07:00i think 4 objecti think 4 objectrakesh roshanhttps://www.blogger.com/profile/06077629735375181641noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-39040781721163568332020-10-19T12:40:08.430-07:002020-10-19T12:40:08.430-07:00both falseboth falserakesh roshanhttps://www.blogger.com/profile/06077629735375181641noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-41259164555102969152020-06-23T17:23:34.794-07:002020-06-23T17:23:34.794-07:00No. In that case it wouldn't create object on ...No. In that case it wouldn't create object on the Heap exclusively.Rangonoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-12867302424110132672020-05-08T06:12:33.237-07:002020-05-08T06:12:33.237-07:00Hello OMER,
When you write String str = new String...Hello OMER,<br />When you write String str = new String ("abc") there are two objects created here, string literal "abc" which is created in String pool and an object refereed by str, both contains "abc" but one is in heap and other is in string pool. if you do str.intern() then it will refer to same object as "abc". You can try it yourself by comparing using == operator to find out more.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-57566533443028988352020-05-01T15:42:43.142-07:002020-05-01T15:42:43.142-07:00I think there are false sentences in the article. ...I think there are false sentences in the article. Or I may have misunderstood me. When I search on the internet, when I say new String ("abc"), 2 values are created in memory, including a heap and a String pool. I agree that there is no value in the pool.<br />Then the following sentences in the article attract my attention. And I can not understand. It says that it will not be put in the pool without calling the intern () method. Do I get it wrong?<br /><br />"Java by default doesn't put all String object into String pool, instead they gives you flexibility to explicitly store any arbitrary object in String pool. You can put any object to String pool by calling intern() method of java.lang.String class."<br /><br />According to these sentences, when I say new String ("abc"), if I don't call the intern () method, it will not be put in the pool. However, it does not write like this on the Internet.Can you explain it to me?OMERhttps://www.blogger.com/profile/08564795173879846538noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-91493285896592677332019-12-17T05:07:47.334-08:002019-12-17T05:07:47.334-08:00Please read the article, they are different object...Please read the article, they are different object and == only return true for same object, not same content of different object, which equals does.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-53958119117778653822019-12-11T03:19:24.059-08:002019-12-11T03:19:24.059-08:00String j = "Java";
String f = ne... String j = "Java";<br /> String f = new String("Java");<br /> <br /> System.out.println(j.equals(f)); // true<br /> System.out.println(j==f); // false Why? If f doesn't create a new object and uses reference of j. Then why false?Anonymoushttps://www.blogger.com/profile/11601353457645509086noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-28849715884666979442019-12-11T02:58:27.187-08:002019-12-11T02:58:27.187-08:00String j = "Java";
String f = ne... String j = "Java";<br /> String f = new String("Java");<br /> <br /> System.out.println(j.equals(f));<br /> System.out.println(j==f);Anonymoushttps://www.blogger.com/profile/11601353457645509086noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-88094777737544583592019-08-31T08:48:41.939-07:002019-08-31T08:48:41.939-07:00String a="a";
String b="b";
St...String a="a";<br />String b="b";<br />String c=a+b+"c";<br />String d=new String ();<br /><br />How many string objects are created in this code?Anonymoushttps://www.blogger.com/profile/14673158204040813142noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-13829131409720499512019-04-09T05:03:09.758-07:002019-04-09T05:03:09.758-07:00new String("..") always creates an objec...new String("..") always creates an object in heap we all know<br />but the question is ,is it also check in String pool.???<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-21441881177794560182018-06-05T05:45:16.125-07:002018-06-05T05:45:16.125-07:00yes Sridhar , me too having same point can anyone ...yes Sridhar , me too having same point can anyone please clear me.Anonymoushttps://www.blogger.com/profile/03356707916464873264noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-46387081551986893862017-09-14T00:04:38.347-07:002017-09-14T00:04:38.347-07:00why they need it
why they create two different typ...why they need it<br />why they create two different type String object..<br />in case of String str=new String("Hello");<br />two String object is created...why it need...nirajhttps://www.blogger.com/profile/03991305828357342644noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-92060094481131440062017-09-08T23:58:46.389-07:002017-09-08T23:58:46.389-07:00agreed.agreed.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-43279566808429293192017-08-31T23:08:27.353-07:002017-08-31T23:08:27.353-07:00Very good explanation, thank you!
But this is an ...Very good explanation, thank you!<br /><br />But this is an exception, a trick and any exception makes our life harder and it pales the beauty of Java. <br />Why Java designers did not automatically decide if a string in PermGen to use it no matter it is literal or new object? <br />So if after a <br />String s1="Java"; <br />//coming with <br />String s2 = new String("Java"); <br /> to return also s1 as it is already there?<br /><br />This in idea that Java is a tool in our hands not vice-verse. <br />And we prefer simple but effective tools. <br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-70395351581588851492017-07-03T03:15:25.950-07:002017-07-03T03:15:25.950-07:00Nope, until you explicitly call intern() method, i...Nope, until you explicitly call intern() method, it will create one object in Heap space only.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-65560068244389246902016-11-16T03:47:26.972-08:002016-11-16T03:47:26.972-08:00String a = "one";
String b = "two...String a = "one";<br /> String b = "two";<br /> String c = a+b;<br /> String d = "one"+"two";<br /> String e = "onetwo";<br /> if(c==d){<br /> System.out.println(true);//1st<br /> }else if(c==e){<br /> System.out.println(true);//2nd<br /> }else if(d==e){<br /> System.out.println(true);//3rd<br /> }else{<br /> System.out.println(true);//4th<br /> }<br /><br /><br />Explain Mr. Javin Paul,<br />1.Why 3rd will be true and why not 1/2?<br />2. Afetr concatenation will new object will be created in heap/pool? <br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-73632460778924020052016-07-26T17:41:52.486-07:002016-07-26T17:41:52.486-07:00hello @Himanshu, it will be returned when you crea...hello @Himanshu, it will be returned when you create another object using same character but by using String literal e.g. "abc" will return the same object.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-9673853566514656632016-07-26T17:40:42.814-07:002016-07-26T17:40:42.814-07:00@true taller yes, if you create an object using ne...@true taller yes, if you create an object using new String() without using String literal e.g. via byte array then that object only remain in heap space, not created in String pool.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-56193089983427871562016-07-26T17:39:38.910-07:002016-07-26T17:39:38.910-07:00@Anonymous, 4 objects, two String literal and two ...@Anonymous, 4 objects, two String literal and two created by String concatenation.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-67682435352135644482016-07-26T17:38:59.261-07:002016-07-26T17:38:59.261-07:00@Anonymous, yes result will be true because both a...@Anonymous, yes result will be true because both are pointing to same String literal.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-71081282630600945882016-07-26T17:37:56.190-07:002016-07-26T17:37:56.190-07:00Hello @Anonymous, the intern() method has no effec...Hello @Anonymous, the intern() method has no effect on existing object. Now, if you create another object with literal e.g. "intern" then same object is returned instead of creating a new one. See my post <a href="http://javarevisited.blogspot.com/2015/12/when-to-use-intern-method-of-string-in-java.html" rel="nofollow">when to use intern() method of String in Java</a> to learn more about internjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.com