Java Stack class implementation list interface is it really a joke?

Source: Internet
Author: User

Today, I saw this remark on the internet, saying that "the Java stack class implements the design of the list interface is a joke." Of course, the author of this article is not the focus of this, originally I was just laugh, but there are still people to see the comments echoed, said "Java The design of the stack as a joke, almost can be counted", I am a bit not calm, why, when "as a joke" and "almost can be counted as a" , huh? So I decided to write an article to talk about the problem. What is an interface?In a narrow sense, an interface is a method defined by a class (method name, parameter, return value). A class provides the Foo method, which can be called by other classes. Broadly speaking, an interface can be understood as a contract between a subsystem and other subsystems for interaction. The subsystem here can be a class, or a module, or any other entity that can interact with the outside world.Note: In Java, we can use interface to define an interface, interface do nothing, just to provide "specification" for the implementation, so interface is more pure than the abstract class, the level of abstraction tends to be higher. Note, however, that the interface here is different from the generic "interface", interface is a language mechanism that Java provides to better support "programming for interfaces", even without interface we can do "programming for Interfaces" well. If a class fully adheres to an interface, it can be said that this class "implements" this interface. Implementing an interface is arbitrary at the language level, because the language can only check the syntax and not the semantics. For example, you can have the people class implement the plant interface, so this person can be used as a plant at any time (vegetable?). )。 For dynamic languages, this is even more obvious: as long as an object shows some behavioral characteristics, the object can be used as another object. People give a vivid name to this characteristic of dynamic language: Duck type (Duck Typing). In other words, as long as an object can "Croak", it can be regarded as a duck. But from the designer's point of view, what interfaces a class implements, in addition to conforming to the syntax, should be more semantic, so that people who use this class don't get confused. Therefore, the "stack class should not implement a list interface" is a Semantic IssuesInstead of Grammatical Problems。 To answer this question, first understand what the stack is and in what scenarios it needs to be used. What is stack?Stack, a stack, is a data organization that all programmers are familiar with. Stacks are usually "last in, first Out" (LIFO). As a convention, stacks typically have 2 operations called push and pop, which pushes an element into the stack (push-in), while the pop operation, in contrast, takes an element out of the top of the stack (eject). Application of StacksIn most cases, the stack is used only as a "stack", that is, you simply use the Push/pop operation, for example, you can use the stack to check for parentheses matching problems, or to calculate the value of a suffix expression. Another example is procedure calls (procedure Call) in the computer. Before invoking a procedure (also known as a child procedure, a function), you need to press the current frame pointer, return address into the stack, then press into each parameter sequentially, and finally execute the call instruction to jump to the starting address of the target procedure.  Its stack structure is shown. It is the opposite of returning and calling from the process: the parameters and temporary variables are ejected from the stack (typically dropped directly), and the RET instruction is executed to jump to the location where the return address is located. Random Access StacksSo far, the operation of the stack is only a standard push/pop operation. But inside a process, you might need random access to the stack (similar to how arrays are accessed), in addition to the push/pop operation. For example, the code in procedure Q may have to access the parameter Y1, but cannot discard the y2, so the pop operation cannot be used. In fact, in all the stacks-based machines that implement procedure calls, the compiler uses random access to manipulate parameters and temporary variables on the stack, and the stack is only required for PUSH/POP operations on call and Ret. At this point the stack is more like an array for the process. For example, process Q may access Y1 in the following way:
1)

Here the SP is the stack pointer, which points to the top of the stack, and assumes that Y2 occupies 1 machine word lengths (for example, 4 bytes in a 32-bit system is a machine word length). The other reason is to add 1 instead of minus 1 because the stack is growing from a high address to a low address, so the Y2 address is smaller than the Y1. As can be seen from this example, depending on the scenario, the stack sometimes requires random access like an array, in which case the stack is actually treated as an array. Java's Stack class implementation list is not a jokeLet's look at the declaration of the Stack class in Java: Public classStack<e> extendsVector<e> Vector: Public classVector<e> extendsAbstractlist<e> ImplementsList<e&gt, Randomaccess, cloneable, Serializable in other words, stack defines the standard push/pop operations, and also implements the list and randomaccess interfaces. The list interface represents a sequential list in Java, and Randomaccess adds a "Random access" convention based on sequential access. In general, the time complexity of random access is O (1). For example, an array is a list of "sequential and random Access", and a list of "sequential non-random access". As can be seen from the above example, it is perfectly reasonable for the stack to implement the list and Randomaccess interfaces. If you need to use it as a pure stack, you just have to think of it as an ordinary "stack" instance. But if you need random access to it, you can use it as a "randomaccess list" at any time. Both of these situations are likely to be encountered. So I read the above articles and comments on the "Stack implementation list is a joke" is very confused, but also do not understand when this has become the so-called "cases". In my opinion, this is very reasonable and will not have any adverse effect on my correct use of stack.

Java Stack class implementation list interface is it really a joke?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.