How does it work ?
- Compiling the program : To compile a program we have to use the statement javac prog1.java (where “prog1.java” is the name of the Java program)
![]() |
Java Program Compilation Process |
This statement invokes the Java compiler, javac and compiles the program. As a result of this compilation a new file having the name “prog1.class” is created. This “.class” file has something known as the “Java Bytecode”. Byte codes are a set of instructions that looks a lot like some machine codes, but that is not specific to any one processor. There is huge difference in the way programs are compiled in Java or other languages like C or C++. In other languages when you compile a program it is translated by the compiler into the machine code or what we called machine language. This machine language contains the processor specific instructions on how to run the program on a specific computer. For example, if you compile your program on a Pentium system, the resulting program will run only on other Pentium systems. If you want to use the same program on another system, you have to go back to your original source, get a compiler for that system, and recompile your code. But this is not the case with Java.
In Java, the compilation phase is divided into two stages. First the program is compiled using the “javac” command and then the program is executed using the “java” command that invokes the Java interpreter.
The Java development environment has two parts: a Java compiler and a Java interpreter. The Java compiler converts the source code into the Byte code.
Having your Java programs in byte code form means that instead of being specific to any one system, your programs can be run on any platform and any operating or window system as long as the Java interpreter is available. This capability of a single binary file to be executable across platforms is crucial to what enables applets to work, because the World Wide Web itself is also platform independent.
- class prog1 {
The keyword “class” denotes that a new class is being defined whose name is “prog1”. The body of the class is enclosed within the pair of curly braces – { }.
- public static void main(String args[]) {The above line starts the definition of the main(). As you are aware that the main() marks the beginning of the program. All java applications (except Applets) must have a main() which make them executable.
- The public keyword is an access specifier, which allows the programmer to control the visibility of class members. There are three access specifiers - private, public and protected. When a class member (variable or method) is preceded by public, it denotes that member can be accessed from outside the class in which it is declared. Since main() is required to be called from outside of its class it must be declared as public.
- As you know, a class must instantiated in order to use it members but you can see that there is no object of the class “prog1” declared. The keyword static allows main() to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.
- The keyword void simply tells the compiler that main() does not return a value. If a function returns a value the “void” keyword is replaced by the type of value that it will return.
- The “String args[]” is an array of type String that is used to store command line arguments. Command line arguments are the values that are passed to a Java program while running it. Java automatically stores the arguments in the array “arr”. Of course you can change the name of the array from “arr” to any other name but it has become a standard practice for Java programmers the world over to use this name. When the user passes some values, args stores the first value in index no 0, second value to index no 1 and so on.
- The last character on the line is the {. This signals the start of main( )’s body.
- Inside main(), there is a statement
System.out.println("This is a simple Java program.");
This line prints the string “This is a simple Java program.” followed by a new line on the screen. You can also use the command “System.out.print()” instead of “System.out.println()” , if you want the output to appear on the same line. You can also say that “println()” is somewhat similar to “\n” of C or “endl” of C++.
0 comments:
Post a Comment