tag:blogger.com,1999:blog-8712770457197348465.post2844597924670969182..comments2024-03-17T23:01:26.872-07:00Comments on Javarevisited: Why String is Immutable or Final in Java? Explainedjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger79125tag:blogger.com,1999:blog-8712770457197348465.post-74755064130086734842019-08-27T06:13:15.670-07:002019-08-27T06:13:15.670-07:00In java many value define like pie is 3.14 and if ...In java many value define like pie is 3.14 and if String is not immutable than if user use this than its also changed<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37162866959695748402019-02-28T01:30:32.153-08:002019-02-28T01:30:32.153-08:00I wanna ask a question then why Wrapper Class Obje...I wanna ask a question then why Wrapper Class Object are immutable...Mohit Vermahttps://www.blogger.com/profile/05338986065620121373noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-18876216839940185762019-02-28T01:29:28.600-08:002019-02-28T01:29:28.600-08:00"The string is Immutable in Java because Stri..."The string is Immutable in Java because String objects are cached in String pool" As per my knowledge this is wrong sentence the String is immutable so String object are cached in String pool.<br /><br />Mohit Vermahttps://www.blogger.com/profile/05338986065620121373noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16297638202408007552018-09-01T11:16:55.444-07:002018-09-01T11:16:55.444-07:00e.g. "Test" has referenced by many refer...<br />e.g. "Test" has referenced by many reference variables, so if any one of them change the value others will be automatically gets affected i.e. lets say<br /><br />String A = "Test"<br />String B = "Test"<br /><br />Now String B called, "Test".toUpperCase() which change the same object into "TEST", so A will also be "TEST" which is DJhttps://www.blogger.com/profile/04694837551673877483noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32643554534610180572018-09-01T09:19:13.293-07:002018-09-01T09:19:13.293-07:00Immutability certainly leads to more memory and CP...Immutability certainly leads to more memory and CPU usage if more and more new strings are created due to overcome immutability. In memory constrained and battery powered devices immutability would slow the system..<br /><br />But looks like the way JVM makes use of immutability of Strings, makes programs secure, safer and faster.Raja Nagendra Kumarhttps://www.blogger.com/profile/00155780059348792464noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29824299191197423952018-07-10T23:20:52.960-07:002018-07-10T23:20:52.960-07:00Actually new creates a new object each time. Liter...Actually new creates a new object each time. Literal, if the value exists in the string pool already then you will get that itself, instead of creating new object. So using Sting literal is always better and new should not be used until required.Anonymoushttps://www.blogger.com/profile/11176658292322330455noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33813130434263912812017-12-14T08:08:24.110-08:002017-12-14T08:08:24.110-08:00But in Java 8 there is no perm space then string ...But in Java 8 there is no perm space then string is still immutable<br />Anonymoushttps://www.blogger.com/profile/07909302903873386069noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-56669505583270325852017-10-16T19:57:10.420-07:002017-10-16T19:57:10.420-07:00Hi here you are adding two string var so here apee...Hi here you are adding two string var so here apeend operation will take place that’s the reason you se getting this output .initially s1 and s2 are two string object when you append it will create another block of mem in heap mem to store s1+s2Anonymoushttps://www.blogger.com/profile/04841048091682130085noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21806263972900212722017-07-29T03:34:47.887-07:002017-07-29T03:34:47.887-07:00One advantage of making String immutable is for sa...One advantage of making String immutable is for saving memory. When your program grows the number of String instances it creates also grows and if you don't cache String constants you end up with lots and lots of String in your heap space. By caching and sharing String constants JVM reduces lots of memory for real world Java applications.<br />Anonymoushttps://www.blogger.com/profile/05086615354593328714noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44266667799359680512017-07-16T02:53:07.130-07:002017-07-16T02:53:07.130-07:00Thanks for your article. Thanks for your article. Anonymoushttps://www.blogger.com/profile/10900472796646632913noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-20296376095317513762017-02-26T22:40:55.052-08:002017-02-26T22:40:55.052-08:00When jvm is loaded into memory as instance of java...When jvm is loaded into memory as instance of java.lang.Class,how much memory taken by jvm into the ram so that it can load the classes and perform their operationsAnonymoushttps://www.blogger.com/profile/00095886527076444001noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78430956709164052862016-08-21T06:31:32.897-07:002016-08-21T06:31:32.897-07:00As I understand String, I don't see any use of...As I understand String, I don't see any use of new operator while creating String object. Can any one explain where do we need new operator to create String?Rahul Kalenoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65258487340441880922015-12-12T05:55:49.811-08:002015-12-12T05:55:49.811-08:00All of those posters who say String isn't immu...All of those posters who say String isn't immutable because they can use introspection to change its private internal state need to read up on what a SecurityManager does during the reflection call to change its value. Then load one and try to change a String's underlying value. <br /><br />Just because we developers frequently ignore security doesn't mean that your code can count Anonymoushttps://www.blogger.com/profile/10939091044212106220noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33422707205851832422015-09-23T13:16:37.805-07:002015-09-23T13:16:37.805-07:00HI Javin,
Recently interviewer asked me What is t...HI Javin,<br /><br />Recently interviewer asked me What is the significance of creating String using new operator if we have string literal functionality for the same ?<br /><br />Do you have any thoughts on it ?<br /><br />ThanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-20491253309427702752015-08-30T06:24:32.427-07:002015-08-30T06:24:32.427-07:00@Sapna, Java designer knows that String will be us...@Sapna, Java designer knows that String will be used heavily in Java application and there would be so many String object all around which will take lot of memory. To avoid that they thought about sharing String object between clients and that's why they made it Immutable and created String pooljavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4405571665290254392015-08-24T21:06:17.570-07:002015-08-24T21:06:17.570-07:00I have a basic question, Why the String Pool is ac...I have a basic question, Why the String Pool is actually provided in java?Anonymoushttps://www.blogger.com/profile/02820685141805233894noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-52257236444535821362015-08-05T02:44:28.227-07:002015-08-05T02:44:28.227-07:00I believe Strings are immutable to make life easy ...I believe Strings are immutable to make life easy for JVM and java team did not do it for developer/development process. I do no think any of the points are of relevance as you can always use reflection to play around with values. Anonymoushttps://www.blogger.com/profile/11332272406563931740noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21928805232778793612015-06-13T20:44:33.062-07:002015-06-13T20:44:33.062-07:00@Anonymous, that's the hypothetical scenario I...@Anonymous, that's the hypothetical scenario I was explaining that why its MUST for String to be final if you want to have something like String pool. I hope it make sense to you now.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63331736250234492282015-06-13T10:52:34.957-07:002015-06-13T10:52:34.957-07:00This line dose not make sense
String A = "Te...This line dose not make sense<br /><br />String A = "Test"<br />String B = "Test"<br /><br />Now String B called "Test".toUpperCase() which change the same object into "TEST" , so A will also be "TEST" which is not desirable.<br /><br />If you do toUpperCase() to String B, why string A wll get affected?<br />In this example Strign A and String B Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24949524235184768662015-06-04T02:09:55.952-07:002015-06-04T02:09:55.952-07:00Good explanation,
need to ask one thing, Is string...Good explanation,<br />need to ask one thing, Is string pool stores the hashcode of the string or only value.<br />if value stores in string pool then how string hashcode is stored in memory. Ansuhttps://www.blogger.com/profile/08912595433416173774noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30132249662692899122015-05-19T23:54:07.955-07:002015-05-19T23:54:07.955-07:00@Keyur, in classloading name of class is passed as...@Keyur, in classloading name of class is passed as String, if String wasn't immutable its possible to change the name of that String from somewhere else, which can compromise security by loading different class. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-66100373734955589122015-04-27T16:35:25.419-07:002015-04-27T16:35:25.419-07:00sorry can you please add more to following, I coul...sorry can you please add more to following, I couldn't understand it :( <br /><br /><br />In case, if String is not immutable, this would lead serious security threat , I mean some one can access to any file for which he has authorization, and then can change the file name either deliberately or accidentally and gain access of those file. Because of immutability, you don't need to worry tituhttps://www.blogger.com/profile/07511429348433654007noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11926648945060888992015-02-10T19:12:26.609-08:002015-02-10T19:12:26.609-08:00how immutability of string helps in class loading?...how immutability of string helps in class loading??Anonymoushttps://www.blogger.com/profile/15099419859811500726noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24435674949604093852014-12-02T23:37:45.644-08:002014-12-02T23:37:45.644-08:00Hi Javin, one more reason for String being immutab...Hi Javin, one more reason for String being immutable could be that String is more like the primitive type but represented as object. Since java uses pass by value it makes sense for passing an immutable object to the methods as parameter so that pass by value behavior is maintained.<br /><br />AkshatAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25927074961283066952014-10-06T05:38:56.758-07:002014-10-06T05:38:56.758-07:00@vaibhav, Yes indeed, that's a side effect of ...@vaibhav, Yes indeed, that's a <a href="http://javarevisited.blogspot.com/2013/03/how-to-create-immutable-class-object-java-example-tutorial.html" rel="nofollow">side effect of making a class Immutable in Java</a>. You need to be careful to store the modified reference otherwise it can really create hard to find bugs in your code.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.com