Java Reference
In-Depth Information
The comment describes the point where the program starts executing. Note that the //
comment syntax specifies that everything till the end of the line is a comment. However,
sometimes we want to specify a multi-line comment. In this case, we can use the following
syntax: /* ... */. The sequence /* is used to denote the start of the comment section and
the sequence */ is used to denote the end of comment section. Using the new syntax, our
previous comment can be rewritten as follows.
package helloworld ;
public class Main {
public static void main(String args []) {/∗
your program starts
executing here
∗/
}
}
Next, let us examine the code. The code package helloworld means that the file be-
longs to the package helloworld . One can think of a package as a software library that
contains several programs that are packaged together. If we did not select a package name
when creating the Main class, then this line would have been omitted and the class would
belong to the default package. Note that we created a class that is called Main .Asaresult,
afile Main.java was created.
All Java files have extensions .java. Java requires that a file that is called Main.java
contains a public class called Main . Every Java file contains exactly one public class.
This class has the same name as the name of the file. All class names (and therefore
Java file names) should start with a capital letter.
Packages are broken down into classes. For now, we can think of a class as an independent
part of the program, where more information will be provided in Chapter 6. Classes contain
methods and variables. The code in our example Main class contains the main method.
Every Java application contains the main method. This is where the program
starts executing. It is possible to have multiple main methods. In this case, however,
one needs to explicitly specify which main method should be the starting point of the
program.
Every method has a name. We can think of a method as “hired help”. For example, we
can ask Joe to add two numbers. Similarly, we can ask a method to add two numbers. When
the method finishes executing, it can return some information. For now, we will assume that
the main method always has the syntax: public static void main(String[] args) .The
text in the parentheses means that the main method can take as input a set of strings. A
full overview of methods is presented in Chapter 4. More information about the parameters
of the main method are presented in Chapter 5.
Note that there is an opening brace (a.k.a., curly bracket) after the definition of the class
and after the definition of the method in our example code. Braces in Java are used to define
the beginning and end of a block. For example, all the code inside the main method (or any
other method) should always be delimited by an opening and closing brace. Similarly, the
start and end of a class is represented by an opening and closing brace, respectively.
 
Search WWH ::




Custom Search