Showing posts with label java 8. Show all posts
Showing posts with label java 8. Show all posts

Tuesday, October 7, 2014

Java 8 Overview: Lambdas

Probably one of the most requested features and biggest time saver in Java 8 are lambdas. Essentially, a lambda is a short hand way to create an anonymous inner class. Let's look at how we used to have to do an anonymous comparator for comparing the lower case result of strings in a list:
    Collections.sort(comparison, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.toLowerCase().compareTo(o2.toLowerCase());
        }
    });
Even with this simple example you can see quite a bit of extra code you have to write just to put in the logic of comparing two strings in lower case. Now, in Java 8 with lambdas you can compress the previous statement to the following:
Collections.sort(comparison, (o1, o2) -> o1.toLowerCase().compareTo(o2.toLowerCase()));
Much simpler right?

Lambda requirements

There is basically a single requirement for an interface to be used as a lambda expression. The interface needs to be annotated with the @FunctionalInterface annotation. This annotations requires a single non default, non static method in the interface. This way, with only a single method available it's obvious what method should be called and what the parameters and return type are for the method call. When it comes to creating lambdas there are essentially two types, what I call simple lambdas and standard lambdas.

Simple Lambdas

Simple lambdas consist of a single expression where the result of that expression is returned. They don't require braces and you can also omit the return keyword as well. The following is an example of what one would look like:
Collections.sort(comparison, (o1, o2) -> o1.toLowerCase().compareTo(o2.toLowerCase()));
In these cases even more of the code that is unnecessary is removed to make the result even cleaner.

Standard Lambdas

You would use a standard lambda when your implementation requires multiple statements. In these cases you would enclose the statements within braces and you need to include the return statement for all return points. If you were to compare a list of users based on their last name then first name then the expression would look something like the following:
        Collections.sort(comparison, (User o1, User o2) -> {
            int compare = o1.getLastName().compareTo(o2.getLastName());
            return compare != 0 ? compare : o1.getFirstName().compareTo(o2.getLastName());
        });
As you can see above, when items become more complex you have to specify the object types (User o1, User o2 instead of just o1, o2 like for the simple setup), you have to use braces and a return statement.

Extra information

To cut the code necessary for working with lambdas even further Java 8 includes a set of the most common types of operations you'd perform in the java.util.function package. In there you'll find operations such as Functions for processing one property and returning a result, predicates that take in an argument and return a boolean result, and a whole host of other standard operations. They're used heavily by streams (more on that in a future blog post) and can easily be integrated in your code base.

Conclusion

While lambdas don't introduce a new programming archetype in the language they do greatly simplify the code you need to write (and thus read as well). Like annotations before them they help to make the code more concise and easier for others to work with and understand. Finally, albeit to a lesser extent, it shows that Java can still move with the times and incorporate current programming paradigms to help coders in other languages to relate to the code.

Friday, August 15, 2014

Java 8 Overview: Exact numerical methods and negative floor/mod methods

Exact Numerical Methods

