The State of Java

I think living in a beautiful city in a fantastic climate has its advantages. Not just the obvious ones, but we find people unusually keen to come and visit us on the pretence of presenting at the Sevilla Java User Group (and please, DO come and present at our JUG, we love visitors).

This week we were really lucky, we had Georges Saab and Aurelio Garcia-Ribeyro giving us an update on where Java is now and where it looks like it’s going in the future.

I’m just starting to use Java 8 in real life, so this could not have been better timed - I got to ask the guys a bunch of questions about the intentions behind some of the Java 8 features, and the current vision for the future.

2015 Java update and roadmap, JUG sevilla from Georges Saab and Aurelio Garcia-Ribeyro

My notes from the session:

  • Lambdas could be just a syntax change, but they could be more than that - they could impact the language, the libraries and the JVM. They could have a positive impact on performance, and this work can continue to go on through small updates to Java that don’t impact the syntax.
  • Streams are a pipeline of operations, made possible/easier/more readable by lambdas. The aim is to make operations on collections easier and more readable.
  • In the Old World Order, you had to care about how to perform certain operations. With streams, you don’t need to tell the computer exactly how it’s done, you can simply say what operations you want performed. This makes it easier for developers
  • Streams will take all the operations you pass in and perform them in a single pass of the data, so you don’t have to write multiple loops to perform multiple operations on the same data structure, or tie your brain in knots figuring out how to do it in one loop. There are also no intermediate data structures when you use streams.
  • The implementation can be optimised under the covers (e.g. not performing the sort operation if the data is already ordered correctly), and the developer doesn’t have to worry about it. Java can introduce further optimisations in later releases without changing the API or impacting the code a developer has already written.
  • These new features in Java have a focus on readability, since code is much more often read than written.
  • The operations are easier to parallelise, because the developer is no longer dictating the how - multiple for loops might not be easy to parallelise, but a series of operations can be.
  • Default methods and new support for static methods on interfaces are interesting. I’d forgotten you could put static methods on interfaces and I’m going to sneak them into my latest project.
  • Nashorn is here to replace Rhino. Personally I haven’t worked in the sort of environment that would need server-side JavaScript so this whole area has passed me by somewhat, but seems it might be interesting for Node.js or creating a REPL in JavaScript that you want to run on the JVM.
  • The additional annotation support in Java 8 will be useful for Java EE. As this is something I’m currently playing with (specifically web sockets) I’m interested in this, but it seems like it will be a while before this filters into the Java EE that’s used on a daily basis.
  • Mission Control and Flight Recorder - look interesting. Feel like I should play with them.
  • Many people are skipping straight from Java 6 to 8 - the new language features and improved performance are major driving factors
  • End of public updates of Java 7 in April. Having libraries that, um… encourage… adoption of the latest version of Java makes life a lot easier for those who develop the Java language, as they can concentrate on moving the language forward and not be tied down supporting old versions.
  • Either this is the first time I’ve heard of JavaDB, or my memory has completely discarded it. I had no idea what it was.
  • Java 9 is well under way, check out the JEPs.
  • (This is the JEP process)
  • Jigsaw was explained, and actually I could see myself using it for the project I’m working on right now. I had a look to see if I could use it via the OpenJDK, but it looks like the groundwork is there, but not the actual modules themselves.
  • The G1 Garbage Collector is “…the go forward GC”, it’s the one that’s being actively worked on.
  • This is the first I’ve heard of Enhanced Volatiles, I’m so behind the times!
  • Access to internal packages is going away in Java 9. So don’t use any sun.* packages. Use jdeps to identify any dependencies in your code that need to change.
  • …and, looking further ahead than Java 9, we have value types and Project Valhalla
  • …a REPL for Java
  • …possibly a lightweight JSON API
  • …and VarHandles were also mentioned.
  • Finally, the guys mentioned a talk by Brian Goetz called “Stewardship: the Sobering Parts”, which has gone onto my to-watch list.

Ideas

It became clear throughout the talk there are plenty of ideas that we could explore in later presentations. If you want to see any of the following, add a comment or ping me or IsraKaos on twitter or Meetup and we’ll try and schedule it.

Similarly, if you can present on any of these topics and want to come to a beautiful, sunny city with amazing food to do so, drop me a line:

  • The OpenJDK
  • The JCP, the purpose, the processes, the people
  • Adopt a JSR, Adopt OpenJDK
  • New Date/Time (JSR310)
  • JavaFX
  • Code optimisation vs Data optimisation (I honestly don’t know what this means, but I wrote it down in my notes)
  • Java EE

Further Reading

