This draft deletes the entire topic.
This draft moves the entire topic to .
Examples
-
To create your first Java program, begin by creating a new file in your text editor or IDE and save the file as
HelloWorld.java
. Then paste this code in that file and save the file:public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Note: In order for Java to recognize this as a
public
class
(and not throw a compile time error), the filename must have the same name as the class name (HelloWorld
in this example) with a.java
extension and should have thepublic
access modifier before it. Naming conventions recommend that Java classes begin with an uppercase character, and be in camel case format. This means no underscores (_
) or dollar signs ($
), even though these can both technically be used, and that the first letter of each word is capitalized.To compile your code, open the directory where
HelloWorld.java
is located in a terminal window:cd /path/to/containing/folder/
Note:
cd
is a terminal command which means change directoryand 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 intobytecode
class files. Optionally, the compiler can also process annotations found in source and class files using thePluggable
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 themain
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!
Note: In order for Java commands (
java
,javac
, etc) to be recognized, you will need to make sure:- A JDK is installed (e.g. Oracle, OpenJDK and other sources)
- Your environment variables are properly set up
You will need to use a compiler (
javac
) and an executor (java
) provided by your JVM. To find out which versions you have installed, enterjava -version
andjavac -version
on the command line. The version number of your program will be printed in the terminal (e.g.1.8.0_73
).
A closer look at the Hello World program
The "Hello World" program contains a single file, which consists of a
HelloWorld
class definition, amain
method, and a statement inside themain
method.public class HelloWorld {
The
class
keyword begins the class definition for a class namedHelloWorld
. 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 theJVM
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 asint
is expected (Java's way isSystem.exit()
).
This main method accepts:
- An array (typically called
args
) ofString
s 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 calledargs
. - 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 thejava
command.Inside the main method, we see the following statement:
System.out.println("Hello, World!");
Let's break this statement down element-by-element:
Element Purpose System
this denotes that the subsequent expression will call upon the System
class..
this is a "dot operator". Dot operators provide you access to a class's data1. In this case, this dot operator allows you to reference the out
static field within theSystem
class.out
this is the name of the static field of PrintStream
type within theSystem
class containing the standard output functionality..
this is another dot operator. This dot operator provides access to the println
method within theout
variable.println
this is the name of a method within the System
class'sout
variable. This method in particular prints the contents of the parameters into the console, on a new line.(
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. } // end of main function scope } // end of class HelloWorld scope
The method body and class body are then closed.
1 - Because the
HelloWorld
class has little relation to theSystem
class, it can only accesspublic
data.
Remarks
The Java programming language is...
-
General-purpose: It is designed to be used for writing software in a wide variety of application domains, and lacks specialized features for any specific domain.
-
Class-based: Its object structure is defined in classes. Class instances always have those fields and methods specified in their class definitions (see Classes and Objects). This is in contrast to non-class-based languages such as JavaScript.
-
Statically-typed: the compiler checks at compile time that variable types are respected. For example, if a method expects an argument of type
String
, that argument must in fact be a string when the method is called. -
Object-oriented: most things in a Java program are class instances, i.e. bundles of state (fields) and behavior (methods which operate on data and form the object's interface to the outside world).
-
Portable: It can be compiled on any platform with
javac
and the resultant class files can run on any platform that has a JVM.
Java code is compiled to bytecode (the .class
files) which in turn get interpreted by the Java Virtual Machine (JVM). In theory, bytecode created by one Java compiler should run the same way on any JVM, even on a different kind of computer. The JVM might (and in real-world programs will) choose to compile into native machine commands the parts of the bytecode that are executed often. This is called "Just-in-time (JIT) compilation".
Java Editions and Versions.
There are three "editions" of Java defined by Sun / Oracle:
- Java Standard Edition (SE) is the edition that is designed for general use.
- Java Enterprise Edition (EE) adds a range of facilities for building "enterprise grade" services in Java. Java EE is covered separately.
- Java Micro Edition (ME) is based on a subset of Java SE and is intended for use on small devices with limited resources.
There is a separate topic on Java SE / EE / ME editions.
Each edition has multiple versions. The Java SE versions are listed below.
Installing Java
There is a separate topic on Installing Java (Standard Edition).
Compiling and running Java programs
There are separate topics on:
- Compiling Java source code
- Java deployment including creating JAR files
- Running Java applications
- The Classpath
What's next?
Here are links to subjects to continue learning and understanding the Java programming language. These subjects are the basics of the Java programming to get you started.
- Primitive Data Types in Java
- Operators in Java
- Strings in Java
- Basic Control Structures in Java
- Classes and Objects in Java
- Arrays in Java
- Java code standards
Testing
While Java does not have any support for testing in the standard library, there are 3rd-party libraries that are designed to support testing. The two most popular unit testing libraries are:
Other
- Design patterns for Java are covered in Design Patterns.
- Programming for Android is covered in Android.
- Java Enterprise Edition technologies are covered in Java EE.
- The Oracle JavaFX technologies are covered in JavaFX.
Versions
Java SE Version | Code Name | End-of-life (free1) | Release Date |
---|---|---|---|
Java SE 9 (Early Access) | None | future | 2017-07-27 |
Java SE 8 | None | future | 2014-03-18 |
Java SE 7 | Dolphin | 2015-04-14 | 2011-07-28 |
Java SE 6 | Mustang | 2013-04-16 | 2006-12-23 |
Java SE 5 | Tiger | 2009-11-04 | 2004-10-04 |
Java SE 1.4 | Merlin | prior to 2009-11-04 | 2002-02-06 |
Java SE 1.3 | Kestrel | prior to 2009-11-04 | 2000-05-08 |
Java SE 1.2 | Playground | prior to 2009-11-04 | 1998-12-08 |
Java SE 1.1 | None | prior to 2009-11-04 | 1997-02-19 |
Java SE 1.0 | Oak | prior to 2009-11-04 | 1996-01-21 |
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft