Java Programming/First Java Program

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Navigate Getting Started topic:

[edit] Hello World

Generally when you first start programming in any language, you'll start with the traditional Hello World example. That said, let's start building your first Java program. You guessed it, it's Hello World! Before starting this exercise, make sure you know how to compile and run Java programs.

Open your IDE and write the following text. Pay close attention to capitalization, as Java is case sensitive.

public class HelloWorld {  
   public static void main(String[] args) {     
      System.out.println("Hello, world!");       
  }
}

Save it as HelloWorld.java. Again, make sure that the filename is the same case as the class name. Compile and run it:

javac HelloWorld.java
java HelloWorld

Your computer should display

Hello, world!

[edit] Line-by-line Analysis

The first line of the class,

public class HelloWorld {

declares a Java class named HelloWorld. This class is declared public - it is available to any other class. The next line,

public static void main(String[] args) {

begins a Java method named main. The main method is where Java will start executing the program. args is a method parameter which will contain the command line arguments used to run the program. The method must be both public and static for the program to run correctly. For more information on modifiers such as public and static, see Access Modifiers.

The

System.out.println("Hello, world!");

statement sends the text Hello, world! to the console (with a line terminator). The final two braces mark the end of the main method and the end of the class.

[edit] Modifying the Program

Now, we will modify this program to print the first command line argument, if there is one, along with the greeting. For example, if you invoke the program as

java HelloWorld wikibooks

it will print

Hello, wikibooks!

Go back to your the program, and modify it to read

public class HelloWorld {  
   public static void main(String[] args) {     
      String who;
      if (args.length > 0) {
         who = args[0];
      } else {
         who = "World";
      }
      System.out.println("Hello, " + who + "!");  
  }
}

Run it again. It should display

Hello, wikibooks!

or, if you do not pass a command line parameter, it will simply print

Hello, World!

Wikipedia has a hello world sample for each type of java code, for more information see w:Java (programming language)#Hello World


[edit] Common Problems

If the program does not work as you expect, check the following common errors.

  • Are you sure all words are spelled correctly and with the exact case as shown?
  • Are there semicolons and brackets in the appropriate spot?
  • Are you missing a quote?

[edit] The Next Step

Now that you have seen the classic Hello, World program in Java, let's move on to a more realistic example which highlights the object oriented nature of Java. Visit Understanding a Java Program which presents key Java language features along with a more complete explanation of the syntax and structure of a basic Java program.


[edit] Beginners topics

[edit] User Interface topics

[edit] Extra Reading

[edit] Advanced topics

Personal tools