Results for tag "java-8"

Java Tip of the Week #8 – Java 8 Default Methods

posted by Roberto Cortez on
tags:

This week Java Tip of the Week we continue to explore some of the new features introduced in Java 8. This time is about Default Methods.

Before Java 8, Interfaces were a closed contract, meaning that after you published an Interface, every implementation would have to implement every method present in the Interface. If you changed your mind later and wanted to add new methods, you couldn’t without breaking the compatibility. Implementations would need to add the missing code to the new methods. The Collections API is a good example. It was designed since the early versions of Java, but it was missing operations that would help every day developer.

With Java 8, we can now implement methods in the Interface directly. This is done via a Default Method with the default keyword. As the name says, we are providing a default implementation for the method. So, if any implementation does not implement the method, it would use the default implementation. This change allowed to expand and extend API’s behaviour without breaking compatibility.

Default Method Interface

Here is a sample:

Yes, this works!

Check out this video with some live coding examples:

Remember to follow my Youtube channel for faster updates!

Leave a comment if you enjoyed it, if not leave one as well!

Share with others!
  • Twitter
  • Facebook
  • LinkedIn
  • Google Plus
  • Reddit
  • Add to favorites
  • Email
  • RSS

Java Tip of the Week #5 – Java 8 Date Time API

posted by Roberto Cortez on

Java Tip of the Week - Java 8 Date TimeThis week Java Tip of the Week we are going to continue to look into some of the new features introduced in Java 8. It’s about Time! The new Date Time API.

And in good Time we got it. Have you ever coded a real application without using any kind of object to represent Date or Time? I don’t think so. All of us are obsessed with Time, so it makes sense that we get a first class support to perform all kinds of operations when coding something. This was not the case prior to Java 8.

Java Date Calendar

Before Java 8, working with Date and Time was not an easy task. The old java.util.Date is not thread safe, represents years as two digits, uses a zero based index for months and is mutable. A real mess!

It’s replacement java.util.Calendar, was not much better. Still a mutable class, also with a zero based index for months. Some say it was even more broken than java.util.Date.

Displaying the Date / Time was not exactly easy. You needed to use yet another object to perform the proper formatting with DateFormat.

Finally, I don’t even want to discuss all the trouble you had to work with Timezones.

Here are two of my two “favourite” java.util.Calendar methods:

Calendar add

If I want to calculate yesterday day, but I only have an add method available. What do I do? Add -1 of course. Makes perfect sense!

Calendar set

Set the Month to 20? That can’t possibly work, right? Wrong! java.util.Calendar will just convert this to a valid Month and move the year, meaning that the 20 Months is actually September of the next year. Confused? You should be.

Joda Time

You could fix most of these by using a widely popular Date Time library called Joda Time. Is still the way to go if you can’t use Java 8 yet.

A good thing is that the main developer and creator of Joda Time, Stephen Colebourne was also the spec lead for JSR-310: Date and Time API.

Java 8 Date Time API

Pre Java 8, we need to write this code to print yesterday day, with trunked seconds:

And now:

Check it out in my video:

Remember to follow my Youtube channel for faster updates!

Leave a comment if you enjoyed it, if not leave one as well!

Check some additional information about Java 8 Date Time in the Java Tutorial.

Share with others!
  • Twitter
  • Facebook
  • LinkedIn
  • Google Plus
  • Reddit
  • Add to favorites
  • Email
  • RSS

Java Tip of the Week #4 – Lambdas

posted by Roberto Cortez on

Java Tip of the Week #4 - LambdasThis week Java Tip of the Week is the sequel to last week Streams API episode.

Lambdas is definitely one of the most awaited features in Java. Quoting Mark Reinhold – Chief Architect of Java Platform Group, Oracle:


“Lambda is the single largest upgrade to the programming model. Ever. It’s larger even than Generics. It’s the first time since the beginning of Java that we’ve done a carefully coordinated co-evolution of the virtual machine, the language and the libraries, all together. Yet the result still feels like Java.”

Java Lambdas

Lambdas is what make the new Streams API really powerful. Nothing was stopping Java to come up with a behaviour based API to perform operations on a sequence of elements. But who wants to code like this:

When you can code like this:

Check it out in my video:

Remember to follow my Youtube channel for faster updates!

Leave a comment if you enjoyed it, if not leave one as well!

Share with others!
  • Twitter
  • Facebook
  • LinkedIn
  • Google Plus
  • Reddit
  • Add to favorites
  • Email
  • RSS

Java Tip of the Week #3 – Streams

posted by Roberto Cortez on

This week Java Tip of the Week is about Streams.

Java Streams

Streams was a much needed feature in Java. They revolutionize the way we perform operations in Collections. For examples, now you don’t need to perform multiple for each statements to filter, sort or print elements.

The Streams API ships with all kinds of operations, including the previously described ones, but also map, group, reduce and many others.

Since I’ve started using them, I find it very hard to write code in Java 7 again, without this excelente API. Check out this small video about it:

Remember to follow my Youtube channel for faster updates!

Leave a comment if you enjoyed it, if not leave one as well!

Share with others!
  • Twitter
  • Facebook
  • LinkedIn
  • Google Plus
  • Reddit
  • Add to favorites
  • Email
  • RSS