Don't hold back on the Java
Java - the programming language that most seem to despise with great passion. Java has been around for quite some time now, and I'm afraid it's probably here to stay. So let's focus on what it is now, what you can do with it, and a speedy overview of some popular Java libraries. Companies and Developers today are using Java to build complex systems for an enormous range of use cases. Take Spotify for example, they use Java extensively in their back-end development environment. Why did they make this choice? It's easier to tune for performance. The Java Virtual Machine (JVM) is fast, well documented and has a massive developer community actively contributing to enhance its capabilities. Now according to Stackoverflow 2017 results, Java is the third most widely used language in the development community. See for yourself.
Looking for a job? Or to make some extra money? Java development jobs are in high-demand (from an Australian's perspective), as it is a language that many companies have adopted in the past. There are numerous Java legacy systems still being maintained and used for integerational purposes by a wide variety of organisations, both in the public and private sectors. So yes, lots of Java jobs. Don't believe me? Have a look at your go-to job search engine, and search "Java Developer". Ok, I hope I have striked your curiosity. Now let's dive into some Java.
Setting up the environment
This process depends on the Operating System you are indeed using. I recommend downloading Java directly from Oracle's website. Also, you will need the Java Development Kit (JDK), you can find that here. So next up will need to download a shiny new program to start writing your Java code. IntelliJ, Eclipse and Netbeans are the popular ones. Personally, I use IntelliJ and find it requires very minimal effort setting up plugins and hotkeys to make your development process more streamlined.
Java beginners, listen closely
Firstly we will write a Java program that simply prints a String to the output window of your development environment. The steps to do this will be:
- Create a Java Class (Call it anything)
- Define the main function (ie.
public static void main(String args))
- Print a String to standard output
- Run the program see what happens
Here is what the result should look:
Cool! So what happened? Our program was compiled to Java bytecode and then was run on the Java Virtual Machine (JVM) to execute the function
println and write the words to the output stream. Let's go exploring see what else we can find.
If you are unfamiliar with the concept of Objects or Object-oriented programming, I highly suggest spending a mere 5 minutes reading this.
Exploring Java libraries
Let's take a journey through the popular standard Java libraries.
java.lang: The fundamental library of all class objects in Java. It holds for example, the 'Object' class. It holds the implementation to the classes required to actually use the Java language. It definitely is handy to understand how these classes are implemented, ie. when optimising your program for performance and security.
java.io: Here is something we can tinker with. The io package is regularly used for writing and reading files, or data streams in Java. Reader is an abstract class and acts an umbrella class for file reading implementations. We will use a BufferedReader/FileReader implementation, but feel free to scope out other read alternatives.
Let's first create a text file in top level of the project directory. Something like the below:
After creating the text file, we now want to read the file and output its contents to standard output. See if you can work this one out on your own first.
If you didn't get it, here is a solution:
java.util: The util library contains implementations of the commonly used data structures. Lists, HashMaps, Queues, Stacks etc. If you're unfamiliar with Data Structures and Algorithms I would suggest picking up a course in your spare time, it will pay you back tenfold. Here is one I suggest from Coursera.
java.math: Math - This is Java's standard math library that provides functionalities for most commonly used math equations in programming. For example, finding the
minof two numbers, or the
random()function for generated a random number between 0 and 1.0. Besides the common operators
*, there is some handy functions that get you out of a tight jam.
javafx: For building Java front-ends or client software. This package allows developers to use pre-built graphics and user interface components. There are many Java frameworks out there for developing client applications, each varying in the type of functionality they provide to the developer. Front-end developers go to town on these frameworks designing and building interactive experiences for the end user.
Good comments means Java Docs
Properly commenting code is so often overlooked when developing software and it is a damn shame. Much time has been wasted trying to read and understand what someone's (or your own) code is trying to achieve. A good rule of thumb is to picture yourself in the future, trying to read over the code you have written. Asking yourself, would I be able to easily read this?. Understandably, working in fast-paced environments often documentation is somewhat pushed to the side, but it's important to take the extra 2 seconds if you're writing something fairly complex to write in a short comment. It actually enables you to move faster in the long run.
Java have a documentation based system called
Javadocs. What does it do? It's basically just a tool that generates HTML files to view the comments that have been written. For example, say I have a simple add method, and I write a javadoc comment outlining the parameters and what the function is doing. Something like this...
Ok now say you want to view this comment from a web browser, because you just joined the team and want a topdown overview of the methods and what they do. Since I'm using Eclipse in this example, I must first generate the Javadoc in eclipse. Done easily by
Project > Generate Javadoc. Cool so now we have HTML files in Eclipse (This process is similar for most environments). To view the files,
Window > Show View > Javadoc. Here is the add function's Javadoc displayed below.
Great code is well tested. There are a range of test suites available for the Java programming language, such as JUnit, Mockito, JWalk and the rest... The best place to start in my opinion, would be writing simple JUnit test cases to get a feel for how unit testing works. Often, developers write their test cases first before actually implementing their application or system. This process is called Test-driven Development (TDD) and it works quite well in practice.
Here is a short example of a JUnit test on our previously defined
So what happened? we defined a test case to be
testSimpleAddition by specifying the
@test at the top of the test function. Then we created the Object where the add method is contained, which is simply called "Blog". As you can also see we checked against the expected result to what the actual result was. Test passed! Yew.
Java Virual Machine (JVM) - Under the hood
Hmm what happens when you click that
run button in your development environment i.e. Eclipse? The program is compiled into an intermediate language called Java byte code to be analyzed and executed by the Java Virtual Machine (JVM). It does this in the Java Runtime Environment (JRE), and has access to the core Java libraries. There are benefits to using an intermediate language as it usually serves the purpose of write once run anywhere. This makes it real easy to move your program around, letting it run on different operating systems (without having to adjust your Java code).
A great way to become familiar with the Java programming language is to develop an application in Java. Utilising concepts such as test-driven development can help you build quality software that is well tested and bulletproof. Another way to hone those Java skills is by solving coding challenges, and writing your solutions purely in Java.
I highly recommend also checking out some Java books, my favorite is Effective Java. Hope this information was helpful and til' next time!