• Post Reply Bookmark Topic Watch Topic
  • New Topic

Try-with-resources best practices

 
salvin francis
Bartender
Posts: 1406
18
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following is a good practice to follow ?



or



or do they both mean the same ?

 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends.

I usually just use the second variant, but there may be a case where you want to use several different wrappers around an inner stream.

For instance, let's say you want to read both Java primitives from a stream, and characters in a certain encoding. You can wrap the inner stream twice to suit your needs:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!