• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

returning a value

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic