Have you seen the list of features that will be included in the next version of Java? You can see a pretty good list here. It’s just disappointing to me that Closures will not be included. Sun is spending time on things like improving Swing and building a library to improve file IO, and making improvements to the concurrency library. All that is fine I guess, but what I want are language improvements!
I’ve started playing around with Groovy and Ruby, and I’ve really enjoyed some of the features offered in those languages. Sun is borrowing some ideas from them, and adding them to Java. Like they’re including the Elvis operator which is a cool little shortcut that let’s you avoid Null Pointer Exceptions. It lets you write code like this:
String s = mayBeNull?.toString() ?: "null";
which is a way of saying “If mayBeNull is not null, run the toString method on it. If it is null, return the value “null” as a string.
That’s great, but why stop there!? Groovy and Ruby offer some great little operators and features, and I’d love to see Sun incorporate them into Java 7. Here’s a list (in no particular order) of what I’d like to see added to Java. By the way… I know there are workarounds to these things. I’m just saying these features would be nice. They’d make coding in Java more fun and productive. So here’s my list:
1. Make Integers behave the way you’d expect so that if you compare one Integer to another, it compares their values, not their memory locations. That’s what programmers want to do 99.9% of the time. Make the language do the most useful thing. Compare the two values, not their memory locations.
2. Improve the Collections utility methods. How about making it easier to combine maps, and find the differences between maps? I think it’d be great to be able to combine maps by using the + operator like this:
hashMap3 = hashMap1 + hashMap2
Or how about find the difference between two collections by running this: hashMap3 = hashMap1 - hashMap2
I know you can use putAll or addAll or retainAll and there are some useful utility methods in the Collections class, but I think it’d be cool and faster and more intuitive to just use the plus or minus sign, and make them work on arrays as well as maps and sets and lists. Right now, this wont work:
String[] array1 = {"one", "two"};
String[] array2 = {"three", "four"};
String[] array3 = array1 + array2;
and I wish it did.
3. Improve Date/Calendar objects. Wouldn’t it be cool if today++ gave you tomorrow? One thing that Java 7 is adding is a new DateTime object, based on the Joda project to replace the Calendar object. It looks pretty cool from what I’ve read about it. You can read more about it here. But Groovy dates, combined with the Range object, give you the ability to iterate through time like this:
for (today..today+7)
and I have come to love the simplicity of using Groovy dates.
4. Give us a Range object so that you can do things like
for (beginningOfMonth..endOfMonth) { doCoolStuff(it); }
or this:
for(1..10){
doFunStuff(it);
}
or this:
switch( myNumber){
case 1..10; doSomething(); break;
case 11..20; doSomethingElse(); break;
}
5. Add a way to query a collection. Like suppose you had a collection of address objects, I think it would be great if you could say something like:
Map atlantaAddresses = myAddressHashMap.find("city='Atlanta'");
and it would be smart enough to search all the objects in the collection and return you the ones that match your criteria. The Collections class has a binarySearch method, but it only tells you if the collection contains what you’re looking for, and the list index of the object if found. I’m looking for a method to return a collection if it matches a criteria.
6. Make Null return false. If you’re testing a value, and the object that contains the value is null, or if the value itself is null, dont throw a Null Pointer Exception, just return false. If it’s Null, it’s obviously not true, so dont make me check for null and check for a value. If it’s null just return false.
7. Add a spread operator (like *. ) so that you can apply an operation to all items of a collection. Like this line of code:
myCustomerCollection*.printFullName();
would iterate through the collection and run the printFullName() method on each object in the collection.
8. I wish you could write formatted strings like
printf("${order.customer.name} bought a new ${order.widget.name} on ${order.date.prettyString}", order);
instead of having to concatenate a string. Other languages support that sort of thing, and I doubt it’s very hard to do. So give it to us!
So that’s my Java wish list. What language features or libraries would you like to see added to Java 7?
Dear Java 7 – Please become more helpful
Have you seen the list of features that will be included in the next version of Java? You can see a pretty good list here. It’s just disappointing to me that Closures will not be included. Sun is spending time on things like improving Swing and building a library to improve file IO, and making improvements to the concurrency library. All that is fine I guess, but what I want are language improvements!
I’ve started playing around with Groovy and Ruby, and I’ve really enjoyed some of the features offered in those languages. Sun is borrowing some ideas from them, and adding them to Java. Like they’re including the Elvis operator which is a cool little shortcut that let’s you avoid Null Pointer Exceptions. It lets you write code like this:
String s = mayBeNull?.toString() ?: "null";
which is a way of saying “If mayBeNull is not null, run the toString method on it. If it is null, return the value “null” as a string.
That’s great, but why stop there!? Groovy and Ruby offer some great little operators and features, and I’d love to see Sun incorporate them into Java 7. Here’s a list (in no particular order) of what I’d like to see added to Java. By the way… I know there are workarounds to these things. I’m just saying these features would be nice. They’d make coding in Java more fun and productive. So here’s my list:
1. Make Integers behave the way you’d expect so that if you compare one Integer to another, it compares their values, not their memory locations. That’s what programmers want to do 99.9% of the time. Make the language do the most useful thing. Compare the two values, not their memory locations.
2. Improve the Collections utility methods. How about making it easier to combine maps, and find the differences between maps? I think it’d be great to be able to combine maps by using the + operator like this:
hashMap3 = hashMap1 + hashMap2Or how about find the difference between two collections by running this:
hashMap3 = hashMap1 - hashMap2I know you can use putAll or addAll or retainAll and there are some useful utility methods in the Collections class, but I think it’d be cool and faster and more intuitive to just use the plus or minus sign, and make them work on arrays as well as maps and sets and lists. Right now, this wont work:
String[] array1 = {"one", "two"};
String[] array2 = {"three", "four"};
String[] array3 = array1 + array2;
and I wish it did.
3. Improve Date/Calendar objects. Wouldn’t it be cool if today++ gave you tomorrow? One thing that Java 7 is adding is a new DateTime object, based on the Joda project to replace the Calendar object. It looks pretty cool from what I’ve read about it. You can read more about it here. But Groovy dates, combined with the Range object, give you the ability to iterate through time like this:
for (today..today+7)and I have come to love the simplicity of using Groovy dates.
4. Give us a Range object so that you can do things like
for (beginningOfMonth..endOfMonth) { doCoolStuff(it); }
or this:
for(1..10){
doFunStuff(it);
}
or this:
switch( myNumber){
case 1..10; doSomething(); break;
case 11..20; doSomethingElse(); break;
}
5. Add a way to query a collection. Like suppose you had a collection of address objects, I think it would be great if you could say something like:
Map atlantaAddresses = myAddressHashMap.find("city='Atlanta'");
and it would be smart enough to search all the objects in the collection and return you the ones that match your criteria. The Collections class has a binarySearch method, but it only tells you if the collection contains what you’re looking for, and the list index of the object if found. I’m looking for a method to return a collection if it matches a criteria.
6. Make Null return false. If you’re testing a value, and the object that contains the value is null, or if the value itself is null, dont throw a Null Pointer Exception, just return false. If it’s Null, it’s obviously not true, so dont make me check for null and check for a value. If it’s null just return false.
7. Add a spread operator (like *. ) so that you can apply an operation to all items of a collection. Like this line of code:
myCustomerCollection*.printFullName();
would iterate through the collection and run the printFullName() method on each object in the collection.
8. I wish you could write formatted strings like
printf("${order.customer.name} bought a new ${order.widget.name} on ${order.date.prettyString}", order);
instead of having to concatenate a string. Other languages support that sort of thing, and I doubt it’s very hard to do. So give it to us!
So that’s my Java wish list. What language features or libraries would you like to see added to Java 7?