Type Annotations: An added feature to annotations in JAVA 8

Earlier we could only use annotations in Java on declarations. With Java 8, annotations can be written on any use of a type in declarations, generics, and casts. Type annotations are not one of the highlighted features of Java 8. Annotations add more behavior to the piece of code we have written. So type annotation is just an add-on to that, in the sense, it boosts the productivity and ensures higher-quality for our code.

For example, if you want to ensure that a particular variable is never assigned to null. You would then modify your code to annotate that particular variable, indicating that it is never assigned to null. The declaration can be like this:

@NonNull String str;

When you compile the code the compiler prints a warning if it detects a defect, which allows you to modify the code if an error is found. After you correct the code to remove all warnings, this particular error will not occur when the program runs.

When we look for tools that make our...

Java 8: What’s new in it ?

Oracle launched a new version of java Jdk1.8 with a lot of features. Some of the important features are provided below.

1) Lambda

 JDK 1.8 allows you to create Lambda functions. Lambda functions will become a powerful concept once integrated with JAVA. Lambda refers to anonymous function in a programming language. Lambda function, generally known as Lambda expression, is a function but without a name. It is very much used in languages like Python and Ruby (which is borrowed from LISP) etc.  An anonymous function (lambda function) does not carry name, access specifier, access modifier, parameters etc. It is just to simplify the code. Lambda function is very convenient to use in the same place where we write a function. If you would like to use the function only once, lambda function is the more convenient way. It reduces typing a lot of code, because the function code is written directly where we use the function and the...

Object list sorting using BeanComparator

We can sort List<Objects> using BeanComparater instead of writing comparator. The beanutils.jar has to be imported. Default sort order is in ascending order.

For eg.
Collections.sort(postings, new BeanComparator(“resumeCount”));
BeanComparator bc = new BeanComparator(“resumeCount”);
Collections.sort(postings, bc);

Pros:- Concise, Minimal code

Cons:- Low performace, Uses reflection(Now if a field is renamed, the compiler won’t even report a problem)

Always override hashcode() if overriding equals()

In Java, equals() is implemented in Object class by default. This method is used to compare two objects. The default implementation just simply compares the memory addresses of the objects. You can override the default implementation of the equals() method defined in java.lang.Object. If you override the equals(), you MUST also override hashCode(). Otherwise, a violation of the general contract for Object.hashCode() will occur, which results unexpected behavior when your class is in conjunction with all hash-based collections.

This is a general contract in java programming that “whenever you override equals(), override hashcode() also”.

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information...