According to what I have read, the only possible outputs for this program are "A"
or no printouts. But since the statement new MyString("A").concat("B");
is creating a new object with string "AB"
, couldn't this object also be garbage collected resulting in output "AB"
?
class MyString {
private String str;
MyString(String str) { this.str = str; }
public void finalize() throws Throwable {
System.out.print(str);
super.finalize();
}
public void concat(String str2) {
this.str.concat(str2);
}
public static void main(String[] args) {
new MyString("A").concat("B");
System.gc();
}
}