Using the Deprecated annotation

The @Deprecated annotation can be used to indicate elements which should no longer be used. Any program that uses an element which is marked as @Deprecated will produce a compiler warning. @Deprecated public void oldMethod() { ... } public void newMethod() { ... } In this examples, newMethod replaces oldMethod. However we do not want to remove oldMethod completely because keeping it will help maintain backwards compatibility with older versions of the program....

April 18, 2017 · 1 min · Rezha Julio

Diamond Operator in Java

Since Java 7 it’s not necessary to declare the type parameter twice while instantiating objects like Maps, Sets and Lists. Consider the following code: Map<String, List<String>> phoneBook = new HashMap<String, List<String>>(); The type parameter for HashMap in the right hand side of the expression seems redundant. This can be shortened using an empty “Diamond Operator” to give: Map<String, List<String>> phoneBook = new HashMap<>();

April 17, 2017 · 1 min · Rezha Julio

Altering format string output by changing a format specifier's argument_index

A format string is a string which can include one or more format specifiers. String hungry = "hungry"; String hippo = "hippo"; String s = String.format( "%s %s", hungry, hippo); Here, “%s %s” is a format string, and %s is a format specifier. The value of s is “hungry hippo”. Modify the order that the arguments appear in the format string by specifying an argument index in the format specifiers....

April 15, 2017 · 1 min · Rezha Julio

Future of Interface in Java 9

Interface is a very popular way to expose our APIs. Until Java 7 it was getting used as a contract (abstract method) where a child class was obliged to implement the contract; but after Java 8, Interface got more power — to where we can have static and default methods. In Java 9, Interface is getting more power, to the point where we can define private methods as well. Let us understand why we need private methods in Interfaces....

April 13, 2017 · 4 min · Rezha Julio

More about Interface in Java 8

Interface was meant to define a contract before Java 8, where we were able to define the methods a class needed to implement if binding himself with the interface. Interface was only involved with abstract methods and constants. But in Java 8, Interface has become much more, and now it can have methods defined using static or default. Remember that default methods can be overridden, while static methods cannot. Method Definitions Interface was meant for good designers because when we create an Interface, we should know the possible contract that every class should implement....

April 12, 2017 · 4 min · Rezha Julio