File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why strings are immutable?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why strings are immutable??" Watch "Why strings are immutable??" New topic
Author

Why strings are immutable??

Hrishikesh Ghatnekar
Greenhorn

Joined: Oct 25, 2004
Posts: 28
Second question ,

Is there any way to synchronise hashmap w/o any code change?


Thanks
Hrishikesh
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8908
    
    8

In the future, try to post a single thread per question. If one question should spark some debate, the other question could get lost in the shuffle.
As for String, it is immutable for performance. If you watch a Java program with a profiler, the class with the most instances by far will be String. Since it is immutable, the compiler can use a number of tricks to optomize its use. For example, every time an identical literal is used:

the compiler will replace the duplicates with a single reference. At runtime, both Strings x and y will point to the same instance of "hey". There's no danger of x changing it's String and invalidating y.
Another optimization is that substrings of a String will point to the same data as the original String:

In this example, the instance z points to the same data (though less of it) as instance x. Saves lots of memory over time and we can do it because neither instance can change the underlying data.
As for HashMap, the java documentation for java.util.HashMap states the following:

If multiple threads access this map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

Map m = Collections.synchronizedMap(new HashMap(...));


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Hrishikesh Ghatnekar
Greenhorn

Joined: Oct 25, 2004
Posts: 28
Hi ,

Thanks for reply but ,

Somehow I am not convinced that string is immutable because of performance, because
majority of the time we can see string addition happing inside the code , which finally creates 5/6 objects

i.e String + String =

String->StringBuffer + String->StringBuffer = StringBuffer ->String


Any Ideas ??

Thanks
Hrishikesh
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
Another good reason for String to be immutable is that most of the time you're using Strings to key Hashing collections.

Imagine the cahaos that could arise if your keys weren't guarranteed to remain what they were when you set them. Hashing functions would become ridiculously innefficient.

McF


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8908
    
    8

Originally posted by Hrishikesh Ghatnekar:

Somehow I am not convinced that string is immutable because of performance, because majority of the time we can see string addition happing inside the code , which finally creates 5/6 objects


Engineering decisions are always tradeoffs. In the case of String, Java's designers decided that having the optimizations that Jeremy and I cited above were more important than the performance impact of creating a StringBuffer every time someone wanted to concatenate Strings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why strings are immutable??