A Simple
HelloWorld.java program
HelloWorld.java Let's look into our first program. Save the following text into the file called HelloWorld.java -------------------------------------------------------------------------------- class HelloWorld {
-------------------------------------------------------------------------------- We examine that there is a class called HelloWorld. Since HelloWorld is the only class in the file, it is called the main class. The main class of the file has to be runnable in order to make your program runnable. In order to make your program runnable, you should have a function called main which is defined as: public static void main(Strings[] args) The explanation is as follows: The public qualifier tells you that the function main is callable from outside. You don't have to worry about this one yet as the concept of making a function private or public is later described in Object Oriented Programming (OOP) philosophy. Then the next qualifier is static. You don't have
to worry about this either.
You can consider System.out.println as a command to print some text on the screen. Not that the text has to be surrounded by the double quotes. In this case, "Hello World!" is being printed, without the quotes of course. Remember that almost every command in Java ends with a semicolon(;). IMPORTANT: The main class name has to be the same as the program file name. Otherwise, your Java program won't compile. In this example the class name is HelloWorld. The program file has to be HelloWorld.java. You have to pay attention that this naming convention is CASE SENSITIVE it means that naming your file into helloworld.java WON'T work. If your program won't compile, pay attention to the capitalization of your file name. This rule also applies even in Windows platform. How to Compile and Run Your Program
javac HelloWorld.java
If it produces a file called HelloWorld.class, it means that your compilation is successful. To run it, under Windows or UNIX, type the following: java HelloWorld
Under Macintosh, simply double-click your HelloWorld.class file. Commenting Your Program
Double slash comment. Example:
Well Done!
Related:
Java Certification, Programming, JavaBean and Object Oriented Reference Books Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|