Java 8 New Features

1.Lamda Expressions
The point of lambda is to be able to pass some code to a method rather than objects. Until java 8, the way to achieve this in java would be to use callbacks. The issue with callbacks is that it leads to verbose code. Like you would write 5 lines of code when the real job is in 1 of these 5 lines.

we started with lambda expressions as this is probably the most sought after feature in the language after probably Generics/Annotations in Java 5.
Here’s the syntax:
1        (argtype arg...) -> { return some expression.. probably using these arguments }
What it does is that it reduces the code where it is obvious, such as in an anonymous innerclass. 

So, a thread can be changed as:
          Runnable oldRunner = new Runnable(){
              public void run(){
                  System.out.println("I am running");
              }
          };
          Runnable java8Runner = () ->{
              System.out.println("I am running");
          };
Similar to Scala, type inference is also possible in Lambdas. Consider the following available example:
1        Comparator c = (a, b) -> Integer.compare(a.length(), b.length());
Here, the types of a,b (In this case String, from the Comparator interface) are inferred as the compare method is implemented.
The symbol used to separate the block from arguments, -> is quite similar to => already used in Scala and if you are good at it, there is not much reason to switch as you will feel the way lambdas are implemented in java is inadequate(and verbose), but for a good ‘ol java programmer, this is the way to go.

2.Generic Type changes and improvements
Taking clues from Lambdas, generic collections can also infer the data types to be used to an extent. The methods for instance using a generic collection need not specify genric types. Hence, the following method
1  SomeClass.method();
Can be called simply ignoring the type information:
1   SomeClass.method();
The type can be inferred by the method signature, which is helpful in nested calls like
1  myCollection.sort().removeUseless().beautify();

3. Stream Collection Types (java.util.stream)
A stream is a iterator that allows a single run over the collection it is called on. Along with Lambdas, this is another noteworthy feature to watch out for. You can use streams to perform functional operations like filer or map/reduce over collections which can be streamed as individual elements using Stream objects. Streams can run sequentially or parallely as desired. The parallel mode makes use of fork/join framework and can leverage power of multiple cores.
Example:
          List guys = list.getStream.collect(Collectors.toList())
can also be implemented parallely as
          List guys = list.getStream.parallel().collect(Collectors.toList()
Another nice example that reduces the collection to a single item is by calling reduce algorithem.
          int sum = numberList.stream().reduce(0, (x, y) -> x+y);
or,
          int sum = numberList.stream().reduce(0, Integer::sum);

4. Functional Interfaces (java.util.function)

These interfaces contain some default methods which need not be implemented and can run directly from the interface. This helps with existing code – changing interfaces need not make all the classes implementing it implement new methods. This is similar to Traits in Scala and functional interfaces will be compatible with lambdas.

5.Date/Time changes (java.time)

The Date/Time API is moved to java.time package and Joda time format is followed. Another goodie is that most classes are Threadsafe and immutable.

6. Type Annotations

Now annotations can be used to decorate generic types itself.
Eg:
          List<@Nullable String>
which is not desired always, but can prove to be useful in certain circumstances. Apart from decorating Generic types, it can also be used in constructors and casting.
          new @NonEmpty @Readonly List(myNonEmptyStringSet)
          new @Interned MyObject()
           
          myString = (@NonNull String) myObject;
Even the array objects can be annoted:
          @NotNull String[] arr;

The inclusion of RuntimeVisibleTypeAnnotations and RuntimeInvisibleTypeAnnotations attributes which cause the .class file to save the annotation information.

No comments: