A Few of My Favorite Things (About Java)

Rick Moore
Nerd For Tech
Published in
6 min readSep 5, 2021

--

Photo by Carli Jeen on Unsplash

Although Java just turned 25, old enough to rent a car in the US, it still remains one of the most popular coding languages in the world. It has remained innovative, relevant and robust. In the 2021 Stack Overflow Developer Survey, Java still ranked #5 worldwide among the most used programming languages, with over 35% of participants responding that they regularly use it and will likely use it over the next year.

Why Java?

When used alongside web frameworks like Spring, JavaServer Faces, Google Web Kit or Apache Struts, Java is a rugged language for creating powerful web applications.

While it’s not the only option for Android development, Java Micro Edition is the framework that opens Java to the continuously expanding mobile market. Java is also compatible with Kotlin and Android Studio, keeping it on the cutting edge of the mobile universe. The fact that wildly used mobile apps like Twitter, Spotify, and CashApp (among many more) are still built using Java is a testament to its continued relevance.

Java was actually designed for embedded systems, things like SIM cards, Blu-ray discs, utility meters and televisions. Many of the systems we interact with daily rely solely on Java code.

Java has a hand in Big Data technology, Gaming Systems, platforms that provide Infrastructure-as-a-service(IaaS), Platform-as-a-service(PaaS), and Software-as-a-service(SaaS), and debugging/software tools.

It’s clear that Java is going nowhere for a very long time, and as Oracle continues supporting the language, it will maintain its gargantuan status in the development world.

Coming from a JavaScript/TypeScript background, much of the syntax in Java is very familiar, however, there are many mechanics unique to Java that set it apart in functionality and developer experience. Here are a few of my favorites!

True Object Oriented Design

Java treats everything outside of its 8 primitive data types like an object, and all methods and variables are declared inside of classes. In fact you cannot create a program in Java without declaring a class to start. This brings a number of benefits to the code.

  • Well organized and easy to read codebases.
  • Encapsulation allows for easy maintenance and debugging.
  • Code is reusable and modular.

Classes have a familiar, yet distinct syntax. Notice that Java is also strongly typed, so we always need to declare our data types of variables and return data.

A lot of this looks similar to how we create classes in JavaScript, but notice a few things.

  • We can always declare a variable or method to be private or public. This allows us to decide what information is accessible outside the scope of this class.
  • We need to declare the type of the variables upon declaration. Our accountNumber and balance will be an integer or int (which is one of the Java primitive types) but the the owner name will be a string. Also notice the String type is capitalized. This is because “String” is not one of the Java primitive types, it’s actually an object.
  • On our methods, we need to declare the type of data that will be returned when the method is used. If we declare void, this implies that the method returns nothing.

This methodology of structuring code into classes forces us to make decisions early on about the architecture of the application, and ultimately creates code that is easier to understand and debug later in its lifecycle.

Multi-threading

Those of us with a JavaScript background, or experience in other single-threaded languages will immediately see the benefits in this feature. Java allows for multiple processes to use available resources and execute at the same point in time. You can extend the built in Java class Thread, to see first hand which processing thread is handling each task.

Essentially, this code is extending the Thread class, which begins its lifecycle at the run() method. We then run a loop (from the Multithread class) that creates new instances of the ThreadReader class, and invokes the run() method using the start() method (which is built into the Thread class).

The output shows that each time the loop runs, a different processing thread is taking that particular task and handling it independently of the other threads. As processors get faster and faster, the benefit of multi-threaded processing is massive, making Java a fast and reliable choice.

Platform Independence

Java employs a virtual machine called the Java Virtual Machine (JVM) that acts as a runtime environment for your Java code. This machine is malleable and knows how to interact with different hardware and software profiles you may be using. When Java code is compiled, it becomes byte code, which the JVM can understand, then it takes care of creating machine code from that byte-code for the computer to execute.

This foresight allows for users to write their Java code in one place, but allow it to run on whatever platform they like: Windows, MacOS, Linux etc…

Platform Dependency

This doesn’t necessarily mean that you can run Java code anywhere, but as long as the JVM is available to interpret the compiled byte code and run it using the local machine language, then your Java program can run on that machine.

Cross platform compatibility is a huge selling point that contributes to Java’s popularity.

Simplicity and Syntax

Compared to its other popular object oriented competitors, Java is one of the simpler languages for developers to wrap their heads around. It’s clean and compact, with specific formatting rules that make it easy to quickly get familiar with a large code base.

It also has several cool pieces of syntactic sugar that I really like.

  1. Number literals can use the “_” underscore without affecting the number. For Example:
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;

2. There are 8 different primitive data types. Knowing which one to use can save you a lot of precious memory.

Primitive Types

We have a very clear correlation between the space requirements of our code and the types of data required. If you are certain that you will never need a value higher than 127, for many values in an array, for example, you could store the variables as bytes instead of ints and save 26 bits of memory for every value.

Though there are baseline standards that Java developers generally stick to except in special cases, being aware of this is crucial to understanding what’s going on under the hood.

3. Bitwise Operators

In addition to the operators we are familiar with from most languages, we get a few special ones in Java that can be used on the individual bits of the data.

  • & Bitwise AND operator: returns bit by bit AND of input values.
  • | Bitwise OR operator: returns bit by bit OR of input values.
  • ^ Bitwise XOR operator: returns bit by bit XOR of input values.
  • ~ Bitwise Complement Operator: This is a unary operator which returns the one’s compliment representation of the input value, i.e. with all bits inversed.
  • << Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as of multiplying the number with some power of two.
  • >> Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit depends on the sign of initial number. Similar effect as of dividing the number with some power of two.
  • >>> Unsigned Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit is set to 0.

Conclusion

Java is a robust and powerful coding language. It would serve any developer to know the basics and wide breadth of possibilities that this mature and widespread language brings. Thank you for reading and happy coding!

--

--

Rick Moore
Nerd For Tech

Software Engineer at Thrive TRM. Specializing in Ruby on Rails, React/Redux and JavaScript.