Hidden Gems In Java

Features of Java that I had no idea even existed

Sanjam Singh
May 7 · 7 min read
Image from unsplash

As programming languages build-up, hidden features must appear and frames that were never come up with by the developers crawl into the reasonable application. A quantity of these features side with their head as figures of expression and become acknowledged discussion in the language, while opportunities develop into enemies of illustrations and are turned over to the dull corners of the language resident field.

For every one of these mysteries, note that some of them, as features in numerals and put auto-boxing might be useful in functions, however, opportunities for illustration, multiple classes in a Java file, have been consigned to the sideline is as it should be in light of the experience that a part is applicable in the language doesn’t mean it ought to be used.

At the moment when a specific class stacked into the JVM, all the class factors will involve default upsides of their sort. This is because the compiler allocates default esteems to all the part factors of a class in the arranged time.

I called to move over ten unique features, which I look at as being hidden gems useful to be familiar with now.

Underscores Can Be Used in Numerals

Whether in computing, massive numbers can be extremely complex to study. In this case, working to figure out that 1183548876845 is “1 trillion 183 billion 548 million 876 thousand 845” can be exhausted. English calculations consist of the comma delimiter, which provides for three digits at a chance to be organized.

While these suffices to carry out our objective of publishing our massive number the stability we found is requiring. since JDK, Java allows established a corresponding to the comma delimiter: the underscore. Underscores can be applied in the same manner as commas, breaking up groups of numbers to build up the readability of massive values.

While this approach for breaking up numbers should not be overused, it should be controlled particularly as a comma in English mathematics or divided groups of three digitals after the total deposit in a floating-point quality it can support determine incomprehensible numbers.

Overflow Operations

A lot of techniques should have been a separation of the basic library from the period of time. A favourite amusement of the field is to unscramble numeric overflows when ints exceed 2³² and establish the toughest and most irregular of bugs.

Already, no bright ammunition here, but an array of functions to act on numbers that start when overflow in a fewer merciful condition than your ideal +/ * operator, which essentially overflows. If it was up to me, I’d have this be the delinquency form for the JVM, with certain functions that support arithmetic overflow.

Annotation Implementation

Since JDK, annotations have an essential pulling out of many Java applications and frameworks. In a great bulk of instances, annotations refer to language establishes, such as classes, fields, methods, and so on but there is another process in which annotations can be given:

As implementable interfaces. Implement the annotationType process and come back the type of the annotation as well, since this is pulling out of the Annotation interface.

While our annotation and interface are exact, with a very remarkable impression, there appears not come out to be a process to combine these two forms. Although in every instance, completing an annotation is not a reliable method as a choice, it can be appropriate in a privileged few cases, such as within annotation-driven frameworks.


There are enough dialects to integrate a multi-stranded approach to a system. One of the most esteemed ones is ReadWriteLock and its related implementations. This idiom reduces ideas by providing various strings to expand a system while only stopping for strings that influence it. Sounds impressive in position, but in existence, this lock is limited, specifically with a significant amount of correspondent threads.

This became so dangerous that Java 8 is setting up a brand different RWLock called StampedLock. Not particularly is this lock quicker, but it likewise provides an impressive API for optimistic locking, where you can look at a reader lock at an extremely moderate yield, assuming that no address use that takes place during the significant part.

At the result of the part, you interrogate the lock to determine whether a note experience developed during that moment, in which event you can choose whether to retry, rise the lock or offer.

Image from pixabay

Class Initialization

When a specific class loaded into the JVM, all the class variables will contain default qualities of their type. This is because the connoisseur determines default qualities to all the representative variables of a class in the assemble stage.

A runtime handling of class initialization will be represented by the JVM for any class which is loaded into the memory by a ClassLoader. JVM will not pass the class initialization deal after the class loading. However, it will appear before the initial instance of the succeeds.

  • Establishment of an object from that class
  • Calling a static method or working/developing a static variable
  • Execution of an assertion statement that is nested within a top-level class

We can load static variables in a class by specifying declaration purposes. Likewise, some of you may be determined about Static Initializer Blocks, which are still controlled for logging in static variables. Java class initialization handling concerns the accompanying steps.

  • Assign declaration amounts to static variables
  • Execute Static Initializer Blocks

Instance Initialization

With most OOP languages, objects are completely instantiated using a constructor. When a superclass is set up, the conduct of execution varies somewhat in order of their declaration:

  1. Static initializers of the superclass
  2. Static initializers of subclass
  3. Instance initializers of the superclass
  4. Constructor of superclass
  5. Instance initializers of subclass
  6. Constructor of subclass

Thus, when we choose to load an object, we develop the initialization logic into the constructor of the object. While it may come out to be a valid inference that all of the initialization logic is formed in the constructor or set of constructors for a class. Instance initializers are determined by including initialization logic within a collection of prepares within the interpretation of a class. When the object is instantiated, its occurrence initializers are called initial, supported by its constructors. Apart from particular initializers, we can still set up static initializers, which are performed when the class is carried into consciousness. When all three initialization procedures are performing in a class, static initializers are regularly performed initially in the charge they are maintained.

Optional Values

Java 8 includes a different template called Optional for wrapping recommendations that may be null. It’s by no processes a bright shot to end nulls, but better techniques for an API designer to stand for at the system status that a null value may be reported or taken place to a program, and the caller should provide for it.

As such, this will simply carry out for other APIs, assuming callers do not let the character escape the case where it can be unsafely dereferenced.

I have to express I’m beautifully mixed about this feature. Whereas, nulls go on a great issue, so I understand everything worked out on that presence. This is because operating Optional requires maintaining community-extensive production, and with limited direct benefit.

Image from unsplash

Annotate Anything

Java language is that annotations can now be included to essentially lot in your code. Previously, annotations could simply be continued to objects like class or program declarations. With Java 8 annotations can be included to variable and parameter declarations when dropping a value to a different type, or indeed when setting aside a different object.

This is pulling out of an integrated production to carry out the language better sympathetic towards static analysis and apparatus tools, its absolute value will depend on what the public goes on with it.

Class Unloading

The classes which are carried into the vision could be got rid of class unloading is an optimization handling done by JVM to optimize the memory management.

This is because, indeed if there are no modern recommendations, it might be referred to by a class that is probably carried by the loader. Likewise, if a class anyway unloaded from memory while it’s setting ClassLoader potentially attainable, the class may be reloaded in perspective, if the loader loads another class which includes the recommendation to the declared by class. The initial bootstrap class loader is permanently obtainable throughout the JVM execution.

Process Termination

isAlive(), lets you simply test if the deal with is even up without admitting to stand by for it. The assisted and better compelling one is destroyForcibly() which lets you effectively execute a process that involves timed-out or is no later significant.

Introducing an extraneous process is one of those parts you make half-learning you’ll show up back to debug it when the deal with impacts, hangs or spends 100% CPU usage. The Process class now becomes set up with two different programs to support your select command of impulsive processes.


Medium’s largest Java publication, followed by 11300+ programmers. Follow to join our community.

Sanjam Singh

Written by

Cloud, DevOps and Web Developer | Machine Learning/ Artificial Intelligence | Blogger | Freelancer https://imsanjams.tk/


A humble place to learn Java and Programming better.

Sanjam Singh

Written by

Cloud, DevOps and Web Developer | Machine Learning/ Artificial Intelligence | Blogger | Freelancer https://imsanjams.tk/


A humble place to learn Java and Programming better.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store