Java Reference
In-Depth Information
Here is our “hello world” program:
1 public class Hello {
2 public static void main(String[] args) {
3 System.out.println("Hello, world!");
4 }
5 }
This program defines a class called Hello . Sun has established the convention that
class names always begin with a capital letter, which makes it easy to recognize
them. Java requires that the class name and the file name match, so this program must
be stored in a file called Hello.java . You don't have to understand all the details of
this program just yet, but you do need to understand the basic structure.
The basic form of a Java class is as follows:
public class <name> {
<method>
<method>
...
<method>
}
This type of description is known as a syntax template because it describes the basic
form of a Java construct. Java has rules that determine its legal syntax or grammar.
Each time we introduce a new element of Java, we'll begin by looking at its syntax
template. By convention, we use the less-than ( < ) and greater-than ( > ) characters in a
syntax template to indicate items that need to be filled in (in this case, the name of
the class and the methods). When we write “...” in a list of elements, we're indicating
that any number of those elements may be included.
The first line of the class is known as the class header. The word public in the
header indicates that this class is available to anyone to use. Notice that the program
code in a class is enclosed in curly brace characters ({ }) . These characters are used
in Java to group together related bits of code. In this case, the curly braces are indi-
cating that everything defined within them is part of this public class.
So what exactly can appear inside the curly braces? What can be contained in a
class? All sorts of things, but for now, we'll limit ourselves to methods. Methods are
the next-smallest unit of code in Java, after classes. A method represents a single
action or calculation to be performed.
Method
A program unit that represents a particular action or computation.
Simple methods are like verbs: They command the computer to perform some
action. Inside the curly braces for a class, you can define several different methods.
 
Search WWH ::




Custom Search