F
FET
Hello everyone,
I am slightly confused here. Here is some code:
public class samp{
public int f(StringBuffer str){
if(str == null)
str = new StringBuffer(10);
str.append("inside f");
return 0;
}
public static void main(String args[]){
samp obj = new samp();
StringBuffer str = null
obj.f(str);
System.out.println(str);
}
}
If I run the code above, the System.out.println line prints null. If
however, I allocate a new StringBuffer(10) in main() instead of
assigning it null, then it prints "inside f".
If I replace all StringBuffer with String, it prints null, no matter
where it gets initialized.
My problem is I am not able to explain this behaviour. Can someone
please explain why this is so ?
Thanks in advance.
Best Regards.
I am slightly confused here. Here is some code:
public class samp{
public int f(StringBuffer str){
if(str == null)
str = new StringBuffer(10);
str.append("inside f");
return 0;
}
public static void main(String args[]){
samp obj = new samp();
StringBuffer str = null
obj.f(str);
System.out.println(str);
}
}
If I run the code above, the System.out.println line prints null. If
however, I allocate a new StringBuffer(10) in main() instead of
assigning it null, then it prints "inside f".
If I replace all StringBuffer with String, it prints null, no matter
where it gets initialized.
My problem is I am not able to explain this behaviour. Can someone
please explain why this is so ?
Thanks in advance.
Best Regards.