aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes returning a value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "returning a value" Watch "returning a value" New topic
Author

returning a value

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
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
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Hi Venkat,

the rules and the accompanying code look pretty self-explanatory. What is it that you do no understand ? Could you be a little more specific ?

Secondly, I saw that you have been asked several times by the bartender and sherrif to comply with the usename policy on this forum. Why don't you do something about that ?

Soumya.
 
wood burning stoves
 
subject: returning a value