News

Java Annotated Monthly – August 2015

At 20 years old, Java remains the world’s most popular programming language this August, with nearly 10 million active developers. If accurate, this figure puts Java slightly ahead of Swedish. While only slightly easier to read, Java has a number of compelling features, including lambdas and streams, intersection types, multiple inheritance, and unrivaled tools and frameworks. If you haven’t seen Java lately, you haven’t seen what Java can do. Join us each month, and stretch your idea of modern Java development.

jam_pic

Java

When you think of Java development, you may be reminded of heavy libraries and runtimes, but this was a shortcoming of Java’s early class loading mechanism, which added a lot of unnecessary burden. Prior to modules, if you wanted to use a single package in a JAR, all of its dependencies became your dependencies – and their dependencies too. There was no way to establish the exact runtime requirements without downloading the internet or splitting yet-another-JAR. Throw in version conflicts, namespace collisions and soon you’ve got dependency hell.

With the introduction of Compact Profiles in Java 8, and the planned rollout of Project Jigsaw in Java 9, all this should soon become a thing of the past. Jigsaw introduces something called the Java Module System, a more granular alternative to the JAR format that provides metadata for the class loader to determine which parts of the dependency graph are actually needed at runtime. The JMS promises to deliver a number of popular features including a local repository for storing and retrieving modules on the same machine, Linux-oriented package management and support for installation and removal (a la RubyGems and NPM). If done well, this could have a huge benefit to the Java ecosystem.

Project Jigsaw is really coming in Java 9, and none too soon, for a project in development over the last six years. Given the scope of these changes and their intended timeline, JDK 9 must work carefully to balance repaying technical debt with feature enhancements, while maintaining two decades of backwards compatibility. Jigsaw will help ensure that Java 9 remains a versatile platform for future applications by introducing strategic changes to the Java runtime and dependency management. To stay up to date with the latest changes, subscribe to their mailing list.

JShell and REPL in Java 9 – This is a feature not traditionally associated with Java development, a Read–eval–print loop. In the spirit of command line friendliness, JShell provides a way to quickly evaluate and test Java statements with just a terminal. The JDK will offer this same functionality to IDEs and developer tools through the JShell API, making it easier to do certain things in IntelliJ IDEA, like execute Java scratch files.

JEP 259: Stack-Walking API is a new candidate for Java 9, enabling lazy stream-based access to Java stack traces. To date, there are 51 Java Enhancement proposals targeting JDK 9, and three candidates for acceptance, including JEP 225: Javadoc Search which will add a keyword search for Javadocs, and JEP 238: Multi-Release JAR Files, which will extend the JAR format to allow multiple class files compiled on different Java versions to occupy a single JAR. In the midst of JAR overhauling, it is not yet clear how this proposal will fit into the new JMS.

7 Days with Java 8 – With Java’s adoption at an all-time high, there’s never been a better time to start using Java 8 in production. A key feature in Java 8 is direct support for functional style programming, with the help of lambdas, streams and functional primitives like map(...) and reduce(...). These are designed to work well with existing OOP patterns, helping you to reduce boilerplate and improve readability. With a little help from Shekhar Gulati, you’ll be using lambdas in no time flat.

Kotlin

One of the most unique and ambitious projects we’ve tackled at JetBrains is a language called Kotlin. With Kotlin, you have the chance to see a language being designed from the inside out, interact with our developers, and contribute to its future. We’re excited to start using Kotlin in production, and look forward to hearing your feedback as we prepare for its release. Give Kotlin a try today and let us know what you think!

Modifiers vs. Annotations – One of the early language goals of Kotlin was to reduce boilerplate, and by taking what we’ve learned building parsers at JetBrains, we’ve been able to make a lot of progress on that front. For example, trailing semicolons are optional in Kotlin. Getters and setters are optional. Types are usually optional, if they can be inferred (ex. variable and return types). But sometimes a single character can make a huge difference in tooling efficiency, which is why we’ve decided to sacrifice unified metadata to achieve it. We’re interested in hearing your feedback.

Android + Kotlin = <3 – If you’re an Android developer interested in learning Kotlin, you’ll be pleased to know that Kotlin is totally compatible with Android. Since Kotlin compiles down to Java bytecode, you’ll be able to use the same familiar patterns and APIs that you’ve always tolerated, but never really loved. Get ready to fall back in love with Android development with true lambdas, built-in null safety, extension functions, and Anko, a library we’ve created to help you develop Android applications with pleasure. Thanks to Michael Sattler for his support!

Building APIs on the JVM Using Kotlin and Spark – Another area where Kotlin has seen significant adoption is web services. One of Kotlin’s early targets was JavaScript, and we’ve designed the language to work seamlessly in static and dynamically typed environments like JavaScript and NodeJS. If you’re determined to build web applications on the JVM, Spark (not to be confused with Apache Spark) is a tiny web framework for doing just that, and Travis Spencer will show you how to build APIs using Kotlin and Spark.

