Top Java 7 Features in Oxygene for Java
I saw an article listing the Top 8 features in Java 7 and realized a number of them were already present in Oxygene for Java.
Strings in Case Statements
In Java they are Switch statements, but in Oxygene we call them a case statements. Instead of a series of ifstatements, you can use a single case statement to evaluate multiple values of a string.
method TestApp.ProcessStatus(status: String);begincase status of'urgent': DoUrgent;'high': DoHigh;'low': DoLow;else DoNormal;end;end; |
Using Statements
In Java 7 this is called the try-with-resources statement, but the idea is to have a try block to catch exceptions and also close resources that need to be closed. Just like on .NET, the Java virtual machine supports a garbage collecting memory manager, but also just like on .NET there are unmanaged resources, like handles. These resources still need to be closed manually in your code, even when you encounter an exception. In Oxygene we make this really easy with the using statement.
using f :=new java.io.File(...)dobegin...end; |
tryusing f :=new java.io.File('...')dobegin // use fend;// f always gets closedexcept// handle exceptionend; |
One note about the using statement. In .NET the object instance that is manged by using must implement IDisposable. In Java the required interface is Closable. A class that implements Closable is a class that needs to be closed when you are done with it, and it is a class that using will close automatically for you.
Binary Integral Literals
Java 7 adds support for binary literals using binary notation. Oxygene has that, and hexadecimal too!
var bin := %10101010;var hex :=$DEADF00D; |
This is another spot where Oxygene actually beats Java. With Java 5 & 6 you had to declare the type and the constructor completely, which got quite verbose for generic types. With Java 7 it introduced a left to right type inference, but you still have to specify a partial type on the right, and this feature only works with generic types.
In Oxygene you have a full right to left type inference. Just declare the variable using the var keyword on the left, and create a new instance on the right.
var m :=new HashMap, List>; |
The Rest
Most of the rest of the new features were either added to the Java Framework, so Oxygene gets those automatically, or they are features that don’t really make sense to Java. Keep in mind there are still quite a few features in Oxygene that Java doesn’t even touch!