By Jefferson Santos from Unsplash

Java Keywords You Might Not Know

Christoph Krassnigg
4 min readAug 1, 2021

--

The Java language provides many different keywords. A keyword is a reserved word with a predefined meaning. Hence it cannot be used as a name for a variable, method, or class. However, there is one exception, and there are some rather unknown keywords covered in this article.

The exceptions

goto

If you are a Windows user or C/C++ developer, then you might encounter the “goto” keyword once. You can jump to a specific place in your batch script or C/C++ code with an identifier you define with goto.

Although the keyword makes code, in many cases, harder to read, it deserves to exist. Many developers still use it from time to time in their code, at least not in Java anymore.
In Java, “goto” is a keyword but without any functionality. Oracle removed it some time ago.

Java has no goto statement. Studies illustrated that goto is (mis)used more often than not simply “because it’s there”. Eliminating goto led to a simplification of the language — there are no rules about the effects of a goto into the middle of a for statement, for example. Studies on approximately 100,000 lines of C code determined that roughly 90 percent of the goto statements were used purely to obtain the effect of breaking out of nested loops. As mentioned above, multi-level break and continue remove most of the need for goto statements.

Goto is superfluous and provokes illegible, unmaintainable spaghetti code. This is one of the reasons why it is a reserved keyword in Java but no functionality is assigned to it–you should not use it anywhere today.

const

In Java, this is still a keyword, with which you were able to define constants. However, as the “goto” keyword is not in use anymore, developers should use the “final” keyword instead.

Oracle cannot implement the “const” keyword without breaking compatibility. It would be a nightmare if many Java programs would fail, only due to one single keyword.

Compatibility is a very important feature of the JDK. Arguably, the collection classes should be modified to indicate that the elements are const. That would require all existing implementations to be updated in the same way, effectively breaking all existing non-JDK implementations of the collection interfaces. Similarly, hashCode would have to be const, breaking the current implementation of String.”

Assertions

Java has functional assertions like the programming language Dart with the “assert” keyword, which is excellent for debugging.

Provide a boolean which will throw an AssertionError when the boolean is false.

Theoretically, you could catch that AssertionError, but there is a good reason not to do so. Errors in Java are something that should not be caught because they represent a critical failure of something.
Another example is that when your program runs out of memory, it should crash and tell the user something is wrong. So, never catch an error if it is not necessary for a program to work ultimately.

Assertions, in general, are a great way to guarantee that a program works, but in Java, they only work if you opt-in with the launch parameter “-ea” which stands for “enable assertions”. If you do not do so, then assertions will not work and will be ignored.

Synchronized

When writing a critical program, like making a transaction to a user’s account or counting views, your program might run into a problem, depending on how you implement certain things.

Imagine you get two incoming requests at the same time. For example, two users viewed a page simultaneously, and the backend will now update the count. Your method gets called and fetches the current view count, adds one to it, and stores the number in the database.
Because the requests come in simultaneously, both methods fetch the same view count, increment one and store the same amount in the database. In other words, one view got lost.

  1. Thread A: Retrieve views; got value 0
  2. Thread B: Retrieve views; got value 0
  3. Thread A: Increment retrieved value; the result is 1.
  4. Thread B: Increment retrieved value; the result is 1.
  5. Thread A: Store result in views; views is now 1.
  6. Thread B: Store result in views; views is now 1.

The “synchronized” keyword can prevent this by letting the method, which counts the view, not run multiple times at once. If a thread tries to run it, while another thread executes the method, the JRE will block the call until the other thread has finished executing.
For smaller pages, this is the proper solution until they get an enormous amount of views. Then, the “queue” can only work off one request at a time, which can be a weak point. Imagine getting millions of views per minute–the queue cannot comply.

transient

The “transient” keyword is used on fields in classes. It defines that a field does not belong to the object persistent’s state, hence should not be serialized. In other words, transient fields get ignored, e.g. using GSON.

If we take this class and convert it to JSON with GSON, then the field ignored will not be included in our JSON String.

To clarify things, persistent data is data with the ulterior motive to exist after the program has finished executing. For example, if you play a video game and create a save game, you wrote something to the hard drive. This data is persistent and will continue “living” after you close the game. You only care about certain things in your save game, e.g., player position, inventory, etc.
Debug information or short living data is not essential, so those fields are transient and won’t be saved.

Conclusion

This article covered the reasons, why goto and const do only exist as reserved keywords in Java. Besides that, you learned something about synchronized and transient and how to use them.

--

--

Christoph Krassnigg

Developer at block42. Student. Java fanatic. Loves to write about techy things.