Spring

Frameworks are the bread and butter of an enterprise Java application, and learning a framework in Java is a bit like learning another language. Choosing a new framework for your business-critical application is not a choice to be made lightly, and one you will need to live with for years to come. In the beginning, there were two schools: Spring and Java EE. Life was simple. Now, there is Spring, Vaadin, GWT, Grails, Akka, Play – never mind reactive or actor frameworks. So each month we’ll focus on a new framework and see where that gets us.

Spring began its life as a dependency injection and AOP framework, later expanding into a number of related areas like security, data access, mobile and distributed computing. Traditionally considered a heavyweight choice, Spring evolved into a number of smaller, more agile projects, with significantly improved tooling in recent years. It also absorbed and maintains convenient abstractions for technologies, like ZooKeeper and Netflix OSS for doing things like service discovery with Spring Cloud, and composing various technologies with Spring Boot. But at its core is the same dependency injection framework you have grown to know and love.

Spring Framework 4.2 GA is the latest version of the Spring Framework, proper. It consists of several loosely related modules for dependency injection and aspect oriented programming. Spring Framework 4.2 includes a new annotation-based event listener, refinements to Java 8 support, integration with Hibernate 5.0, data binding for JSR-354: Money & Currency, support for CORS and much more. See here for a full list of changes in Spring Framework 4.2 and to learn more about Spring Framework, you may find the reference documentation surprisingly accessible.

Microservices with Spring are a more recent addition, designed to alleviate the complexity of monolithic applications, and it is only fitting that Spring should now have frameworks for building microservices (itself following a similar approach). Spring Boot is Spring’s lightweight solution for building modern web applications. Together with Spring Cloud, another framework for building distributed systems, you can bootstrap a distributed microservice architecture in days, rather than months. Or you could use Ratpack with Spring Boot for a more reactive kind of microservice.

Coming up in 2016: Spring Framework 4.3 & 5.0 – A somewhat unique feature of Spring Framework 4.0 was its compatibility with Java 6-8, revamping the APIs to use library features like Stream and Optional where available, and shipping vital updates to legacy Java users on the core framework. With their 4.* release cycle nearing completion, the next big step for Spring will be requiring Java 8 exclusively. Spring Framework 5.0 will bring all new support Java 9, and notably JEP 110: HTTP/2 Client. You can follow their issue tracker for the latest updates.

Android

Java and Android share a language, but that’s mostly where the comparison stops. Despite their initial resemblance, writing Java for the JVM is very different to Android development, which has its own lifecycle, usage guidelines and memory constraints. It is interesting how Android has evolved independently of the Java ecosystem, yet it represents Java’s only significant presence on consumer mobile devices. Likewise, you might wonder if that presence were merely incidental, as though it were just waiting for a suitable candidate to take its place.

Android Studio 1.3 Released – This is the one you’ve been waiting for. With fully featured C/C++ editing, now you can dig into Android NDK development with the support of powerful debugging and profiling tools to help diagnose bugs. Android Studio 1.3 ships a number of highly anticipated features including a heap dump viewer, allocation tracker, separate modules for APK tests, new annotations and inspections, and brand new support for data binding, a feature that lets you bind references to variables and handler methods directly inside the Layout XML.

Android Databinding: Goodbye presenter, hello ViewModel – Traditionally, Android has left the implementation of UI patterns up to developers. This has created some debate over Model-View-Controllers, Model-View-Presenters, Model-View-ViewModels and other three letter acronyms. The introduction of Android databinding removes the necessity of manually updating Views, and can lead to much simpler solutions. As Frode Nilsen mentions, Android’s approach to databinding in XML is a somewhat contentious technique, and is still far from feature-complete.

Exploring the new Android Permissions Model – Android M introduces granular permissions, a long-awaited feature for users. This change will have implications for permission-hungry applications, and in order to effectively utilize the new permissions model, Android applications must be able to handle missing permissions and gracefully recover from losing them at runtime. Ultimately, this makes the process a lot more complex, but as our luck would have it, Joe Birch breaks down the changes and shares a number of best practices for getting all the right permissions.

RxAndroid 1.0 – Reactive has quickly found applications in many programming domains in the span of a few years. If you’re wondering what Reactive is all about, Ben Christensen at Netflix has a great introduction on applying reactive programming to existing applications, and many of the same techniques apply to data centers on the cloud and mobile devices in your pocket. You can use plain RxJava on Android, but RxAndroid provides a few convenience classes to help simplify scheduling on Android, and is now stable.

Develop with pleasure!

image description