From charlesreid1


More cool:

Getting Started

Download and install Java, if you need to.

Hello World

This will walk through compiling and running a "Hello world" example in Java.

The program

Here's a simple Java "Hello world" program:

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


To compile this program, use the javac command:

$ javac

This will create a HelloWorld.class file, which is the Java executable.


To run a Java executable, use the java command, and pass it the executable's filename without the extension:

$ java HelloWorld
Hello, world!

Object Oriented Programming


Abstract methods: Abstract Methods

Measuring Performance of Java Code

See Java/Profiling and Java/Timing

Also see Java/Memory for memory profiling

Easily Reading from Input Files

Like many components of Java, it's easy to get tripped up with the simple stuff, since there are many ways to do it. Here's the canonical way:

Scanner s = new Scanner(new BufferedReader(new InputStreamReader(;

This allows you to pass input file contents on the command line like so:

$ java MyProgram < data.txt

See Java/Input Files

Using Jar Files

If you have a jar file that contains class definitions and you wish to use it, you can do the following:

  • To check contents of the jar, use the jar utility - it works a lot like tar
  • jar xf - expands (shows contents of) jar


To compile code with a jar file called org.example.jar:

On Linux/Mac:

javac -cp '.:org.example.jar'

On Windows:

javac -cp .;org.example.jar

After this, you obtain the bytecode file mysource.class


Now run the bytecode file:

java -cp '.:org.example.jar' mysource
java -cp .;org.example.jar mysource


awesome-java repo on github:

Immutables in Java (similar notation to javascript/d3):

libgdx for 3d games in Java:

stanford core NLP in java:

legion of the bouncy castle (lightweight crypto in Java):

Selenium web browser test suite:

jsoup crawler and html parser

twitter4j twitter client:

crawler4j another crawler and html parser

Apache Shiro for session management (large enterprise or small mobile)

Assorted Basic Features

File Reading/Writing

There's a particular way to allow for easy reading and writing to a file so that you can say:

$ java MyProgram < in.txt > out.txt

Reading input from file

To read from an input file:

Scanner s = new Scanner(new BufferedReader(new InputStreamReader(;

Writing output to a file


See Java/Exceptions

Useful link:


See Java/Interfaces

Collections interface and Iterable interface are two common interfaces in Java API:

Example of class that uses these: List class

Assorted Advanced Features

Generics in Java

Generics involve designing a data structure or class that takes an object of arbitrary type. Java provides the "old" way to do it, and the "new" way to do it.

Old (object array) approach

A "cheap" way to get this without the use of generics (older versions of Java) was to store underlying objects in an Object[] array, and let the type of object be determined at runtime.

An example, demonstrated by implementing an array-based, Python-list-like Java object, is here:

This does not use templating, and illustrates the Object[] array notation/approach.

This leads to difficulties with consistent typing of collections of data. Hence the diamond notation introduced in (relatively) recent versions of Java.

New (diamond notation) approach

The "new" way to do it in recent versions of Java is to work like a Collections object, using the diamond notation. When the class is defined, a type <T> can be specified as a "template" type, in place of the generic Object class.

An example of this is here, in a generics/templated linked list:

public class MyContainer<T> { 

    // .....blah blah blah.....

    private T data;

    // .....blah blah blah.....


Lambda Expressions

Lambdas for Comparators


See also: