⑴ java中什麼是棧啊
存放基本類型的變數數據和對象的引用,但對象本身不存放在棧中,而是存放在堆(new 出來的對象)或者常量池中(字元串常量對象存放在常量池中。)。
棧和常量池中的對象可以共享,對於堆中的對象不可以共享。棧中的數據大小和生命周期是可以確定的,當沒有引用指向數據時,這個數據就會消失。堆中的對象的由垃圾回收器負責回收,因此大小和生命周期不需要確定。
局部變數的數據存在於棧內存中。
棧的優勢是,存取速度比堆要快,僅次於寄存器,棧數據可以共享。但缺點是,存在棧中的數據大小與生存期必須是確定的,缺乏靈活性。棧中主要存放一些基本類型的變數數據(int, short, long, byte, float, double, boolean, char)和對象句柄(引用)。
⑵ java中棧的應用
棧中存儲的是方法參數變數和方法體中的局部變數還有整形變數。。具體的應用你可以去看thinking in java。。。
⑶ java中棧是如何實現的
這是java.util包下的Stack類,你可以看一下它是如何實現的,至於用法,無非就是push,pop,peek等操作等
/*
* @(#)Stack.java 1.30 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.util;
/**
* The <code>Stack</code> class represents a last-in-first-out
* (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
* operations that allow a vector to be treated as a stack. The usual
* <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
* method to <tt>peek</tt> at the top item on the stack, a method to test
* for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
* the stack for an item and discover how far it is from the top.
* <p>
* When a stack is first created, it contains no items.
*
* <p>A more complete and consistent set of LIFO stack operations is
* provided by the {@link Deque} interface and its implementations, which
* should be used in preference to this class. For example:
* <pre> {@code
* Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
*
* @author Jonathan Payne
* @version 1.30, 11/17/05
* @since JDK1.0
*/
public
class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
}
/**
* Pushes an item onto the top of this stack. This has exactly
* the same effect as:
* <blockquote><pre>
* addElement(item)</pre></blockquote>
*
* @param item the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
public E push(E item) {
addElement(item);
return item;
}
/**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
*
* @return The object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
/**
* Looks at the object at the top of this stack without removing it
* from the stack.
*
* @return the object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
/**
* Tests if this stack is empty.
*
* @return <code>true</code> if and only if this stack contains
* no items; <code>false</code> otherwise.
*/
public boolean empty() {
return size() == 0;
}
/**
* Returns the 1-based position where an object is on this stack.
* If the object <tt>o</tt> occurs as an item in this stack, this
* method returns the distance from the top of the stack of the
* occurrence nearest the top of the stack; the topmost item on the
* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
* method is used to compare <tt>o</tt> to the
* items in this stack.
*
* @param o the desired object.
* @return the 1-based position from the top of the stack where
* the object is located; the return value <code>-1</code>
* indicates that the object is not on the stack.
*/
public synchronized int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1224463164541339165L;
}
⑷ java 創建棧問題
import java.util.Stack;
public class Test1 {
public static void main(String[] args){
Stack kk = new Stack();
kk.push(new Integer(11));//只能放入對象,int,double是不行的,只有先作成對象
kk.push(new Integer(12));
kk.push(new Integer(13));
kk.push(new Integer(14));
kk.push(new Integer(15));
System.out.println(kk);
kk.pop();
kk.pop(); //後進先出,不存在優先順序的概念
System.out.println(kk);
kk.push("132343");
System.out.println(kk);
}
}
⑸ JAVA中隊列和棧的區別
隊列(Queue):是限定只能在表的一端進行插入和在另一端進行刪除操作的線性表;
棧(Stack):是限定只能在表的一端進行插入和刪除操作的線性表。
區別如下:
一、規則不同
1. 隊列:先進先出(First In First Out)FIFO
2. 棧:先進後出(First In Last Out )FILO
二、對插入和刪除操作的限定不同
1. 隊列:只能在表的一端進行插入,並在表的另一端進行刪除;
2. 棧:只能在表的一端插入和刪除。
三、遍歷數據速度不同
1.
隊列:基於地址指針進行遍歷,而且可以從頭部或者尾部進行遍歷,但不能同時遍歷,無需開辟空間,因為在遍歷的過程中不影響數據結構,所以遍歷速度要快;
2.
棧:只能從頂部取數據,也就是說最先進入棧底的,需要遍歷整個棧才能取出來,而且在遍歷數據的同時需要為數據開辟臨時空間,保持數據在遍歷前的一致性。
⑹ java 中的堆棧是什麼
首先堆棧是計算機為程序分配的內存空間,用來存儲數據的。
在java中因為我們不直接操作內存,所以並不需要考慮指針的問題
在java中堆和棧也是用來存儲數據,其中棧存儲的引用,堆存儲的對象
如:Student s = new Student("張三");
s在棧中 張三在堆
⑺ 請簡單通俗易懂的解釋一下在Java中什麼叫堆 什麼叫棧 謝謝
堆:也叫動態內存,相當於一個內存池子,在java中創建對象的時候,就從堆裡面拿出一塊來存放對象;當GC(垃圾回收)回收對象的時候,又把對象佔用的內容還給堆。
舉個例子:堆就好比一個面團,類就好比一個饅頭印子,用印子從面團中取出一小塊面團,印成饅頭,這個饅頭就是這個饅頭印子類產生的對象了。當無限的創建饅頭的時候,這個面團總會被用光的,這個時候就不能在創建新的饅頭了。
所有GC就有存在的必要了,當對象不被持有的時候,GC就會把對象還給堆。也就是,當這個饅頭沒有被使用,這個饅頭就變成小面團,還給面團。
棧:也是一段內存,但是這段內存比較有特點,遵循一個先進後出的規則。
舉個例子:吃過罐裝的薯片吧,薯片一片一片的放到罐裡面去,想想,最先放進去的是不是放在罐的最底部。現在一片片把薯片取出來,是不是要從最頂部開始去,也就是最後放進去的,最先取出來。這個薯片放進去和取出來的這個過程,就是棧的工作原理啦(先進入的後出來,後進入的先出來)
在java中的棧:棧的原理明白了,其實只要是計算機只要是編程語言,什麼堆什麼棧都是一樣的,基本作用也一樣。java中可以認為,棧用來存放局部變數的。
public void fun(){
int i=0; //i 是一個局部變數,存放在棧裡面的
Object obj = new Objec(); //obj 是一個對象應用,同樣也是一個局部變數,存放在站裡面的,但是obj指向的對象,在存在堆中
}
⑻ java 棧 什麼意思
棧是一種常用的數據結構,棧只允許訪問棧頂的元素,棧就像一個杯子,每次都只能取杯子頂上的東西,而對於棧就只能每次訪問它的棧頂元素,從而可以達到保護棧頂元素以下的其他元素.」先進後出」或」後進先出」就是棧的一大特點,先進棧的元素總是要等到後進棧的元素出棧以後才能出棧.遞歸就是利用到了系統棧,暫時保存臨時結果,對臨時結果進行保護.
對於棧的學習,建議你看一看<數據結構與演算法>這本書.
⑼ Java中什麼叫堆棧
和別的一樣啊,就是後進去的先出來!