你可以去看看类的生命周期,这有我转载的一篇博客(http://blog.csdn.net/wangwuyilove/article/details/21177219)希望对你有所帮助。 最好多看看源码,类中加载到连接到初始化到使用,都是在内存中,JVM中有存放方法的方法区,当创建一个类,new出来的时候就初始化了这个类,调用非静态方法的时候就会加载这个方法。有些我也解释不来,自己本身理解的不透,有什么不对的可以多讨论一下。 下面的是scanner类中关于next方法的源码 /** * Finds and returns the next complete token from this scanner. * A complete token is preceded and followed by input that matches * the delimiter pattern. This method may block while waiting for input * to scan, even if a previous invocation of {@link #hasNext} returned * <code>true</code>. * * @return the next token * @throws NoSuchElementException if no more tokens are available * @throws IllegalStateException if this scanner is closed * @see java.util.Iterator */ public String next() { ensureOpen(); clearCaches();
while (true) { String token = getCompleteTokenInBuffer(null); if (token != null) { matchValid = true; skipped = false; return token; } if (needInput) readInput(); else throwFor(); } }
你没新建一个对象,这个对象就被创建在内存里,他所有的方法,属性都在内存里,这个对象有个名字,也就是引用。
cs就是他的引用,sc里有这个对象的地址信息,你通过sc就能找到对象地址,然后找到对象,找到对象的那个方法的代码,执行之。
里先这么理解。具体的你以后就明白了,太细节的东西需要更多的专业知识才能理解,如果你想知道更多就去看更细的知识。估计你想学得事JAVA而不是C不是汇编语言什么的吧。别太抠这些。
因为Scanner 中有next 这个方法啊
楼主可以去找java的API文档。
最好多看看源码,类中加载到连接到初始化到使用,都是在内存中,JVM中有存放方法的方法区,当创建一个类,new出来的时候就初始化了这个类,调用非静态方法的时候就会加载这个方法。有些我也解释不来,自己本身理解的不透,有什么不对的可以多讨论一下。
下面的是scanner类中关于next方法的源码
/**
* Finds and returns the next complete token from this scanner.
* A complete token is preceded and followed by input that matches
* the delimiter pattern. This method may block while waiting for input
* to scan, even if a previous invocation of {@link #hasNext} returned
* <code>true</code>.
*
* @return the next token
* @throws NoSuchElementException if no more tokens are available
* @throws IllegalStateException if this scanner is closed
* @see java.util.Iterator
*/
public String next() {
ensureOpen();
clearCaches();
while (true) {
String token = getCompleteTokenInBuffer(null);
if (token != null) {
matchValid = true;
skipped = false;
return token;
}
if (needInput)
readInput();
else
throwFor();
}
}