Friday, July 4, 2008

JAVA-3(APPLICATION)

The Hello World Application:-
class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World!");
}

}
Hello World is very close to the simplest program imaginable. When you successfully compile and run it, it prints the words "Hello World!" on your display. Although it doesn't teach very much programming, it gives you a chance to learn the mechanics of typing and compiling code. The goal of this program is not to learn how to print words to the terminal. It's to learn how to type, save and compile a program. This is often a non-trivial procedure, and there are a lot of things that can go wrong even if your source code is correct.
To write the code you need a text editor. You can use any text editor like Notepad, Brief, emacs or vi. Personally I use BBEdit on the Mac and TextPad on Windows.
You should not use a word processor like Microsoft Word or WordPerfect since these save their files in a proprietary format and not in pure ASCII text. If you absolutely must use one of these, be sure to tell it to save your files as pure text. Generally this will require using Save As... rather than Save. If you have an integrated development environment like BlueJ 1.0 or Borland JBuilder, that will include a text editor you can use to edit Java source code. It will probably change your words various colors and styles for no apparent reason. Don't worry about this yet. As long as the text is correct you'll be fine.
When you've chosen your text editor, type or copy the above program into a new file. For now type it exactly as it appears here. Like C and unlike Fortran, Java is case sensitive so System.out.println is not the same as system.out.println. CLASS is not the same as class, and so on.
However, white space is not significant except inside string literals. The exact number of spaces or tabs you use doesn't matter.
Save this code in a file called HelloWorld.java. Use exactly that name including case. Congratulations! You've written your first Java program.

Saving files on Windows:-
Some Windows text editors including Notepad add a three letter ".txt" extension to all the files they save without telling you. Thus you can unexpectedly end up with a file called "HelloWorld.java.txt." This is wrong and will not compile. If your editor has this problem, you should get a better editor. However in the meantime enclose the filename in double quotes in the Save dialog box to make editor save the file with exactly the name you want.

Compiling and Running Hello World:-
To make sure your Java environment is correctly configured, bring up a command-line prompt and type
javac nofile.java
If your computer responds with
error: Can't read: nofile.java
you're ready to begin. If, on the other hand, it responds
javac: Command not found
or something similar, then you need make sure you have the Java environment properly installed and your PATH configured.
Assuming that Java is properly installed on your system there are three steps to creating a Java program:
writing the code
compiling the code
running the code
Under Unix, compiling and running the code looks like this: $ javac HelloWorld.java
$ java HelloWorld
Hello World
$
Under Windows, it's similar. You just do this in a DOS shell. C:> javac HelloWorld.java
C:> java HelloWorld
Hello World
C:>
Notice that you use the .java extension when compiling a file, but you do not use the .class extension when running a file.
For IDEs, consult your product documentation.

No comments: