This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Java 7 features

 
Ranch Hand
Posts: 1907
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!)
 
Saloon Keeper
Posts: 12133
258
  • 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:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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: 1907
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.
 
Marshal
Posts: 69789
277
  • 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?
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic