As with try-as-expression, there are many other language features we can simulate with Lambdas. Another example is removing the cast commonly needed with instanceof. Kotlin has a nice feature called smart casts, which allows you to do if (x instanceof Duck) { x.quack(); } if (x instanceof Duck) { x.quack(); } Where x is “casted”… Read more »
Posts Categorized: Java
Try as Expression in Java 8
I’ve been familiarising myself with the new Java 8 language features. It’s great how much easier it is to work around language limitations now that we have lambdas. One annoyance with Java is that try blocks cannot be used as expressions. We can do it with “if” conditionals using the ternary operator. String result =… Read more »
JavaScript shell scripting with Nashorn
One of the nice features of Nashorn is that you can write shell scripts in JavaScript. It supports #!s, #comments, reading arguments, and everything there’s a Java library for (Including executing external processes obviously) Here’s an example #!/home/benji/nashorn7/bin/nashorn #this is a comment print(’I am running from ‘ + __FILE__); var name = arguments.join(’… Read more »
JavaScript tests with JUnit
This weekend I have been playing with Nashorn, the new JavaScript engine coming in Java 8. As an exercise I implemented a JUnit runner for JavaScript unit tests using Nashorn. Others have implemented similar wrappers, we even have one at work. None of the ones I have found do everything I want, and it was… Read more »
MultiCatch in c# and old java
Someone on IRC was asking whether it was possible to do catch multiple types of exceptions at the same time in c#. In Java 7 there’s a feature from project coin called multi catch that enables the following syntax: public class MultiCatch { public static void main(String… args) { try { throw new ExceptionA(); }… Read more »
Jammer – IRC to Yammer Bridge
Jammer is a quick 1hr project that clones IRC discussion on Yammer. Yammer is a social network for internal use by organisations. It’s a great way to broadcast information and have asynchronous discussion, and simple enough for anyone to use. Developers already have a great forum for asynchronous discussion – IRC. IRC is great for… Read more »
Typesafe Hibernate POJO Queries without code generation
Hibernate is great, but often one has to specify queries as HQL in Strings, or as criteria which allow building of invalid queries. It would be great to use the java type system to help enforce correct queries. I am aware of some tools to do this, but all the ones I have seen require… Read more »
Java Abuse: Inline instanceof
One annoyance in Java is having to do instanceof checks on multiple lines. e.g. if (object instanceof Foo) { Foo foo = (Foo)object; foo.foo(); }if (object instanceof Foo) { Foo foo = (Foo)object; foo.foo(); } While this is often a sign of a design failure, there are times when instanceof checks are required often due… Read more »
Java Abuse – Currency Pattern
Here’s a sillier one from last night… People often complain about not being able to return multiple values from a method in Java. I can’t see a good reason for wanting to do this, but some do. The example I was given was wanting to do: int foo, bar, baz; list(foo, bar, bar) = func(ponies);int… Read more »
Java Abuse – Ternary Try/Catch
We often discuss Java limitations on IRC and try to come up with (sometimes silly) workarounds. Unfortunately after time passes it’s often easy to forget the outcome, and lose code snippets. So I thought I’d start blogging some of them so I don’t lose them, and other people might suggest other ways of doing things… Read more »