peekjava的简单介绍

博主:adminadmin 2023-01-20 12:54:06 343

今天给各位分享peekjava的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java术语peek什么意思

【peek】找到但不移除此列表的头(第一个元素)。

【pop】移除顶对象并作为此函数的值返回该对象。

java反射机制详解

反射就是把Java的各种成分映射成相应的Java类。

Class类的构造方法是private,由JVM创建。

反射是java语言的一个特性,它允程序在运行时(注意不是编译的时候)来进行自我检查并且对内部的成员进行操作。例如它允许一个java的类获取他所有的成员变量和方法并且显示出来。Java 的这一能力在实际应用中也许用得不是很多,但是在其它的程序设计语言中根本就不存在这一特性。例如,Pascal、C 或者 C++ 中就没有办法在程序中获得函数定义相关的信息。(来自Sun)

JavaBean 是 reflection 的实际应用之一,它能让一些工具可视化的操作软件组件。这些工具通过 reflection 动态的载入并取得 Java 组件(类) 的属性。

反射是从1.2就有的,后面的三大框架都会用到反射机制,涉及到类"Class",无法直接new CLass(),其对象是内存里的一份字节码.

Class 类的实例表示正在运行的 Java 应用程序中的类和接口。枚举是一种类,注释是一种接口。每个数组属于被映射为 Class 对象的一个类,所有具有相同元素类型和维数的数组都共享该 Class 对象。

基本的 Java类型(boolean、byte、char、short、int、long、float 和 double)和关键字 void 也表示为 Class 对象。Class 没有公共构造方法。

Class 对象是在加载类时由 Java 虚拟机以及通过调用类加载器中的 defineClass 方法自动构造的。

Person p1 = new Person();

//下面的这三种方式都可以得到字节码

CLass c1 = Date.class();

p1.getClass();

//若存在则加载,否则新建,往往使用第三种,类的名字在写源程序时不需要知道,到运行时再传递过来

Class.forName("java.lang.String");

Class.forName()字节码已经加载到java虚拟机中,去得到字节码;java虚拟机中还没有生成字节码 用类加载器进行加载,加载的字节码缓冲到虚拟机中。 

另外,大家可以关注微信公众号Java技术栈回复:JVM,获取我整理的系列JVM教程,都是干货。

考虑下面这个简单的例子,让我们看看 reflection 是如何工作的。

import java.lang.reflect.*;

public class DumpMethods {

public static void main(String args[]) {

try {

Class c = Class.forName("java.util.Stack");

Method m[] = c.getDeclaredMethods();

for (int i = 0; i m.length; i++)

System.out.println(m[i].toString());

}

catch (Throwable e){

System.err.println(e);

}

}

}

public synchronized java.lang.Object java.util.Stack.pop()

public java.lang.Object java.util.Stack.push(java.lang.Object)

public boolean java.util.Stack.empty()

public synchronized java.lang.Object java.util.Stack.peek()

public synchronized int java.util.Stack.search(java.lang.Object)

这样就列出了java.util.Stack 类的各方法名以及它们的限制符和返回类型。这个程序使用 Class.forName 载入指定的类,然后调用 getDeclaredMethods 来获取这个类中定义了的方法列表。java.lang.reflect.Methods 是用来描述某个类中单个方法的一个类。

以下示例使用 Class 对象来显示对象的类名:

void printClassName(Object obj) {

System.out.println("The class of " + obj +

" is " + obj.getClass().getName());

}

还可以使用一个类字面值(JLS Section 15.8.2)来获取指定类型(或 void)的 Class 对象。例如:

System.out.println("The name of class Foo is: "+Foo.class.getName());

在没有对象实例的时候,主要有两种办法。

//获得类类型的两种方式

Class cls1 = Role.class;

Class cls2 = Class.forName("yui.Role");

注意第二种方式中,forName中的参数一定是完整的类名(包名+类名),并且这个方法需要捕获异常。现在得到cls1就可以创建一个Role类的实例了,利用Class的newInstance方法相当于调用类的默认的构造器。

Object o = cls1.newInstance();

//创建一个实例

//Object o1 = new Role(); //与上面的方法等价

java中queue中什么可以移除

java中的queue类是队列数据结构管理类。在它里边的元素可以按照添加它们的相同顺序被移除。

队列通常(但并非一定)以 FIFO(先进先出)的方式排序各个元素。不过优先级队列和 LIFO 队列(或堆栈)例外,前者根据提供的比较器或元素的自然顺序对元素进行排序,后者按 LIFO(后进先出)的方式对元素进行排序。无论使用哪种排序方式,队列的头都是调用remove()或poll()所移除的元素。在 FIFO 队列中,所有的新元素都插入队列的末尾。其他种类的队列可能使用不同的元素放置规则。每个Queue实现必须指定其顺序属性。

offer 添加一个元素并返回true 如果队列已满,则返回false

poll 移除并返问队列头部的元素 如果队列为空,则返回null

peek 返回队列头部的元素 如果队列为空,则返回null

put 添加一个元素 如果队列满,则阻塞

take 移除并返回队列头部的元素 如果队列为空,则阻塞

element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常

add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常

remove 移除并返回队列头部的元素 如果队列为空,则抛出一个

NoSuchElementException异常

注意:poll和peek方法出错进返回null。因此,向队列中插入null值是不合法的。

java (String) s.peek()是什么意思?

s.peek() 表示的是查看堆栈顶部的对象,但不从堆栈中移除它。

除此之外:

push(E item) 表示的是把项压入堆栈顶部。

pop() 表示的是移除堆栈顶部的对象,并作为此函数的值返回该对象。

empty() 表示的是测试堆栈是否为空。

search(Object o) 表示的是返回对象在堆栈中的位置,以 1 为基数。

以下是从jdk中拿下来的相关方法的源码,可以参看下:

public class StackE extends VectorE {

    /**

     * Creates an empty Stack.

     */

    public Stack() {

    }

    /**

     * Pushes an item onto the top of this stack. This has exactly

     * the same effect as:

     * blockquotepre

     * addElement(item)/pre/blockquote

     *

     * @param   item   the item to be pushed onto this stack.

     * @return  the codeitem/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 ttVector/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 ttVector/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  codetrue/code if and only if this stack contains

     *          no items; codefalse/code otherwise.

     */

    public boolean empty() {

return size() == 0;

    }

    /**

     * Returns the 1-based position where an object is on this stack.

     * If the object tto/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 tt1/tt. The ttequals/tt

     * method is used to compare tto/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;

}

关于peekjava和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。