we declare String s="abc" instead of String s=new String("abc"); what is the difference in String s="abc" could any body tell Question 2: i have Vector and Arraylist we know Vector is Synchronized in default.but my doubt is when i declare Synchronized ArrayList method()..and Vector is default Synchronized if i use Arraylist method is there any difference between these two..
Hi jai, Jvm internaly maintains a string pool, so when you assign a value to a string variable without the new operator jvm first searches for the string in the pool if it finds one then it will return the reference of that string else it will create a new string with the specified value and returns the reference. By using the new operator you instruct the jvm to create a new memory space for the string. e.g.
And i don't have proper answer for the second question.
You can push the new String into the pool of strings by calling the myString.intern() method, either that or if a String already exists in the pool that is equal (using equals(..)) to the one you invoked intern() on then that String is returned.
The synchronization in Vector is usually pretty useless, even in multi-threaded applications - it's rarely the little Vector methods that need to be synchronized (i.e. atomic with respect to other threads). That is why Sun made the Collections framework unsynchronized. On the rare occasion that you do need a synchronized collection, you can use Collections.synchronizedList(), and so on.
In general, Vector, Hashtable and Enumeration are legacy classes and should be avoided for any new software. They aren't part of the Collections framework, and just represent inferior duplications of the functionality in ArrayList, HashMap and Iterator. The retrofit with Collections interfaces has made their API has bloated and ambiguous. The fact that they are synchronized is a liability, not an asset.
I really don't understand the difference between collection implementation and collection framework
Collection implementations in earlier versions of Java included Vector , Hashtable , and array. While earlier versions of Java contained collection implementations, they did not contain a collections framework.
This is from sun. Pls. clarify why Vector, Hashtable and Array not part of collection frmwork. What is the difference between these two?
Thanks a lot, Guru
Peter den Haan
Joined: Apr 20, 2000
Originally posted by Gurumurthy Ramamurthy: Pls. clarify why Vector, Hashtable and Array not part of collection frmwork.
Well, I have my opinions, but they are not important. Bottom line is, they are not part of the Collections framework because Sun says they aren't. Read the documentation - Vector, Hashtable and Enumeration are legacy classes.
If they had been part of the framework, there would not have been any need to introduce ArrayList or HashMap, don't you think?
- Peter [ July 29, 2004: Message edited by: Peter den Haan ]