String myString = "this";
//string is immutable
myString.concat(" that");
//a new object is created but not assigned to anything
System.out.println(myString); //prints out "this"
I would prefer a compile time error - why is this not the case ? The same question can be applied to any method with a return type, when it is called without supplying return type.
public myObject doStuff(...whatever){
//define my method
return anObject;
}
can be called without providing a reference/variable to hold the return type:
MyObject newObject = doStuff(); //works
doStuff(); //works too without assigning return object
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…