Java Language


This draft deletes the entire topic.

expand all collapse all

Examples

  • 181

    Create a new text file named HelloWorld.java and paste this code in it:

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

    Run live on Ideone

    Note: In order for Java to recognize this as a class (and not throw a compiler error), the filename must comprise the name of the class (HelloWorld in this example) with a .java extension. Convention states that Java classes should begin with an uppercase character.

    To compile your code, open the directory where HelloWorld.java is located in a terminal window:
    cd /path/to/containing/folder/ and 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.

    Next enter java, followed by the name of the class with the main method (HelloWorld in our example). Notice how the .class is omitted:

    $ java HelloWorld
    

    Note: The java command runs a Java application.

    This will output:

    Hello, World!

    You have successfully programmed and built your very first Java program!

    Note: In order for Java commands (java, javac, etc) to be recognised, 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. java -version and javac -version on the command line, will report which versions (e.g. 1.8.0_73) of these are installed.


    A closer look at the Hello World application

    The Hello World application consists of a HelloWorld class definition and a main method.

    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).

    The main method:

    This is an entry point method (defined by its name main) from which the JVM can run your program. Every Java program should have one. It is:

    • public: meaning where the method can be called from isn't limited to inside your program.
    • 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 where a return code such as int is expected (Java's way is System.exit()).

    It accepts :

    Almost all of this is required for a Java entry point method. The exceptions being:

    • The name args is a variable name, so can be called anything.
    • Whether its type is an array or Varargs (String... args).

    Note A single application may have multiple classes containing an entry point (main) method, and the entry point of the application is determined by the class name passed as argument to the java command.

I am downvoting this example because it is...

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".

Installing Java

There is a separate topic on Installing Java (Standard Edition).

Compiling and running Java programs

There are separate topics on:

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:

Versions

VersionCode NameRelease Date
Java SE 9 (Early Access)None2017-03-23
Java SE 8None2014-03-18
Java SE 7Dolphin2011-07-28
Java SE 6Mustang2006-12-23
Java SE 5Tiger2004-10-04
Java SE 1.4Merlin2002-02-06
Java SE 1.3Kestrel2000-05-08
Java SE 1.2Playground1998-12-08
Java SE 1.1None1997-02-19
Java SE 1.0Oak1996-01-21
Still have a question about Java Overview? Ask Question

Creating a new Java program

181

Create a new text file named HelloWorld.java and paste this code in it:

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

Run live on Ideone

Note: In order for Java to recognize this as a class (and not throw a compiler error), the filename must comprise the name of the class (HelloWorld in this example) with a .java extension. Convention states that Java classes should begin with an uppercase character.

To compile your code, open the directory where HelloWorld.java is located in a terminal window:
cd /path/to/containing/folder/ and 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.

Next enter java, followed by the name of the class with the main method (HelloWorld in our example). Notice how the .class is omitted:

$ java HelloWorld

Note: The java command runs a Java application.

This will output:

Hello, World!

You have successfully programmed and built your very first Java program!

Note: In order for Java commands (java, javac, etc) to be recognised, 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. java -version and javac -version on the command line, will report which versions (e.g. 1.8.0_73) of these are installed.


A closer look at the Hello World application

The Hello World application consists of a HelloWorld class definition and a main method.

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).

The main method:

This is an entry point method (defined by its name main) from which the JVM can run your program. Every Java program should have one. It is:

  • public: meaning where the method can be called from isn't limited to inside your program.
  • 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 where a return code such as int is expected (Java's way is System.exit()).

It accepts :

Almost all of this is required for a Java entry point method. The exceptions being:

  • The name args is a variable name, so can be called anything.
  • Whether its type is an array or Varargs (String... args).

Note A single application may have multiple classes containing an entry point (main) method, and the entry point of the application is determined by the class name passed as argument to the java command.

Topic Outline