somehume
somehume

Reputation: 154

How many elements can a Stack store in Java?

Is there a maximum number of elements that can be stored in a stack? Is the only limitation the amount of storage available to the system?

For clarity, I'm referring to java.util.Stack.

Upvotes: 1

Views: 1246

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533492

If you are taling about java.util.Stack, then the limit is Integer.MAX_VALUE which is about 2 billion. However if you let it grow naturally, you get an exception if you add more than about 1.3 billion (10 * 2^28) as it will try to grow the underlying array to a size larger than is allowed.

IMHO Stack is a legacy class replaced in Java 1.2 (1998) I don't suggest you use it.

Upvotes: 5

Thomas
Thomas

Reputation: 88707

The storage capability is normally limited by memory available, either heap memory for stack data structures or stack memory for the call stack.

Upvotes: 2

Related Questions