Java Tutorial/Hello World!

From Wikiversity
Jump to navigation Jump to search

java hello world program

Hello World Java Program[edit]

public class HelloWorld {
    public static void main( String[] args ) {
        System.out.println( "Hello World!" );
        System.exit( 0 ); //success
    }
}

Compiling the Program[edit]

Type this program up in your text editor and save it as HelloWorld.java. Go to your console window (in Windows, go to the start menu and click "run", then type "cmd.exe"; in Linux, open a terminal). Use the cd command (cd dirname or .. to go up one directory) to navigate to the directory where HelloWorld.java was saved. Compile the program using the command javac HelloWorld.java. Remember that Java is case-sensitive, so even when you compile your program, you have to make sure you are typing in the file name exactly.

Compiling the program will produce the file HelloWorld.class, the JVM version of your program. This code is machine independent.

Your first impression of this program is probably "This is really long just to display a line of text." While this may be true compared to other programming languages, this program shows a lot about the Java programming language, way beyond simply displaying a line of text.

Running the Program[edit]

To run the compiled version of the HelloWorld program, on the command line simply type:

java HelloWorld

Do not add the ".class" extention to the name; the Java interpreter will already be able to locate the file based on the class name you provided, and adding the extention incorrectly will simply confuse it.

Line-by-line analysis[edit]

Let us examine the program line by line. The first line

public class HelloWorld {

declares a class called HelloWorld. (Almost) everything in Java is contained in something called a class. Classes are the basis of Object-Oriented Programming (OOP) which we will discuss in a later lesson.


The second line

public static void main( String[] args ) {

declares a method called main(String[]). Most of the actual code in Java is found within methods. Methods are one of the elements of classes. This particular method is executed when the HelloWorld class is executed using the java command. Note the String[] args. This means that the "arguments" will be passed into the main method - essentially, within this method, you can edit and see arguments.


The third line

System.out.println( "Hello World!" );

does pretty much what you think it does: it writes "Hello World!" to your screen. println means to print the string (text) provided and then add a line break (like the enter key).


The fourth line

System.exit( 0 ); //success

exits the program with return code of 0, meaning success. Anything besides 0 means that an error was encountered. Note the //success after System.exit( 0 );. This is a comment, meaning that it will not be read by the compiler (javac). Comments all begin with a "//", and anything after the comment (on the same line) will not be read. Comments are used so that you or anyone reading your code know what the code is doing.

The remaining lines are to close the method and the class.

Excercises[edit]

Play with the Hello World program, and:

  • Change the text displayed
  • Display multiple lines
  • Play around with comments
  • Try deleting or changing parts of the program like changing "public" to "private" and recompile to see what happens.
  • Try adding \n\t\t\t at the beginning of the Hello World!!! string.

Example:

public class HelloWorld {
    public static void main( String[] args ) {
        System.out.println("Hello, my name is Noah!");
        System.out.println("I will be your virtual assistant!");
        System.out.println("Please, be my guest");
        //System.out.println("this won't be printed!")
        //this is a comment
        System.exit(0); //another comment
    }
}
Previous: Choosing the right text editor Up: Java Tutorial Next: Variables