Java
From charlesreid1
Cool: http://www.ibm.com/developerworks/library/j-robots/index.html
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!
Measuring Performance of Java Code
See Java/Profiling and Java/Timing
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/
- 3d graphics: https://github.com/libgdx/libgdx/wiki/3D-Graphics
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
Common Java Exceptions
First, if you want to compile your own list of Java exceptions, each package in the Java standard API will list a package summary on its package summary page. This has an "Exceptions summary" at the bottom that gives a brief summary of all the exceptions defined in a Java package.
For java.io.* the page is here: http://docs.oracle.com/javase/7/docs/api/java/io/package-summary.html
That being said, here are the most common:
Method parameters:
- IllegalArgumentException
Arrays:
- ArrayIndexOutOfBoundsException
- NullPointerException
Collections:
- NullPointerException
- ClassCastException (if not using diamond notation <>)
Type casting:
- ClassCastException
File IO (java.io package):
- IOException
- FileNotFoundException
- EOFException
- ParseException
Everybody:
- NullPointerException
- IllegalArgumentException (UNCHECKED EXCEPTION)
Serialization:
- java.io.ObjectStreamException
Threads:
- InterruptedException
- SecurityException
- IllegalThreadStateException
Custom Exceptions
Assorted Advanced Features
Generics
Generics involve designing a data structure or class that takes an object of arbitrary type.