Workshop: AngularJS, HTML5, Groovy, Java and MongoDB

Setup required for the AngularJS/HTML5/Groovy/Java/MongoDB tutorial for QCon London 2015. Sign up with code SPEAKGEE100 for £100 off the price!

Who is this for?

This workshop is aimed at Java developers who want to learn more about how to rapidly prototype applications using web front-end technologies with JVM back-end technologies. The workshop may be suitable for developers who don’t normally use Java as their primary language, but this is not an introduction to Java and familiarity with server-side development of some kind is assumed.

A basic understanding of HTML and JavaScript is expected, but previous experience of Groovy or MongoDB is not required.

The workshop is not suitable for people who have no programming experience.

This is a hands-on workshop, bring a laptop set up according to the following instructions - you will be writing code!

Overview

I’ve created my very first screencast! This video takes you through all the steps you need to go through in order to be setup for the workshop.

The setup instructions below are the same process, so you can copy & paste where appropriate.

Setup

I am assuming you already have a version of the Java SDK downloaded. For this workshop, Java 7 or above should work, but I recommend you download the latest version of Java 8 if you’re not already using it.

Create a new work area for this tutorial. For the rest of these instructions I’ll refer to it as <location>. I’ve put mine in ~/Projects/workshops/qcon2015

Installing MongoDB

I’m going to be using MongoDB 3.0.0-rc8, but version 2.6 will be fine too.

Download MongoDB for your operating system and unzip/install it. Unless you prefer to put it somewhere else, I suggest you unzip it to <location>/mongodb

Now we have to create the directory for the data to go into. Create a data directory in <location>, so that you have a folder at <location>/data

From the command line, start MongoDB to make sure everything is working. From the <location> directory:

Linux/OSX
./mongodb/bin/mongod --dbpath data 
Windows:
monogdbbinmongod.exe --dbpath data 

MongoDB should be running on localhost and port 27017

Connect to the shell to make sure it’s working:

Linux/OSX
./mongodb/bin/mongo 
Windows
monogdbbinmongo.exe 

You should see something like:

~Projectsworkshopsqcon2015>monogdbbinmongo.exe MongoDB shell version: 2.8.0-rc5 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see         http://docs.mongodb.org/ Questions? Try the support group         http://groups.google.com/group/mongodb-user > 

Creating your project

Clone the Cafelito project into <location>/cafelito

In a terminal/command line, navigate to this location, and run:

Linux/OSX
./gradlew check 
Windows
gradlew.bat check 

This should download all the required dependencies.

Open in your favourite IDE and import this directory as a new project. I’ll be using IntelliJ IDEA 14 and showing a number of its tricks. If you’re using IntelliJ IDEA and you import this project as a Gradle project, you don’t even need to do the above step, IntelliJ will download and import the dependencies for you.

Feel free to use Eclipse or Netbeans (or whatever your favourite IDE is) but I’m out of practice using those IDEs and might not be able to help you out as much.

And now…

You should be ready to begin: you have a working instance of MongoDB, a skeleton project that we’ll be working with, and all the dependencies should have been downloaded.

New IntelliJ Tricks I Learnt Part 2

Apparently, I’m all about Continuous Learning - after I wrote the last post about IntelliJ Tips, I carried on through my trawl of the documentation and, of course, discovered a load more things I didn’t know. And this is all just in the Quick Start section!

So here’s the next batch (although once again I’m not sure how many are only applicable to IntelliJ 14). All shortcuts are for Windows unless otherwise stated, and Mac users should be able to replace the Ctrl key with Cmd in all these cases.

Navigate between methods

Now I knew you could use the structure thingie (Ctrl+F12) to let you navigate to methods in your class (although I never actually use it, it somehow never sticks in my head). What I did not know is that you can jump between methods using Alt+Up/Down arrows. This strikes me as being super useful, especially to browse tests.

Refactoring String Fragments

I’ve wanted this feature so many times, but could never find it or get it to work - did you know you can refactor out parts of a hardcoded string into variable/field/constant? Select the section of the String you want to extract, and use the normal “Extract…” shortcuts - e.g. Ctrl+Alt+V to extract it into a variable.

I’m sure I’ve tried this before and it didn’t work. But I’ve tested it and it does!

Invert Boolean

There doesn’t seem to be a keyboard shortcut mapped by default, but using “Refactor -> Invert Boolean…” (or using our newly discovered “Navigate to Action” shortcut of Shift+Ctrl+A and typing “Invert”) you can tell IntelliJ you want a boolean field to mean the opposite thing, and it will flip the meaning everywhere. For example, you might have an saved flag, and you now want it to mean notSaved - IntelliJ will make sure the isSaved method returns !notSaved.

Magic Refactor Suggestions

You don’t always know exactly what refactoring operation you want to perform, and IntelliJ is ready to help - it can suggest refactorings available in the current context using the catchy magic incantation Ctrl+Shift+Alt+T. Seems useful, but I’m pretty sure I’m not going to remember that one.

Highlight implemented/overridden methods

I love everything about IntelliJ’s ability to find usages of things - finding where methods are called, finding implementations of a method, highlighting usages of a field, method or variable… What I didn’t know, is that you can highlight the methods that implement or override a specific interface or superclass. For some people, this might not add much, as your little “I override something” icon

"Overrides

combined with the @Override annotation, might tell you all you need to know. But that only really works if you’re implementing a a single interface. If you’ve correctly separated your concerns, you might implement several interfaces, and want to know which are the methods from a particular one. That’s where Shift+Ctrl+F7 on the “extends” keyword comes in handy.

Run individual inspection by name

I use inspections quite a lot. They’re especially useful if you have a codebase that doesn’t quite conform to your ideal standards, so you can’t yet turn on automated checks like checkstyle to break the build when the code doesn’t conform - there’s an initial step where first you need to clean up the code. Inspections are great, because you can have IntelliJ give you little yellow marks to show where things are suboptimal, and aim to check in only files without little yellow marks.

What I didn’t know, is that you can run a single inspection on files in any scope. So if, for example, you wanted to make sure that the code you’ve touched in this change has no unused bits, like unused parameters or unused/untested methods, you could press Shift+Alt+Ctrl+I and type “unused”. Select the “Unused Declaration” inspection and choose to run this on all uncommitted files. This will tell you where you can clean up your code, and every commit should make your codebase that little bit better.

Saving Power With Highlighting

I did not know you could easily alter the active errors/warnings, and use that to save power. Quite Interesting.

Auto indent your lines

I almost obsessively reformat my code as I go, pressing Ctrl+Alt+L almost like a muscle twitch. What I’ve just discovered, though, is that there’s a shortcut which will just fix the line indentation, which is probably 80% of what I need anyway - Ctrl+Alt+I.

Fix Evil String Concatenation

I can’t believe I didn’t notice this one earlier - do you know how many times I’ve gone around and manually changed:

System.out.println("This thing " + something + " is " + somethingElse); 

to

System.out.printf("This thing %s is %s%n", something, somethingElse); 

Do you??? Anyway you don’t need to - Alt+Enter inside the params will suggest the replacement and do it for you. Nice.

VCS Quick Access

One of the areas where I definitely don’t use IntelliJ to the best of its abilities is the Git integration. I’ve been using SourceTree combined with IntelliJ to do most of my VCS management (I’m so sorry, I’m just not a command line person, I need to actually see it). With the new laptop, I haven’t installed SourceTree yet and I’m going to try and do everything via the IDE. Previously I used the right-click menu mostly for managing VCS operations, occasionally the VCS menu, and did most stuff in the “Changes” window. But now I’ve discovered Alt+"Back Quote” (Ctrl+V on the mac) to bring up the menu of git-shaped operations for this context. To stop you having to hunt all over the keyboard to find what on earth a “Back Quote” is, it’s `, which on both my UK and US (Windows) keyboards is in the top left corner of your keyboard (on the Mac, it’s on the bottom left next to the shift, but you shouldn’t need it since your shortcut is different anyway).

Amend Commits

I’ve just discovered that you can commit a change which smushes up into your last commit and fixes it. I’m assuming it commits the new changes and (potentially) an updated commit message into the last change. I really wish I’d known about this earlier, on a number of occasions in the last few months I’ve done this either manually or by doing a rather dangerous soft reset. I will definitely be using this in the future.

"Amend

The Cursor as a Breakpoint

Did you know that you can use the cursor as a temporary breakpoint? I did not. Well, now you know.

Labelling Changes

I hope by now none of us developers are doing that icky thing of having “Copy of…” and “Backup of…” filenames and directories to keep track of our versions. We’re all using a VCS of course. But if you’re being strict about making sure that every commit leads to a working build (we all do that, right…?) then sometimes even distributed file systems like git, with its local branches, are too heavyweight for experiments and debugging.

IntelliJ has Local History, which I personally think you should never rely on, but has occasionally saved my bacon in times of strife. It also does this really nice thing (automatically) where it labels versions where the tests passed, so you’ve got some level of confidence that at that point in time, at that revision, things weren’t hideously broken. At the very least, they must have compiled. What I did not know is that you can add your own labels, so you could label some local, non-committed version of the file as “feature x works but y doesn’t” (or probably something much shorter).

