Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return Keyword Help!

 
Buburub
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like a lot of question on the Java Cert kept on using return and I don't really know how it works:
>
>
> * method 1
> * {return 1}
> * method 2
> * {return 5}
> * Main method
> * {System.out.println("something")
>
>Can u please fully explain what return does and how its used in method invocation and method behaviors.
Thanks A Lot!!!
 
Andy Ceponis
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well in order for a method to return something it must be in the method declaration. Something like this:
public static int returnThis()
{
int x = 5;
return x;
}
Return simply returns a value of the type that you declared. If you are looking to see if a method returns a value or not look for the "void" keyword. Void means the method does not return a value. If you see stuff like string, object, int, char, etc.... then the method does return a value. That help?
 
Buburub
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a method:
public int method1( )
int x;
return 5
Does this mean that this method always return a 5 or does it mean that its added to the variable after you pass a value into it?
 
sanjays samadder
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
a return statement is always associated with a method.
it is like you are giving raw material to a factory ,it processes
the raw materials and gives it back to you.
you can the sell it in the market or throw it away as your need be.
if you do not want the product back then tell them to sell it themselves!!!
in that case the method has a return type as void.
when you receive back the product( in the case of a method through a return statement like return(x);
or return 5;
or return "hello";etc
you have to have a place to dump it.
ie you need a variable to store the return value at the position where you call the method.
if your method is like this:
public String print()
{
return "hello";
}
then you should have a statement like this at the place where you have called this print method
like this:
String s=print();
what happens here is that the print function returns "hello"
via the return statement in that method;
you store it in the varable s;
now you can you it as your program demands
hope this helps.
regards
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buburub,
I hate to say this but, your name does not comply with the Javaranch naming guidelines which can be found at http://www.javaranch.com/name.jsp
please register again with a valid name.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Buburub:
If you have a method:
public int method1( )
int x;
return 5
Does this mean that this method always return a 5 or does it mean that its added to the variable after you pass a value into it?

Your example will always return 5.
 
Buburub
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a Lot for the help!
and...yes I'm going to go reregister
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic