From charlesreid1

Cool: http://www.ibm.com/developerworks/library/j-robots/index.html

More cool: http://www.javamex.com/tutorials/exceptions/exceptions_uncaught_handler.shtml#

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!");
    }
}

Compiling

To compile this program, use the javac command:

$ javac HelloWorld.java

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

Running

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

Java/OOP

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(System.in)));

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

Compile

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

On Linux/Mac:

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

On Windows:

javac -cp .;org.example.jar mysource.java

After this, you obtain the bytecode file mysource.class

Run

Now run the bytecode file:

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


Libraries

awesome-java repo on github: https://github.com/akullpp/awesome-java

Immutables in Java (similar notation to javascript/d3): https://immutables.github.io/

libgdx for 3d games in Java: https://libgdx.badlogicgames.com/

stanford core NLP in java: https://stanfordnlp.github.io/CoreNLP/

legion of the bouncy castle (lightweight crypto in Java): https://www.bouncycastle.org/java.html

Selenium web browser test suite: http://docs.seleniumhq.org/

jsoup crawler and html parser https://jsoup.org/

twitter4j twitter client: http://twitter4j.org/en/index.html

crawler4j another crawler and html parser https://github.com/yasserg/crawler4j

Apache Shiro for session management (large enterprise or small mobile) https://shiro.apache.org/


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(System.in)));

Writing output to a file

Exceptions

See Java/Exceptions

Useful link: http://www.javamex.com/tutorials/exceptions/exceptions_unchecked.shtml

Interfaces

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: https://git.charlesreid1.com/cs/java/src/master/arrays/python-list/PythonList.java

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: https://git.charlesreid1.com/cs/java/src/master/lists/linked-lists/TList.java

public class MyContainer<T> { 

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

    private T data;

    // .....blah blah blah.....
}

Decorators

Lambda Expressions

Lambdas for Comparators

Flags





See also: