Posts

Showing posts from January, 2020

The package-info.java file

Remember the javadocs are written within the class (or interface, or enum files). There are no files to document the package information (or package javadoc). This file (package-info.java) is precisely used for that. It (which must always be named as package-info.java) contains the information about the package under which it is created.  Apart from the javadoc it also serves as a placeholder for package level annotations . Instead of say deprecating the classes one by one, the annotation could be applied to this file, and the processor deprecates the entire package. This is the place where the annotation with @Target (ElementType. PACKAGE ) can be placed. In fact, the Annotation with ElementType. PACKAGE cannot be placed on any other file, except package-info.java. The file can just contain the package name and the annotation. A sample code for package-info.java is   below. The comments can be generated as Javadocs. Also notice the import statement below the packag...

Implicit method declaration in Java Interfaces

Ever noted that in the Comparator<T> interface we have two abstract methods: ·          static compare(T o1 , T o2 ); ·          boolean equals(Object obj ); Yet the interface is declared as a functional interface. We can create an instance of the Comparator<T> even without extending the method “ equals ”, because all classes the methods in the class Object   is declared implicitly in an interface, and the method “ equals ” is the member of the class “ Object ”. If you choose you can override it or else leave it. You would always have a concrete implementation of the method “ equals ”. The question now arises, why was it declared in the Comparator <T> interface in the first place? Logically speaking there was no need to do so. The only reason it is there is for documentation inside the javadoc so that you know that equals forms an integral part of the Comparator interf...

Java Functional Programming : 3. The java.util.function Library

Image
This is the multi part series on Java Functional Programming 3. The java.util.function Library In most cases you might not need to declare your own functional interface as Java gives you a standard set of interfaces to save your time. In case you’d like to work with a function of two parameters of the same type, you could probably use the interface java.util.function.BinaryOperator<T> for the same activity. (More on the package java.util.function is to follow). As a shortcut, you’d probably prefer to keep things simple without the need of an extra interface declaration. Lets look at the constructs below:        BinaryOperator<Integer> binAdd = ( a , b ) -> a + b ;        System. out .println( "Bin Add 2, 3 = " + binAdd .apply(2, 3)); The lambda expressions can be passed as a parameter into the methods. Let us define a method which doubles the value given by the lambda ex...