Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in String

 
jai gurudev
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Anybody clear my doubt:

Question 1:

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..

any body tell.

regards

jaigurudev
 
Kalai Selvan
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Pollitt
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

for example:

 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are still differences between ArrayList and Vector. If your purpose is only to synchronize, then both are okay.

If the vector exceeds its capacity, then it increases its capacity by two-fold. But the ArrayList increases its capacity by 50%.

In Vector, you have the capacityIncrement factor, which is not there in ArrayList.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

- Peter
 
Atul Prabhu
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Synchronized code will always have an influence on the performance of your code. Even in non-multithreaded apps, the overhead can be significant.

Regarding ur question , i think there would be no difference as both internally use array to maintain the elements.

----
Atul
[ July 29, 2004: Message edited by: Atul Prabhu ]
 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic