I know that String is a final class and so we cannot extend it.But,i do not know the reason behind making String as a final class.Even if String would not have been a final class,then also i think there would not have been any problem. So, can anyone tell me if there is some special reason behind making String as a final class.
As Kaydell stated, the reason why the String class is final is to guarantee immutability. And the reason why this guarantee is needed is for security.
If String objects were not immutable, it will be technically possible to create objects that make a request for items (such as public files), pass the require challenges, and then change those requests for other items (such as password files).
Of course, this is somewhat paranoid. But imagine if this guarantee could not be enforced. Applets, URLs, JDBC, JMS, and anything that needs to be secured, will need some way to lock down what was requested.
Immutability has a number of advantages. I think the security issue Henry mentioned may be the most important, but there are others. Immutable objects are much easier, safer and faster to use with multiple threads. They also protect you from many accidental bugs when other programmers change something that you weren't expecting them to. And using immutable strings allows Java to save memory by reusing many string in the string pool.
"I'm not back." - Bill Harding, Twister
Raj Kumar Bindal
Ranch Hand
Joined: Apr 15, 2006
Posts: 409
posted
0
Thanks to all of you.So finally the answer is :
1.URLs,Applets,JMS,JDBC etc. needs to keep track of what all has been done using them,so they just keep address of Strings which are executed and if we want to see what has happened to them last n days,JMS etc. will simply show those strings to us by taking them from their addresses which is present with JMS.
2.Immutable objects are thread safe and so become safer and easier and faster in case of multithreading.
3.Immutable objects allow java to save memory--This i am not clear as this can be possible with stringbuffer also..can you explain with an example.
Since immutable objects cannot be changed, you can have a single object with many, many references to it and only have one copy of the object using RAM.
For example, if you have the following code:
You could have an unlimited number of references to the String "XYZ" and only have one copy taking up space in RAM.
Kaydell
Raj Kumar Bindal
Ranch Hand
Joined: Apr 15, 2006
Posts: 409
posted
0
Thanks a lot ,i understood
Raj Kumar Bindal
Ranch Hand
Joined: Apr 15, 2006
Posts: 409
posted
0
I have just a small clarification.As told by kaydell that immutable objects allow java to save memory.
So,suppose
on one day in some program,i am doing
String raj="xyz"
and after one week in some other program,i am doing
String hari="xyz"
Does it mean that whole memory will be searched for string xyz,if it exists hari will also start pointing to this or if garbage collector has removed it from memory,then in a new memory location xyz will be put and hari will start pointing to that.