GregT
GregT

Reputation: 1099

adding File to a String?

I just discovered this curious line of code, which compiles:

String a = (new File("")) + "f";
System.out.println(a);

Output:

f

I use jre 1.8_111 on Eclipse Neon.2 on Windows 10 Pro.

It only compiles with an empty String initializer, but I think it shouldn't, because the + operator isn't overloaded for File. Or is it?

I checked that new File("") isn't null, but its filePath and status are.

Any ideas why does it compile?

Upvotes: 0

Views: 59

Answers (1)

your code is a bad readable alternative to

File file = new File("c");
String a = file.toString() + "f";

Upvotes: 3

Related Questions