aspose file tools*
The moose likes Beginning Java and the fly likes extending String class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extending String class" Watch "extending String class" New topic
Author

extending String class

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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

Joined: Nov 18, 2006
Posts: 689

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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: 418
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

Joined: Nov 18, 2006
Posts: 689

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

Joined: Apr 15, 2006
Posts: 418
Thanks a lot ,i understood
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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

Joined: Oct 02, 2003
Posts: 11422
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: extending String class