(c) Copyright 2000, 2001, 2002, 2004 by Daniel Berleant
Review: a First Java
Program
import java.lang.*;
class HelloWorld
{
public static void main (String args[])
{
System.out.println ("Hello World!");
}
}(Note the I/O statement in it)
New Topic: I/O
I/O can involve console, file, or URL
("console" - screen and keyboard)
I/O with screen and keyboard
Similar to I/O from a file, but simpler
Data "comes from" a keyboard or file
Data "goes to" a screen or file
Keyboard and screen are automatically opened
for I/O
...so, no need for open statement
(Other files must be explicitly opened)
Keyboard, screen, files (and internet http
connections for I/O with URLs) are all streams
Stream: an ordered sequence of bytes, characters, or other data
(Analogy: a stream of data flowing like the water in a stream of water)
The keyboard stream is automatically
declared as stream System.in
The screen stream is automatically
declared as stream System.out
System
is a class,
out is a "member" of the class,
out itself contains stuff
...it has a method ("function") called println(...)
Hence: System.out.println("Hello!")
To get to a member of a class, use the dot operator "."
className.classMember gets you to the
member
objectName.objectMember gets you to the member
(C++ uses "::" for one case and "." for the other)
System.out contains various
methods
print(), println(), etc.
C: a "bunch of statements" is called a
function
Java: a "bunch of statements" is called a
method
To get to the println() method associated with stream
System.out
... use the dot (".") operator!
Conclusion: to print, call System.out.println()
Printing to the Screen: More Details
System.out.println
("Learn Java for fun and profit");
takes a string argument
But System.out.println(5)works too!
Is this a good quality of Java?
How can it do this?
Answer: There are several different
println() methods
The computer can determine which to use
How do you suppose the computer does this?