aspose file tools*
The moose likes Beginning Java and the fly likes Return Keyword Help! 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 » Java » Beginning Java
Bookmark "Return Keyword Help!" Watch "Return Keyword Help!" New topic
Author

Return Keyword Help!

Buburub
Greenhorn

Joined: Apr 08, 2001
Posts: 8
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

Joined: Dec 20, 2000
Posts: 782
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

Joined: Apr 08, 2001
Posts: 8
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

Joined: Mar 30, 2001
Posts: 24
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Buburub
Greenhorn

Joined: Apr 08, 2001
Posts: 8
Thanks a Lot for the help!
and...yes I'm going to go reregister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return Keyword Help!