I think it looks useful. As I say though, not a replacement for VCS, but a suppliment.

So, that's the end of Week One at JetBrains, and already I've learnt a whole heap of things. I'm sure there's more to come, so stay tuned!

New IntelliJ Tricks I Learnt Today

Day three of my brand new job - woohoo! One of my first tasks is checking the documentation of IntelliJ, since I’m already a “Power User”, to see if there’s stuff missing that someone like me might want to see. Firstly, a confession - I’m not sure I’ve actually read the IntelliJ documentation before. I mean, really read it. Of course, I’ve dipped into it when I needed to know something, I’ve searched for help with specific items, but like many developers, I tend to use something first rather than read the documentation first.

But I’ll tell you what, today I’ve learnt a whole bunch of useful things IntelliJ can do that I hadn’t previously known, and they’re the sorts of things you would only stumble over if a) you read the docs (pah!) or b) you watched someone else using the tool and saw them doing something you didn’t know was possible.

So here’s today’s list, and feel free to laugh at me and be smug in my direction if you already knew these things (note: I have upgraded to IntelliJ 14, and at the time of writing I don’t know which of these features are new and which are simply previously undiscovered):

Navigate directly to an action or option

Press Shift+Ctrl+A (Windows) or Shift+Cmd+A (Mac) and start typing the name of the thing you want to do, or the setting you want to change. I’ve found this especially useful now I’m back on Windows and I don’t have that nice helpful typing box on the help menu that will take me straight to the menu item I want. But what’s most useful about this for me today, as I’m setting up a new profile on a new laptop (more on the new laptop later, if it doesn’t defeat me utterly) is that from here, you can turn various settings on and off without having to navigate through the settings screen:

"Navigate

Statement completion

I never knew there was a shortcut for finishing off a statement, all that Java boilerplate you need to keep the compiler happy - semi colons and curly brackets and everything. But there is, and I can see it being super useful, especially if you’re using other bits of IntelliJ code completion to create the core of your line of code.

Issue tracker integration

I’m sure loads of people are already using integration with their favourite issue tracker, but I hadn’t really thought about this as something that I really cared about. But I was watching the intro videos for YouTrack (not surprisingly I’ll be using this instead of Jira during my time at JetBrains) and when I saw how the IDE integration worked, I suddenly realised how this small thing can add up to save thousands of keystrokes over time, not to mention saving precious navigational seconds and window-switching.

For me, just the ability to generate at least the start of the commit message is a saving in time, and adds to project consistency if all your developers are doing the same thing. On my last project we only just realised we all quoted the Jira issues in subtly different ways.

Settings shortcut keys

When I initially started using IntelliJ in anger back in 2008, I was very much a point-and-click type user. Over time, pair programming with keyboard-super-users and giving live demos with very limited time available, I’ve worked harder to become a keyboard-shortcuts person. With the latest version of IntelliJ, I can’t find the tiny icons I use to get to the settings window, something that I’ve been using a lot as I set up the new IDE instance on the new laptop. So I bit the bullet and learnt the keyboard shortcuts, and it’s already saving me a bunch of time (on the rare occasions when the aforementioned “Navigate to Action” fails me).

Ctrl+Alt+S for the settings dialog (appears to be Cmd + , on the Mac) Shift+Ctrl+Alt+S for the project structure dialog (appears to be Cmd + ; on the Mac)

"Settings

Yeah. I can’t believe I wasn’t already using those either.

Selecting the negative

I use completion/suggestions all the time from IntelliJ, I’m far too lazy to type everything. What I didn’t realise, is that you don’t just have to accept the default they give you. If, for example, IntelliJ suggests a boolean value, you can get it to negate that value by selecting it with “!” instead of enter or tab. Yes, I know I’ve done a horrible job of explaining that, but take a look at the documentation and try it, it’s quite cool.

Exclude from Autocomplete

Speaking of code completion, sometimes IntelliJ is sometimes just a bit too keen to suggest something, and you really will never want that value. In the past, I’ve always used Settings -> Editor -> General -> Auto Import to manage the suggestions (the most common one for me is I will never want java.awt.List, I will almost always want java.util.List). Well, you don’t have to go into settings to do this, you can do it from the suggestions themselves - navigate to the item you don’t want (in my case, java.awt.List) and press Alt+Enter, this will give you the option of excluding this from the suggestions in the future.

That’s what I found from just one day of browsing the documentation and setting up my IDE from scratch yet again. Maybe I’ll promptly forget all these, or maybe they’ll be new weapons in my armoury of IDE-fu. Either way, maybe one or two people will find this useful.