Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extending String class

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the String class is final so that it is guaranteed to be immutable and have the advantages of an immutable class:

Wikipedia: Immutable Object
[ February 04, 2008: Message edited by: Kaydell Leavitt ]
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Please add if i have missed out something.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3.Immutable objects allow java to save memory...


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
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot ,i understood
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you have two different programs, they do not share the same memory space, so you will not save memory that way.

however, if in one program you write

String myString = "Fred"

and then 100 lines later, write

String myString2 = "Fred"

both of those will point to the same space in memory.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic