Chapter 2
Coding a Simple program,
compiling & Running the programs.
|
Here is our first java source code
first.java
class first {
public static void main(String args[]) {
System.out.println("hello world");
}
} |
For the most computer languages, the name
of the file that holds the source code to a program is arbitrary. However,
this is not the case with Java. The first thing that you must learn about
Java is that the name you give to a source code file is very important.
For this example, the name of the source file sould be first.java. let’s
see why.
In Java, a source file is officially called
a compilation unit. It is a text file that contains one or more class definations.
The Java compiler requires that source file use the .java filename extension.
Notice that the file extension is four character long.
Compiling the program
To compile the first program, execute the
compiler, javac, specifying the name of the source file on the command
line, as shown here:
C:\>javac first.java
The javac compiler creates a file called
first.class that contains the bytecode version of the program. As discussed
earlier, the Java bytecode is the intermediate representation of your program
that contains instructions the Java interpreter will execute. Thus, the
output of javac is not code that can be directly executed.
To actually run the program, you must use
the Java Interpreter, called java. To do so, pass the class name first
as a command-line argument, as shown here:
c:\>java first
When the program is run the following output
is displayed:
Hello World
source code
|
|
Compiler
|
|
byte-code file
|
|
Interpreter
|
|
output screen
|
first.java
|
è
|
javac
|
è
|
first.class
|
è
|
java
|
è
|
Hello World
|
When Java source code is compiled, each individual
class is put into its own output file named after the class and using the
.class extension. This is why it is a good idea to give your Java
source files the same name as the class they contain-the name of the source
file will match the name of the .class file. When you execute the
Java interpreter as just shown, you are actually specifying the name of
the class that you want the interpreter to execute. It will automatically
search for a file by that name that has the .class extension. If it finds
the file, it will execute the code contained in the specified class.
Explanation of program
class first {
This line uses the keyword class to declare
that a new class is defined. First is an identifier that is the name of
the class. The entire class defination, including all of its members, will
be between the opening curly brace ( { ) and the closing curly brace (
} ).
public static void main(String args[ ])
All Java applications begin execution by calling
main(). ( This is like C/C++.)
The public keyword is an access specifier,
which allows the programmer to control the visibility of class members.
When a class member is preceded by public, then that member may be accessed
by code outside the class in which it is declared.
static allows main() to be called without
having to instantiate a particular instance of the class. This is necessary
since main() is called by Java interpreter before any objects are made.
The keyword void simply tells the compiler
that main() does not return a value.
Any information that you need to pass to a method
is received by variables specified within the set of parentheses that follow
the name of the method. These variables are called parameters. If there
is no parameter required for a given method, you still need to include
the empty parentheses. In main(), there is only one parameter. String
args[] declares a parameter named args, which is an array of instances
of the class String.
System.out.println("Hello world");
This line outputs the string "Hello world" followed
by a new line on the screen. Output is accomplished by the build-in println()
method. println() displays the string which is passed to it. The same can
be used for other datatypes too. System is a predefined class that
provides access to the system, and out is the output stream that
is connected to the console. And all statement in Java end with a semicolon.
second.java
/* This is the first Java program written
using jdk 1.1.1 compiler */
class second {
public static void main(String args[]) {
System.out.println("hello world"); // it prints hello world to screen
}
} |
Compile the above program and run it. You will not
find any difference between first and second Java program. In Java the
comments can be given in two ways. If it is multiline comment /* */
are to be used. If it is single line comment then // can be
used.
third.java
class third {
public static void main(String args[]) {
int i=20;
System.out.println("i ="+i ); // it prints the value
of i.
}
} |
The above program creates int type variable with
the name i and assigns the value 20 to it at the time of declaration. Afterwards
the value is concatenated with the string and given to println() method
to direct it to screen. No data type conversion is required while concatenating
the numeric value to character string.
|