wood burning stoves*
The moose likes Beginning Java and the fly likes doubt  in String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt  in String" Watch "doubt  in String" New topic
Author

doubt in String

jai gurudev
Greenhorn

Joined: May 28, 2004
Posts: 20
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

Joined: Jul 07, 2004
Posts: 79
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

Joined: Jun 30, 2004
Posts: 9
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

Joined: Feb 13, 2003
Posts: 272
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Dec 17, 2002
Posts: 60
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

Joined: Feb 13, 2003
Posts: 272
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

Joined: Apr 20, 2000
Posts: 3252
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 ]
 
 
subject: doubt in String