大家都知道,java.lang包是在JVM运行时自动导入的,所以我们在写代码时不需要写上:
import java.lang.*;但是:
System.out对象是OutputStream类的,而这个类却是在java.io包中定义的,但java.io包不是被自动导入的。这究竟是个怎么回事呢?
import java.lang.*;但是:
System.out对象是OutputStream类的,而这个类却是在java.io包中定义的,但java.io包不是被自动导入的。这究竟是个怎么回事呢?
你说的没错,out是System的一个静态成员,但是,这个成员的类型是OutputStream啊!或者,换种说法,如果我写代码时不写import java.io.*; 直接用OutputStream ops; 编译都不会通过的,这是为什么呢?
我想你的意思也许是在java.lang包中导入了java.io包,但为什么我们在写代码时不能直接用io包中的类呢?
package java.lang;import java.io.*;
import java.util.Properties;
import java.util.PropertyPermission;
import java.util.StringTokenizer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.AllPermission;
import java.nio.channels.Channel;
import java.nio.channels.spi.SelectorProvider;
import sun.nio.ch.Interruptible;
import sun.reflect.Reflection;
import sun.security.util.SecurityConstants;
import sun.reflect.annotation.AnnotationType;/**
* The <code>System</code> class contains several useful class fields
* and methods. It cannot be instantiated.
*
* <p>Among the facilities provided by the <code>System</code> class
* are standard input, standard output, and error output streams;
* access to externally defined properties and environment
* variables; a means of loading files and libraries; and a utility
* method for quickly copying a portion of an array.
*
* @author unascribed
* @version 1.162, 04/01/09
* @since JDK1.0
*/
public final class System {
private static native void registerNatives();
static {
registerNatives();
} ......
public final static PrintStream out = nullPrintStream();
System是lang包下的,System.out是PrintStream打印流类型顶部不是有导入import java.io.* 吗?