Sams Teach Yourself C in 21 Days

(singke) #1
Java Language Fundamentals 705

BD4


You can also use the *wildcard to import all classes in a package:
import java.io.*;
This line imports all classes in the java.iopackage. Packages that are part of Java have
names starting with java. If your program makes reference to a class without having
imported it, the compiler generates an error. A Java program can have as many import
statement as it needs.

A package is simply a file that contains classes. Using the wildcard character,
Note you can include all of the classes from the file.

Methods ........................................................................................................

In Java, a methodis essentially the same thing as a function in C or C++. Methods are so
named simply because Java is a completely object-oriented language and, by convention,
objects have methods, not functions. You saw an example of a method in the Hello,
Worldexample presented on Bonus Day 1, “Object-Oriented Programming Languages.”
In a Java program, most of the code is in methods.

Comments ......................................................................................................

No programming language is complete without the capability to add comments to the
source code. Java has three comment styles. One is just like a C comment; anything
between/*and*/is a comment and is ignored by the compiler:
/* This is
all
one
big
comment */
The second style is the same as C++; anything after //on a line is a comment:
// This is a comment.
X = 5; //This is also a comment.
Java’s third comment style is used for automatic generation of documentation. It is like
the C comment style with the addition of an extra asterisk:
/** This comment will be included in class documentation
created by the Javadoc tool. */
With this style of comment, you can us a program called Javadoc to generate documenta-
tion from your program. The use of Javadoc is beyond the scope of this book, but you
need to know about this comment style in case you run across it in Java code.

39 448201x-Bonus4 8/13/02 11:19 AM Page 705

Free download pdf