Java
Creating Your First Java Program
Create a new file in your text editor or IDE named HelloWorld.java. Then paste this code block into the file and save:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); }
}
Note: For Java to recognize this as a public class (and not throw a compile time error), the filename must be the same as the class name (HelloWorld in this example) with a .java extension. There should also be a public access modifier before it.
Naming conventions recommend that Java classes begin with an uppercase character, and be in camel case format (in which the first letter of each word is capitalized). The conventions recommend against underscores (_) and dollar signs ($).
To compile, open a terminal window and navigate to the directory of HelloWorld.java:
cd /path/to/containing/folder/
Note: cd is the terminal command to change directory.
Enter javac followed by the file name and extension as follows:
$ javac HelloWorld.java
Note: The javac command invokes the Java compiler.
The compiler will then generate a bytecode file called HelloWorld.class which can be executed in the Java Virtual Machine (JVM). The Java programming language compiler, javac, reads source files written in the Java programming language and compiles them into bytecode class files. Optionally, the compiler can also process annotations found in source and class files using the Pluggable Annotation Processing API. The compiler is a command line tool but can also be invoked using the Java Compiler API.
To run your program, enter java followed by the name of the class which contains the main method (HelloWorld in our example). Note how the .class is omitted:
$ java HelloWorld
Note: The java command runs a Java application.
This will output to your console:
Hello, World!
You have successfully coded and built your very first Java program!
A closer look at the Hello World program
The "Hello World" program contains a single file, which consists of a HelloWorld class definition, a main method, and a statement inside the main method.
public class HelloWorld {
The class keyword begins the class definition for a class named HelloWorld. Every Java application contains at least one class definition (Further information about classes).
public static void main(String[] args) {
This is an entry point method (defined by its name and signature of public static void main(String[])) from
which the JVM can run your program. Every Java program should have one. It is:
- public: meaning that the method can be called from anywhere mean from outside the program as well. See Visibility for more information on this.
- static: meaning it exists and can be run by itself (at the class level without creating an object).
- void: meaning it returns no value. Note: This is unlike C and C++ where a return code such as int is expected (Java's way is System.exit()).
This main method accepts:
- An array (typically called args) of Strings passed as arguments to main function (e.g. from command line arguments).
Almost all of this is required for a Java entry point method.
Non-required parts:
- The name args is a variable name, so it can be called anything you want, although it is typically called args.
- Whether its parameter type is an array (String[] args) or Varargs (String... args) does not matter because arrays can be passed into varargs.
Note: A single application may have multiple classes containing an entry point (main) method. The entry point of the application is determined by the class name passed as an argument to the java command.
Inside the main method, we see the following statement:
System.out.println("Hello, World!");
Let's break down this statement element-by-element:
- System - this denotes that the subsequent expression will call upon the System class, from the java.lang package.
- . - this is a "dot operator". Dot operators provide you access to a classes members1; i.e. its fields (variables) and its methods. In this case, this dot operator allows you to reference the out static field within the System class
-
out - this is the name of the static field of PrintStream type within the System class containing the
standard output functionality.
-
. - this is another dot operator. This dot operator provides access to the println method within the out variable.
println - this is the name of a method within the PrintStream class. This method in particular prints the contents of the parameters into the console and inserts a newline after.
( - this parenthesis indicates that a method is being accessed (and not a field) and begins the parameters being passed into the println method.
"Hello, World!" - this is the String literal that is passed as a parameter, into the println method. The double quotation marks on each end delimit the text as a String.
) - this parenthesis signifies the closure of the parameters being passed into the println method.
: - this semicolon marks the end of the statement.
Note: Each statement in Java must end with a semicolon (;).
The method body and class body are then closed.
} // end of main function scope
} // end of class HelloWorld scope