You have to remember only six rules for returning a value:
1. You can return null in a method that has an object reference return type.
public Button doStuff() {
return null;
}
2. An array is a perfectly legal return type.
public
String [] go() {
return new String[] {"Fred", "Barney", "Wilma"};
}
3. In a method with a primitive return type, you can return any value or variable
that can be implicitly converted to the declared return type.
public int foo() {
char c = 'c';
return c; // char is compatible with int
}
4. In a method with a primitive return type, you can return any value or variable
that can be explicitly cast to the declared return type.
public int foo () {
float f = 32.5f;
return (int) f;
}
5. You must not return anything from a method with a void return type.
public void bar() {
return "this is it"; // Not legal!!
}
6. In a method with an object reference return type, you can return any object
type that can be implicitly cast to the declared return type.
public Animal getAnimal() {
return new Horse(); // Assume Horse extends Animal
}
Hi all,
with respective to above 6 rules can anybody provide the explantion with sample program for each rule
thanks
venkat