导航:首页 > 编程语言 > java栈

java栈

发布时间:2022-02-12 11:44:12

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中什么叫堆栈

和别的一样啊,就是后进去的先出来!

阅读全文

与java栈相关的资料

热点内容
看帧率app如何使用 浏览:523
从DHC服务器租用IP地址 浏览:473
编译怎么学 浏览:329
数码管显示0到9plc编程 浏览:665
服务器是为什么服务的 浏览:765
java定义数据类型 浏览:874
安卓pdf手写 浏览:427
什么是app开发者 浏览:284
android闹钟重启 浏览:101
程序员失职 浏览:518
在云服务器怎么改密码 浏览:586
服务器pb什么意思 浏览:940
51驾驶员的是什么app 浏览:670
php静态变量销毁 浏览:886
编程买苹果电脑 浏览:762
flac算法 浏览:499
reactnative与android 浏览:665
程序员是干什么的工作好吗 浏览:258
kbuild编译ko 浏览:471
条件编译的宏 浏览:566