Possible Duplicate:
what happens to an object in Java if you do not reference it, like here : myString.concat(“that”)
public class ReturnTest
{
public static void main(String[] args)
{
ReturnTest rt = new ReturnTest();
rt.show();
}
public String show()
{
return "Hello";
}
}
In the above code the show() method returns a String value which is not captured by any variable. Neither the compiler nor the JVM raise any warning, error or exception whatsoever. The same is true for primitive return types. Why?
Shouldn’t the compiler make sure that no important value returned by a method is lost by this?
How can i fix this from a shell??
This is the design of the language.
In many cases, methods returns a value you don’t care about. For example
StringBuilder.append()returns the object itself.It is true that in some cases an important return value might be lost. For example,
InputStream.skip(long n)returns the number of bytes skipped. Andreadreturns the number of bytes read. There are some tools (Checkstyle, FindBugs) that detect such bugs. But the language itself doesn’t request checking the return value. And even if it did, the program could choose to ignore the value.