Top Ten Untrodden Areas of Java

From its inception, the diverse feature of the Java API has grown into a multifaceted giant. It's almost impossible for one to tread every route Java has taken into consideration. There was a time when one could say "I know every aspect/library of the C/C++ language." No doubt, C/C++ has grown to a great extent now, but, it's still a child from the perspective of the diverse features and API library of Java. In this article, I shall try to get into ten (in my opinion) of those lesser treaded alleys of Java and give a little hint about what those are for, with some relevant examples.

1. Generics

Tthe Generics APIs were added to Java from version 1.5. Generics is basically a general type cast of a collection where we can recast into a specific object and tell the compiler what type of objects are permitted in each collection. The compiler then inserts the cast automatically. Because the compiler knows the cast now, it can give an error message if we try to insert a wrong type of object into the collection at compile time. This makes both our code safe and clean. Each generic type defines a raw type, such as List<E>. Here, E corresponds to a raw type that we can recast into, say, List<String>.

The following code is a bad idea because we accidentally added a date object into a list of strings. The compiler does not complain because it does not have a hint of the object cast in the list.

2. Threads

A thread simply defines the single execution life cycle of a program. In fact, even the simple "hello world" program in Java is a thread in its execution. Each thread has certain states where we can intervene programatically during execution. The states are new, when the program starts a thread; runnable, where we can intervene with await; and lock calls to a state called waiting. The waiting state regains a runnable state with unlock and signal calls. There is also another waiting state called timed waiting. Timed waiting regains a runnable state after a given time interval. The final state of a thread is terminated, where the thread completes its task. Now, there can be multiple threads at work synchronously; this is called multithreaded programming. In Java, there are two ways to create a thread: either implement the Runnable interface or extend the Thread class.

3. Concurrency

Threads allow multiple activities to proceed concurrently. Concurrent programming is the most logical outcome. In fact, the power of threads is grossly underestimated until you give it a vent through concurrency. Concurrent programming is much harder than single threaded programming because every mistake you commit here has a ripple effect. Also, failures can be very hard to reproduce. Additionally, programs and their design are also quite complicated. Even though you may not like it, you cannot evade the need of the hour. Processors are racing with multiple cores. To take advantage of these multi-core processors from the Java language point of view, concurrent programming is the only answer. There are lot of concepts involved with concurrent programming. I won't go into much detail for now, let's try an example of a Java concurrency lock.

4. Reflection

The core reflection features are from java.lang.reflect. Reflection provides the facility of programmatic access to loaded class information. That means, given a Class object, we can obtain information about the constructor, methods, and fields of the class represented by the Class instance. Remember, the drop-down list appears in your favorite IDE, providing information about the list of member functions and constructor whenever you type the dot(.) operator after any object. This feature is possible because the programmer who created the IDE has used the language's reflection features. Also, reflection allows one class to use another, even though the latter class may not exist when the former class was compiled.

5. Serialization

Serialization is a mechanism to encode an object as a bytestream. This bytestream then can be transmitted across different virtual machines or stored on a disk for later deserialization. Serialization is ideal for remote communication and object persistence. Serialization is like breaking an object into a stream of bytes. Deserializarion is its reverse; we can convert a stream of bytes into an object.

6. Regular Expression

With a regular expression, we can define a set of strings with the help of a sequence of characters and symbols. Regular expressions are particularly helpful in validating input and ensuring that data is in a particular format.

7. Enumeration

Enumeration was introduced in Java version 5.0. It is a special kind of class defined by the keyword enum and a type name. In a comma-separated enumeration constant, each represents a unique value such as the seasons of the year, the planets in the solar system, or the suits in a deck of playing cards. Before enumeration was introduced, common patterns of constants were declared as a group of named int constants. Enumeration in Java is quite powerful and can be used as follows:

8. Anonymous Inner Class

The anonymous inner class is a special form of the inner class; it has no name and generally is defined within a method declaration. Typically, this class can access only the instance variables and methods of the top-level class object that declared it. It also can access only the final local variables of the method. These types of classes have some special uses. Let's try an example to illustrate the idea where it may be used.

9. Garbage Collection

Every object we create uses various resources, such as memory. There should be some systematic way to give resources back to the system when they are no longer needed, to avoid resource leaks. It is the programmer's responsibility in C/C++ to reclaim dynamically allocated memory explicitly. But, in Java, the JVM performs automatic garbage collection. In fact, a thread of the garbage collector runs simultaneously with any program run in JVM to reclaim memory that is no longer needed and mark any object for garbage collection when there are no more references to that object. That's because every class in Java has the methods of the Object class and therefore contains the finalize method. This method is called by the garbage collector to perform termination housekeeping on an object just before the garbage collector reclaims the object's memory. Rarely, such a situation occurs where you may need to call the finalize method explicitly. Also, it is discouraged to overload or use the finalize method. There is a problem, though; one cannot be sure when garbage collector will execute. In fact, it may never execute before a program terminates. Let's try an example by explicitly calling the garbage collector to execute.

10. Bit Manipulation

For programmers who stoop to the bits and bytes level, Java provides extensive bit manipulation capabilities. System-level communication with the operating system, hardware, or networking software often require direct bit manipulation techniques. Java provides seven bitwise operators to work on bits and bytes. They are: bitwise AND(&), bitwise inclusive OR (|), bitwise exclusive OR (^), left shift (<<), signed right shift (>>), unsigned right shift (>>>), and bitwise complement (~). Java provides a class called BitSet that makes bit manipulation easy (refer to javadoc for more info on BitSet). Here is an example to demonstrate the Sieve of Erastosthenes with with the help of a BitSet class.

Conclusion

While enroute to some of these alleys of Java, I realized there are many things that need to be elaborated and many other concepts to touch upon. These are basic concepts any Java programmer must have experienced. Java has some amazing features deep within its rabbit hole. It surprises me every time I explore them. If you think there are more points to cover, do not forget to comment. I'll keep writing.

IT Solutions Builder
TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD

Which topic are you interested in?

Mobile

Security

Networks/IoT

Cloud

Data Storage

Applications

Development

IT Management

Other

What is your company size?

What is your job title?

What is your job function?

Searching our resource database to find your matches...

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date