Search code examples
javafilefile-iofilereaderjava-io

Creating a FileReader which way is better for optimisation?


From an optimization stand point, is it better to declare the File separately like this

File f = new File("sample.txt");
FileReader fr = new FileReader(f);

or, is it better to do it inline like this

FileReader fr = new FileReader(new File("sample.txt));]

Aren't really sure if it even matters really.


Solution

  • It makes no difference. Do whatever's more readable in your particular situation.

    It's possible that it could affect when the File object is eligible for garbage collection, but I'd be hugely surprised to see a situation in which that's a significant difference.