Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 7 features  RSS feed

 
Arjun Shastry
Ranch Hand
Posts: 1906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
We have the codebase mainly using Java 1.5. We are planning to migrate to Java 7/8. Some of the features of Java 7, i am planning to use
1)Auto close resources.
2)Catching multiple exceptions in one catch block.
3)Using String Switch where we are using dozens of if else for String comparisons.

These features definitely improve the readability but do they add any value beyond that ? Performance or any other advantage? Or any other feature you can think of adding ?(Java 8 has lambdas but as of now not adding this feature!)
 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using try-with-resources on AutoCloseables will not just improve readability of the code, but it will make your code more robust as well.

Catching disjoint exception types avoids code duplication, which makes your code easier to maintain.

I'm not a fan of switch statements in general. I prefer to map strings to objects that can perform different tasks through polymorphism. For instance, instead of:

You can do:

Another cool language enhancement you can use with Java 7 is the diamond operator. When initializing a variable of a generic type, you don't have to repeat the generic type arguments in the constructor call:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot more new features in Java 7 besides the three new language features that you mentioned. Not just new language features, but a lot of enhancements to the standard library. Every new major Java version also comes with performance updates, including, for example, new and better garbage collection algorithms.

See this page from Oracle: Java SE 7 Features and Enhancements.

I would upgrade straight to Java 8, even if you are not yet planning to use the new Java 8 language features such as lambda expressions, because Java 7 is also already obsolete: Oracle does not provide any free and public updates (bugfixes and security updates) anymore since April 2015. See: Oracle Java SE Support Roadmap.
 
Arjun Shastry
Ranch Hand
Posts: 1906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Yes, we are planning to upgrade to Java 8 but some how may not use Lambda etc immediately.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper is right: go straight for Java8.

But I am going to throw in a dissenting voice.
If it ain't broke, don't fix it.
Why do you need to change code which is already working correctly, at least until it is time to upgrade it?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!