One of the lesser known features added to Java 8 is the addition of exact numerical methods to deal with MAX_VALUE/MIN_VALUE situations. Normally when you do arithmetic with integers or longs (floats and doubles work the same but you don't do much addition with them right? You use BigDecimal right? right?) and you go beyond their max value they just wrap around to MIN_VALUE and increase from there or decrease from MIN_VALUE to MAX_VALUE and decrease from there due to the bit shift of the first bit in a word (which designates a positive number from a negative number). The following code example and output shows how this works for int (long, float, and double work the same).
System.out.println("Max Value: " + Integer.MAX_VALUE);
System.out.println("Max Value + 1: " + (Integer.MAX_VALUE + 1));

try {
  System.out.println("This won't print: " + Math.addExact(Integer.MAX_VALUE, 1));
} catch (ArithmeticException e) {
  System.out.println("Exception since we overflowed Integer.MAX_VALUE");
}
Max Value: 2147483647
Max Value + 1: -2147483648
Exception since we overflowed Integer.MAX_VALUE

Negative Floor and Mod

The other arithmetic "fix" in this release deals with negative division and negative mod values. The way java is supposed to work is when you use the '/' symbol with integers and the result isn't exact then it's supposed to give you the first number less than the exact number. So if I were to do (4/3) then I'd get 1 since the actual answer is 1.33. With negative numbers this is off slightly. Instead of the next number down it gives you the next number up (heading for 0), basically the negative version of the positive result. Look at the following example.
System.out.println("Divide with negative numerator: " + -4 / 3);
System.out.println("Math.floorDiv() with negative numerator: " + Math.floorDiv(-4, 3));
Divide with negative numerator: -1
Math.floorDiv() with negative numerator: -2
In this example the old value is -1 (the mirror value for the positive result) but that isn't quite right, it's the next integer GREATER than the correct answer, not the next integer LESS THAN the answer so now with the new method you get the correct answer of -2.
Similar to the floor the mod value with negative numbers is also incorrect with the new floorDiv() logic. Now, MOD with negative numbers is already screwy as may languages define it differently but with the new setup you'd get the following:
System.out.println("Mod with negative numerator: " + -4 % 3);
System.out.println("Math.floorMod() with negative numerator: " + Math.floorMod(-4, 3));
Mod with negative numerator: -1
Math.floorMod() with negative numerator: 2
So, what's happening with the new setup? Well, if you were to do a floorDiv of the same numbers you'd get a -2 and -2 * 3 is -6 so the difference between that result and the original numerator is 2. The floorMod result is always the same sign as the denominator (so if instead of Math.floorMod(-4, 3) we used Math.floorMod(4, -3) then the result would be -2).

Conclusion

While I would imagine the new mod methods will have limited usefulness, however, the exact numerical methods for int and long I would find very useful. Currently well written programs have to constantly check to see if a summation (it will also work in reverse below MIN_VALUE) exceeds the bounds of the variable type. Now, you can check for ArithmaticException (or not as it's a runtime exception so it will bubble up nicely) instead and devote your program to what it's intending to do and leave the bounds checks to the system.

Wednesday, April 30, 2014

Java 8 Overview: Default and Static Interface Methods

Introduction

With the release of Java 8 I thought a series of posts on the new features of Java 8 would be appropriate. Probably the simplest of the new concepts is the idea of default and static methods for interfaces. Basically, what you can now do is define a default set of functionality you want for a given interface. Before, you'd have to define an interface and then immediately define an abstract class that implements the interface where you could put the default implementation (Swing event adapter classes anyone?) To use default methods you simply need to put the keyword default before the method signature and implement the method.
Another inclusion with Java 8 are static methods in interfaces. Like default methods, you can declare any number of static methods inside the interface as well.

Example

Source

public class Main {
    public interface ContainsDefault {
        String greeting1();
        default String greeting2() {
            return "My Second Greeting";
        }
        static String greeting3() {
            return "My Third Greeting";
        }
    }

    public static class Override implements ContainsDefault {
        public String greeting1() {
            return "Alternate Override1";
        }

        public String greeting2() {
            return "Alternate Override2";
        }
    }

    public static class NoOverride implements ContainsDefault {
        public String greeting1() {
            return "My First Greeting";
        }
    }

    public static void main(String[] args) {
        ContainsDefault noOverride = new NoOverride();
        ContainsDefault override = new Override();
        System.out.println("noOverride,greeting1: " + noOverride.greeting1());
        System.out.println("noOverride,greeting2: " + noOverride.greeting2());
        System.out.println("override,greeting1: " + override.greeting1());
        System.out.println("override,greeting2: " + override.greeting2());
        System.out.println("containsDefault,greeting3: " + ContainsDefault.greeting3());
    }
}

Results

noOverride,greeting1: My First Greeting
noOverride,greeting2: My Second Greeting
override,greeting1: Alternate Override1
override,greeting2: Alternate Override2
containsDefault,greeting3: My Third Greeting

Advantages

This has a few advantages:
  • Less classes (you only need the interface, not that plus another abstract class with the default implementations, such as swing event adapter classes)
  • Moving the default implementations to the interface allows classes that use it that need the default implementation to still extend from another class.
  • You can now add new methods to existing interfaces and if you add a default method you won't need to go to each implementation to implement the method, where appropriate

Concerns

There are a few things you need to look out for when using default and static methods:
  • since there is a default implementation, implementing classes aren't required to implement the method at all so you should be careful with putting in defaults for methods that are central to the operation of your system
  • You still cannot define variables, even if they're public (static and final variables are still OK; but since they're public the outside world can already access them anyway) so the usefulness of the methods are limited

Conclusion

While default methods can provide an easier path for setting up interfaces to make them more useful programmers need to be careful not to apply them indiscriminately. They can render default adapters, like the swing event adapters, irrelevant and free up classes to extend others instead of the adapter class; but they can also hide issues when used for core methods that don't really have a default. At the end of the day they're just another arrow in a programmer's quiver that, if used with skill, will make their programs more concise and understandable.