tag:blogger.com,1999:blog-8712770457197348465.post4238161145713353038..comments2024-03-29T00:43:22.798-07:00Comments on Javarevisited: 10 Things Every Java Programmer Should Know about Stringjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-8712770457197348465.post-6417155221992463192015-05-19T22:27:54.637-07:002015-05-19T22:27:54.637-07:00Hi
I want to ask
if i created string object like...Hi <br />I want to ask <br />if i created string object like this<br />String s = "hello";<br />-> "hello" string literal go into string literal pool<br />and if create string object like this<br />String s1 = new String("hello");<br />-> then "hello" string literal where it store by JVM<br />on string literal pool? or on heap?<br />i confused becauseAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-18826432537648792042015-04-24T08:37:50.740-07:002015-04-24T08:37:50.740-07:00what is the function of string?????what is the function of string?????Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54747039939854163142014-08-29T19:56:53.164-07:002014-08-29T19:56:53.164-07:00Few more things to add into this excellent article...Few more things to add into this excellent articles :<br /><br />1) String are stored as UTF-16 characters and not UTF-8, may be some day they will move to UTF-32 as well. <br /><br />2) JVM does not intern all strings created by Java code, only String literals are interned. String created using new() is not interned until you explicitly call intern method on them. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-52878974631930531272014-03-25T07:53:02.317-07:002014-03-25T07:53:02.317-07:00
as already have been answered the second retrie... <br /><br />as already have been answered the second retrieves the instance from the String pool (remember Strings are immutable).<br /><br />Additionally you should check the intern() method which enables you to put new String() into a pool in case you do not know the constant value of the string in runtime: e.g:<br /><br />String s = stringVar.intern();<br /><br />or<br /><br />new String(SARAL SAXENAhttps://www.blogger.com/profile/01084233786047386880noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29728399115322763072014-03-25T07:50:17.660-07:002014-03-25T07:50:17.660-07:00@Javin Very nice article just want to add one more...@Javin Very nice article just want to add one more thing to add the value to this article that is ..<br /><br /><br /><br />In the first case, a new object is being created in each iteration, in the second case, it's always the same object, being retrieved from the String constant pool.<br /><br />In Java, when you do:<br /><br />String bla = new String("xpto");<br /><br />You forceSARAL SAXENAhttps://www.blogger.com/profile/01084233786047386880noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2596967807037707992014-03-20T05:56:12.323-07:002014-03-20T05:56:12.323-07:00I read that When
String s = new String("JAVA...I read that When <br />String s = new String("JAVA"); instruction is executed,<br />JVM checks SCP for "JAVA" , if SCP already holds a reference to "JAVA" , then only one object in heap will be created other wise two objects will be created in heap where one is referenced in SCP and one will be in String. Please answer .. Reference link http://stackoverflow.com/Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-68043486524791742642014-03-01T10:26:02.159-08:002014-03-01T10:26:02.159-08:004) Advice to prefer StringBuilder is good for when...4) Advice to prefer StringBuilder is good for when dealing with single threads only. Otherwise, if multiple threads could be accessing it, a StringBuffer (being synchronised) may be what you are after.Anonymoushttps://www.blogger.com/profile/06453119270643853541noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-464805962102855682014-02-25T22:23:07.714-08:002014-02-25T22:23:07.714-08:00Hi Sanoj,
as per example .GC will not clear the a...Hi Sanoj,<br /><br />as per example .GC will not clear the abc, because String declaration and creation done by string literal <br />inside double quotes are String literal in Java are maintained in a separate String pool, special memory location inside java memory .<br /><br />In String pool will not clean abc immediately .. if String pool(max 200 object will stored ) memory is full then Anonymoushttps://www.blogger.com/profile/14575034994556206495noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-73199390721197236422014-01-25T18:48:33.623-08:002014-01-25T18:48:33.623-08:00what happens when we use
String str1="abc&qu...what happens when we use<br /><br />String str1="abc"<br />str1="xyz";<br /><br />GC will clean abc or that will be in literal pool?sanojnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78711050187224693112014-01-18T12:29:03.242-08:002014-01-18T12:29:03.242-08:00Regarding the PermGen comments.
From Oracle's...Regarding the PermGen comments.<br /><br />From Oracle's Java SE 7 Features and Enhancements:<br /><br />"In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application."<br /><br />Garbage Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58691640610970417512014-01-07T12:30:05.153-08:002014-01-07T12:30:05.153-08:00From Java 7u6 onwards, the substring() method does...From Java 7u6 onwards, the substring() method does NOT use the underlaying char array anymore, it DOES copy the part of the array it needs.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-86821075887566552082014-01-02T22:47:27.904-08:002014-01-02T22:47:27.904-08:00Point (1) should say you can get *a copy of* the c...Point (1) should say you can get *a copy of* the character array used to represent String in Java by calling toCharArray().DerHeiligstehttps://www.blogger.com/profile/07074727197158598993noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62905552052882929432013-07-30T09:47:46.942-07:002013-07-30T09:47:46.942-07:00i thank millions && thank alot && ...i thank millions && thank alot && thank you "the writer" so much. i love java since i don't know what is java. i m trying for SCJP now. Your posts are very useful && understandable for me. SO THANKS!!!<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12949076185295092512013-07-20T08:56:35.812-07:002013-07-20T08:56:35.812-07:00I love the fact that in Java, developers are more ...I love the fact that in Java, developers are more mindful of writing immutable classes. I don't appreciate it before, until of course running into problems later due to concurrency.<br /><br />It is fun and educational to learn about programming reading the source code of Java, such as the String class.grails cookbookhttp://grails.asianoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77863234108500204162013-07-17T22:13:56.153-07:002013-07-17T22:13:56.153-07:00Indeed String is a Special class and special knowl...Indeed String is a Special class and special knowledge of String helps a lot. Just to add on this article, I would like to share couple of best practices while I am here :<br /><br />1) While calling equals() method with String literal, prefer defensive approach e.g. calling equals() on String literal rather than on String object e.g.<br /><br />"USA".equals(country) will return false Anonymousnoreply@blogger.com