First Java Program

From Wikibooks, open books for an open world
Jump to: navigation, search

Execution Java Programming
First Java Program
Understanding a Java Program
Navigate Getting Started topic: v  d  e )

In order to write your first Java program, follow the instructions below:

1. Proceed only if you have successfully installed and configured your system for Java.
Read our guide on installing the Java platform.
2. Open your preferred text editor – this is the editor you set while installing the Java platform.
For example, Notepad or Notepad++ on Windows; Gedit, Kate or SciTE on Linux; or, XCode on Mac OS, etc.
3. Write the following lines of code in a new text document:
Computer code
public class FirstProgram {

  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
4. Save the file as FirstProgram.java – the name of your file should be the same as the name of your class definition and followed by the .java extension. This name is case-sensitive, which means you need to capitalize the precise letters that were capitalized in the name for the class definition.
5. Next, open your preferred command-line application.
For example, Command Prompt on Windows; and, Terminal on Linux and Mac OS.
6. In your command-line application, navigate to the directory where you just created your file. If you do not know how to do this, consider reading through our crash courses for command-line applications.
7. Compile the Java source file using the following command which you can copy and paste in if you want:
javac FirstProgram.java
8. Once the compiler returns back to the prompt, run the application using the following command:
java FirstProgram
9. The above command should result in your command-line application displaying the following result:
Hello World!

[edit] Did the program execute correctly?

Ask for help if the program did not execute properly in the Discussion page for this chapter.

If the program does not work as you expect, check the following common problems:

  • 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? Usually, modern IDEs would try coloring the entire source as a quote in this case.
  • Are you launching the javac or java programs correctly?
  • Did you navigate to the program's location in the command prompt using the CD (Change Directory) function?

[edit] Analysis of your first Java program

In the code for your first Java program, you created a class called FirstProgram which serves as a virtual abstraction model for our program. You could have named this anything but we chose the name FirstProgram in order to highlight the fact that this abstraction serves as the very first program we create in Java.

Computer code
public class FirstProgram { }


Notice how we placed the public access modifier next to the name for the class definition. This tells us that the class is accessible by the Java environment it exists within. In our case, this environment is the Java Runtime Environment (JRE) itself, which would have direct access to the FirstProgram class and all its public member properties, fields and methods.

Computer code
public class FirstProgram {

  public static void main(String[] args) { }
}


Therefore, when we declare the main(...) method, we make sure that this method is accessible to the JRE using the public access modifier. This particular method has some interesting features associated with it. We can see that the method itself returns nothing (as is indicated by the void keyword) but the method accepts arguments args of type String[]. The square brackets [ and ] tells us that this the args argument is a list of type String, therefore instead of just one String, this variable can hold several String elements. Such variables are called arrays.

Note:
Main methods are a part of your code that identify your code as being executable. If you have a main method in your code, you can run that code; if you don't have a main method in your code, you cannot run it. In Java, declaring a main method means exposing your class as a candidate for execution using the java command of your Java Runtime Environment (JRE).

Learn more about main methods in similar programming languages

Main methods in similar programming languages

Main methods or functions are a prominent feature of programming languages that are similar to the Java programming language. This is the primary function or method that enables a program written in those languages to be executable.

For a complete list of main methods for various other languages, see the Wikipedia article on main functions.

C#

In C# (just as in Java), a main method is the first method that a virtual machine searches for within a program in order to execute it. Thus, the main method is sometimes also called the main entry point in the .NET C# parlance.

static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)

C and C++

int main(void)
int main(int argc, char *argv[])
int main()

Finally, within the body of the main method, we have written a single line of code:

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


This command is called a print statement and is used to print out or display stuff on the command-line application interface. It's a slightly complicated routine to write the whole statement but you get used to writing it in your programs. We will be explaining this line and other System commands in a later chapter.


Execution Java Programming
First Java Program
Understanding a Java Program
Personal tools
Namespaces

Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export