0

I have a question which is related to the Stack implementation in the Collections framework in Java.

  1. I can see from the implementation that the size of the Stack can grow. Does this mean that a StackOverflowError can never occur and eventually the Stack reaches a size and an OutOfMemoryError occurs?
  2. From googling I found that the Vector class is deprecated since it synchronizes each and every operation as Jon Skeet pointed out here: Is Java Vector deprecated?

So, after this is there any real life scenario where I would use this Java class? I don't want to synchronize on each and every operation and want to synchronize on a bunch of operations. Can somebody give a real life situation/example.

Community
  • 1
  • 1
Madusudanan
  • 1,017
  • 1
  • 15
  • 36
  • 2
    `StackOverflowError` is thrown when the call stack grows too large, and for no other reason. The data structure known as `Stack` is different. – Patashu Apr 25 '13 at 05:26
  • A stackoverflow can definitely occur. As for a use case for vector, I'm hard pressed to think of one – kolossus Apr 25 '13 at 05:27
  • 1
    Note the the API doc of `java.util.Stack` says: "A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class." – Jesper Apr 25 '13 at 05:30
  • @Jesper: your comment is THE answer. – Aubin Apr 25 '13 at 05:33

3 Answers3

3

First of all, you shouldn't use java.util.Stack, just like you shouldn't use Vector - they are both legacy collection classes which were replaced by Deque and ArrayList since Java version 1.2. Note that Stack's API documentation says:

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.

Note that StackOverflowError extends Error, not Exception or RuntimeException. Errors are only thrown if there's some internal error in the JVM - they are not normally thrown by Java classes.

StackOverflowError is thrown only when the method call stack overflows; it does not have anything to do with java.util.Stack.

As you can see in the API documentation of StackOverflowError:

Thrown when a stack overflow occurs because an application recurses too deeply.

Jesper
  • 202,709
  • 46
  • 318
  • 350
2

A stack, the data structure, which goes by java.util.Stack, can grow. The method call stack in Java, however, cannot. It thus can overflow, invoking a StackOverflowError. This occurs primarily when dealing with recursion, but can occur elsewhere if you're not too careful.

There is an important distinction here between java.util.Stack and the java method call stack. One holds an array of generics, and the other is inherent to the JVM. The latter is used when a method exits to return to the previous method.

I've never heard of anyone unintentionally overflowing the JVM's stack, unless they were working on recursion.

Don't use Vector. There are much better classes out there. You can also just use an ArrayList and keep its access synchronized.

Community
  • 1
  • 1
  • 1
    Maybe it's a good idea to explain that the call stack doesn't have anything to do with `java.util.Stack`, and that `StackOverflowError` doesn't have anything to do with `java.util.Stack`. – Jesper Apr 25 '13 at 05:32
  • Got confused between call stack and the Stack data structure. – Madusudanan Apr 25 '13 at 05:51
1

StackOverflowError has nothing to do with java.util.Stack. The java.util.Stack will grow as long as there is free memory, or OutOfMemoryError is thrown.

In new applications, there is no need to use either Vector or Stack. Use ArrayList or ArrayDeque. You rarely need thread safety provided by Vector, and if you do, use Collections.synchronizedList or LinkedBlockingDeque. However, since HotSpot JVM 1.6, this is not a problem any more, see for example here: http://www.ibm.com/developerworks/java/library/j-jtp10185/

Oliv
  • 10,221
  • 3
  • 55
  • 76