Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

RETURN statement in Java?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only know a tiny thing about this which is this

When we create a method, it provides the option to return some data in many forms such as int, boolean, string etc.

But

Where it returns

Why and where its useful to use this RETURN statement in method

Please could you explain this with simplest and easy way
 
Ranch Hand
Posts: 1338
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using RETURN is useful when you want to retrieve and use the output of the method which is returning the value in a wider scope. For example, if you write a method calculateSum() and it returns the value 5 you could use that value 5 together with another methods like multiply() in order to orchestrate them and solve a bigger problem.




Then when from a wider context like a Class you call calculateSum(x,y) you will get the result of the calculation performed in the method and for example you could write it in the console.




It will print out "Sum results: 5"
 
Marshal
Posts: 65053
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I would shorten that method toRemember a method must state its return type (or void).
 
ImDinesh Sharma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:Using RETURN is useful when you want to retrieve and use the output of the method which is returning the value in a wider scope. For example, if you write a method calculateSum() and it returns the value 5 you could use that value 5 together with another methods like multiply() in order to orchestrate them and solve a bigger problem.



So what you mean is We can solve a bigger problem using two methods with a single return type right
 
Bartender
Posts: 20933
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In some languages, they make the difference between FUNCTIONS and PROCEDURES (or SUBROUTINES). "Subroutine" is also sometimes used as a generic term including functions, but that depends. A FUNCTION returns a RESULT. A PROCEDURE does not. Or in Java terms, returns "void".

Java uses the term method for both types of routine. That's in large part to distinguish Java-style (OOP) subroutines from old-style Procedure-Oriented (non-OOP) Language subroutines. In some languages, such as C++, functions, procedures and methods may all exists, since C++ is a hybrid POL/OOPL.

A return statement is an immediate terminate of the function, procedure or method, returning to the exact point it was called from (as opposed to a jump or a thrown object).

The term "function" is almost a direct lifting from Calculus terminology.

For ease of code readability and maintainability, it's preferred that the RETURN statement be the last statement in a subroutine. In Java, if you do not code a return in a method returning void, the compiler acts like you did anyway. My second-favorite place to put return statements is at the head of a method, in which case, I'm usually filtering the inputs to determine whether or not the rest of the method should execute. Putting return statements in the middle makes them harder to see, and thus the code is harder to understand.

Coding statements immediately after a return statement will result in a "no path to statement" compiler error, since the return jumps out immediately and thus there's no way for processing to reach those statements. To make them reachable, you'd have to have a label preceeding them or be have the return statement terminating a conditional block.
 
Ranch Hand
Posts: 58
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ImDinesh Sharma wrote:I only know a tiny thing about this which is this

When we create a method, it provides the option to return some data in many forms such as int, boolean, string etc.

But

Where it returns

Why and where its useful to use this RETURN statement in method

Please could you explain this with simplest and easy way

let's take example

Class c1{
Int i=5;
boolean b=calculate() ;
Public boolean claculate() {
If(i<10)
Return true;
else
Return false ;}
Public static void main(String[] args) {
c1 t=new c1() ;
}
}
Staff note (Knute Snortum):

Please use code tags to surround your code, make sure it's properly formatted and that it will compile. You have several mistakes involving the case of the letters.

 
Rancher
Posts: 126
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a more general note, java is case-sensitive so always pay attention to capitalisation (as well as spelling and layout). That makes it easier for beginners to read and understand the code.

 
Angus Ferguson
Ranch Hand
Posts: 1338
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So what you mean is We can solve a bigger problem using two methods with a single return type right




You can get the output of a method which sum two numbers and then use this output to solve a bigger challenge together with another one which for example multiplies the retrieved output from the sum method.

It can get much more elaborated and it offers the posibility to reduce duplicated code improving readibility, scalability and modularity.

Note that each method has a diferent return statement which could be the same type of a different one or even void (returning nothing), not necesarily all the methods contained in a system should contain the same return type.
 
ImDinesh Sharma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:

It can get much more elaborated and it offers the posibility to reduce duplicated code improving readibility, scalability and modularity.



Got it!

Lemmi show you an example



If i create a method in ReturnStatements class but i call it in different class ReturnStatements2, will there be any additional things to the process?
Staff note (Knute Snortum):

Be sure that your reply isn't within the quote tags. I've fixed it for you this time. Use the Preview button before the Submit button to be sure your post displays the way you want it to.

 
Bartender
Posts: 2326
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of points from my end.
  • Even a method that has a void return type can have return statement(s)
  • A finally block has precedence over the return statement and can even have a return of it's own.
  •  
    Tim Holloway
    Bartender
    Posts: 20933
    127
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    I wouldn't recommend this. Not the "finally" return, but rather the fact that the last statement in the method is not a return. Technically it's legal, but it's a potential hazard in that the return paths and values are somewhat subject to damage when the code is later updated. Rather, I prefer to set a known return value and have an explict return statement at the end of every function method. Like so:



    One thing that's probably more apparent this way is that that final return can never be encountered thanks to the finally clause. Indeed, the compiler should mark both examples as an error but the second example is, I think more obvious without actually compiling.

    I often set a return value to something that's either obviously wrong or is the worst-case value. That way if the logic is flawed, the results are A) more consistent in their wrong values and B) easier to track down.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!