Wednesday, 11 July 2007

Java Fundamentals (part 4)

Simple Application & Applet


Before we get into nuts and bolts of the Java language lets create a simple Java application and an applet to see how really easy it is to develop applications and applets. We will first type the code, compile it and then run it. Don’t worry about what each line of the code does at the moment it will all be explained in a moment, first just concentrate on how to compile and run the Java Program.


Exercise 1 - 1The “Hello World” Application


Using a plain text editor, create a file named HelloWorldAppc.java with the following Java code:



/* *
* The HelloWorldAppc class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldAppc {
    public static void main(String[] args){
//Display the string
System.out.println("Hello World!");
}

Java source code is always stored in files with the extension .java


Be aware that Java is case sensitive. In other words, Graphics is not the same as graphics!


Compile the file using Java compiler that comes with JDK, you would do this by typing the following command at your MSDOS command prompt (windows) :


javac HelloWorldAppc.java


If the compilation succeeds, the compiler creates a file named HelloWorldAppc.class in the same directory (folder) as the Java source file. This class file contains the Java bytecodes, which are platform-independent codes interpreted by the Java runtime system. If the compilation fails, make sure you typed in and named the program exactly as shown above, using the capitalization shown.


Run the program using the Java interpreter, to accomplish this enter the following command:


java HelloWorldAppc


You should see "Hello World!" displayed at your command line

No comments: