The aim of Java reference is to have a quick kinda cheatsheet on the various topics of Java, covering versions 9 and 11, its going to be a goto place to refresh your memory and to brush up if you don't use Java for a period of time, so think of it as a Java in a Nutshell.
Java has been around since 1991 and now has gone through many revisions, currently as I type this we are now on version 14 of the JDK, over the last few years major changes have taken place, firstly Java is being released (updated) more regularly (this can be a good and a bad thing) and secondly Java is now a licensed product. Java from a usage point of view is still used everywhere, mobile phones, washing machines, web applications, however it not commonly used for operating systems, drivers, computer hardware.
So why use Java compared to many of the other programming languages that you can choose from, Java has many features as per below:
Also many universities use Java as a programming language and has also become the default programming language, plus there are many Java programming jobs in all types of industries (finance, manufactoring, science, etc).
Quick overview of the Java programming language
Java is a fully Object Oriented Programming language which means that it supports the 4OOP principles
When combined it's produces a programming environment that supports the development of far more robust and scaleable programs.
A Java Environment consists of the following:
Java has to go through 5 phases into order to execute a java program
Editor | You create Java source code with a text editor such as vi (unix) or notepad (windows), the file will have a .java extension. you can use a IDE (integrated development Environment) such as JCreator, JEdit or eclipse. |
Compiler | You use the command javac to compile the source code, this checks the syntax and translates the source code into bytecode, the language understood by the java virtual interpreter. A file with a .class extension is created. |
Class Load | This phase loads the .class file into memory (the file can either be on disk or copied over a network) The .class file can be either a application program (run on your computer) or a applet (run within a web browser) |
Bytecode Verifier | This phase checks that the bytecode meets Java's security restrictions, it enforces very strict rules to protect data on your computer. |
Interpreter (Execute) | The last phase executes the bytecode one byte at a time, thus performing the action you asked when to created the source code. |
A simple Java look like below, as with other programming languages you have the option to pass arguments to the program
Example of a simple Java program | public class PrintArgs { public static void main(String[] args) { System.out.println("Number of args is: " + args.length); System.out.println("Printing some arguments in this code: "); // Loop through arguments passed and print them to standard output for (int i = 0; i < args.length; i++) { System.out.println("Argument " + (i + 1) + ": " + args[i]); } } } |
Compile and run a Java program from the command line
There are a common set of tools that you can use when developing in Java from running, compiling, packaging (Jar file), documentation, etc I provide a table below of the most commonly used:
Commonly used Commandline tools |
|
Running | java [--class-path, --classpath, -cp] java [-verbose:class] java [-verbose:gc] java Test # Run a java program without a package configured java Test arg1 arg2 arg3 # Run a java program without a package configured and pass 3 arguments java uk.co.datadisk.Test # Run a java program with a package of uk.co.datadisk, remember you are running from the root directory uk/co/datadisk, dont from from inside uk/co/datadisk the same directory as the class java uk.co.datadisk.Test arg1 arg2 arg3 # Run a java program with a package and pass 3 arguments |
Compiling | javac -d # set the destination output directory of the class file javac -gc # generates all debugging info javac -g:[lines, vars, source] # generates specific debugging info javac -verbose # outputs compiler info |
Packaging | jar [-c, --create] # create a JAR archive file jar -C # changes the specified directory and includes the files at the end of the commandline jar [-f, --file] # specifies the name of the JAR file jar [-m, --manifest] # includes the manifest file information |
Other useful tools | javap -c # prints disassembled code javadoc # generate API documentation in HTML format jlink # assemble and optimize a set of modules and their dependencies into a custom runtime image jmod # create JMOD files and list the content of an existing JMOD file jdeps # Java class dependency analyzer jdeprscan # static analysis tool that scans a jar file |
There are many Java Frameworks available to you, Frameworks are large bodies of pre-written code to which you add your own code in order to solve a problem. You make use of a framework by calling its methods, inheritance, and supplying callbacks, listeners, or other implementations of the patterns. A framework will often dictate the structure of an application. Some frameworks even supply so much code that you have to do very little to write your application. This can be good or bad, depending on how easy it is to use. Frameworks are the substance of programming. You build on top of a good one, your program is solid and fast and comes together beautifully. You build on top of a bad one, your life is miserable, brutish, and short.
Common Java frameworks that are available are below: