System
System.out.print
out.print
importjava.util.Scanner;
<strong>import static java.lang.System.in;</strong>
<strong>import static java.lang.System.out;</strong>
classTwoTeams {
public static void main(String args[]) {
Scanner keyboard = new Scanner(<strong>in</strong>);
inthankees, socks;
out.print("Hankees and Socks scores? ");
hankees = keyboard.nextInt();
socks = keyboard.nextInt();
out.println();
if (hankees> socks) {
<strong>out.print("Hankees: ");</strong>
<strong>out.println(hankees);</strong>
<strong>out.print("Socks: ");</strong>
<strong>out.println(socks);</strong>
} else {
}
keyboard.close();
Of course, computers don’t work that way. If you want a computer to “know” what out.print means, you have to code that knowledge somewhere inside the Java compiler.
Fortunately, the ability to abbreviate things like System.out.print is available from Java 5.0 onward. This ability to abbreviate things is called static import. It’s illustrated in the second and third lines of the code above.
Whenever you start a program with the line
import static java.lang.System.out;
You can replace System.out with plain out in the remainder of the program. The same holds true of System.in. With an import declaration near the top, you can replace new Scanner(System.in) with the simpler new Scanner(in).
System.out
out
System.in
Scanner(System.in)
Scanner(in)
You may be wondering what all the fuss is about. If you can abbreviate java.util.Scanner by writing Scanner, what’s so special about abbreviating System.out? And why do I have to write out.print? Can I trim System.out.print to the single word print? Look again at the first few lines. When do you need the word static? And what’s the difference between java.util and java.lang?
java.util
Scanner
print
static
java.lang
Before you can fix these issues, you need to understand classes, packages, and static members.
Until then, just paste three import declarations to the top of your Java programs and trust that everything will work well.
You can abbreviate System.out with the single word out. And you can abbreviate System.in with the single word in. Just be sure to copy the import declarations exactly as you see them above. With any deviation, you may get a compiler error.
in
import
System.out.println("How many donuts are in a dozen?");
int number = keyboard.nextInt();
if (number = 12) {
System.out.println("That's correct.");
System.out.println("Sorry. That's incorrect");
int n = 100;
if (n > 100)
System.out.println("n is big");
System.out.println("Will Java display this line of text?");
if (n <= 100)
System.out.println("n is small");
System.out.println("How about this line of text?");
Y
:-)
Otherwise, the code displays this:
:-(
if
You win!
You lose
c
m
Sorry!
I thought you were a poetry buff.
Maybe you'll want to see the poem the next time you run this program.
Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.