Java Reference
In-Depth Information
where
x
is a string for the text to be displayed, and
y
is a string for the title of the message
box. The fourth argument can be
JOptionPane.INFORMATION_MESSAGE
, which causes
the information icon (
) to be displayed in the message box, as shown in the following
example.
JOptionPane.showMessageDialog(
null
,
"Welcome to Java!"
,
"Display Message"
,
JOptionPane.INFORMATION_MESSAGE);
Note
There are two types of
import
statements:
specific import
and
wildcard import.
The
specific import
specifies a single class in the import statement. For example, the follow-
ing statement imports
JOptionPane
from the package
javax.swing
.
specific import
import
javax.swing.JOptionPane;
The
wildcard import
imports all the classes in a package by using the asterisk as the
wildcard. For example, the following statement imports all the classes from the package
javax.swing
.
wildcard import
import
javax.swing.*;
The information for the classes in an imported package is not read in at compile time or
runtime unless the class is used in the program. The import statement simply tells the
compiler where to locate the classes. There is no performance difference between a spe-
cific import and a wildcard import declaration.
no performance difference
Note
Recall that you have used the
System
class in the statement
System.out.println
("Welcome to Java");
in Listing 1.1. The
System
class is not imported because it is
in the
java.lang
package. All the classes in the
java.lang
package are
implicitly
imported in every Java program.
java.lang
package
implicitly imported
1.41
✓
✓
What is the statement to display the message “Hello world” in a message dialog box?
Check
1.42
Point
Why does the
System
class not need to be imported?
1.43
Are there any performance differences between the following two
import
statements?
import
javax.swing.JOptionPane;
import
javax.swing.*;
Good programming style and proper documentation make a program easy to read and
help programmers prevent errors.
Key
Point
Programming style
deals with what programs look like. A program can compile and run
properly even if written on only one line, but writing it all on one line would be bad pro-
gramming style because it would be hard to read.
Documentation
is the body of explanatory
remarks and comments pertaining to a program. Programming style and documentation are
as important as coding. Good programming style and appropriate documentation reduce the
chance of errors and make programs easy to read. This section gives several guidelines. For
programming style
documentation