Why are these programs the same? Why is it possible to say InputStream in = System.in. Is System.in an object which can be referenced?
P.S: I found a good posting on read() called "Abstract method read()" but I don't understand it. So i post this question. Thanks. Thomas [ Edited to preserve formatting using the [code] and [/code] UBB tags. It'd be nice if Thomas would start doing this all by himself -ds ] [ July 30, 2002: Message edited by: Dirk Schreckmann ]
posted 13 years ago
The class java.lang.System class contains the following 3 public constants: public static final java.io.PrintStream err; public static final java.io.InputStream in; public static final java.io.PrintStream out; What this means is that System.in is a type of InputStream. In fact, it will be a concrete subclass of this abstract class. However, you can just use the methods of InputStream, and the underlying methods will be called due to polymorphism. Similarly, you can create an alias to the System.in InputStream handle with the code: InputStream in = System.in; What this means is that in and System.in are both handles to the same underlying object. Hope this hasn't confused you more...