All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Return Statement In Java

Last Updated : Mar 11, 2024

Return Statement In Java

In this article we will show you the solution of return statement in java, in Java programming, the return statement is used to return a value after the block has been executed. Within a loop, returning from a statement will cause the loop to break, and the compiler will ignore any subsequent statements.

There is probably no keyword in Java that is underutilized more than return. Most programming languages provide the capability of specifying a method's return value, which can be found in almost every language.

In Java, the return keyword returns a value from a method. A value is returned immediately after the keyword is encountered by the method.

The method will stop executing statements after pressing the return keyword, and any variables created will be discarded.

Depending on the developer's preference, the return keyword can be used with or without a value.

We will now discuss the idea of how to define a return statement in java with an example.

Step By Step Guide On Return Statement In Java :-

class talkerscode
{
    private int a;
    public SumReturn(int i)
    {
        a = i;
    }
    public SumReturn addition()
    {
        return result = new return(a + 100);
        return result;
    }
    public void display()
    {
        System.out.println("Additon result: " + a);
    }
}
public class talkerscode
{
    public static void main(String[] args)
    {
        return obj1 = new return(50);
        return obj2;
        obj2 = obj1.addition();
        obj2.display();
    }
}
  1. The class name is duplicated, which is not allowed. You should rename one of them (e.g., to SumReturn for the first one).
  2. The constructor should have the same name as the class (e.g., public SumReturn(int i)).
  3. The addition() method should declare and initialize a new SumReturn object named result. The return result; line after that is unreachable and should be removed.
  4. The display() method should print the sum of a and 100, not just a.
  5. In the main() method, you cannot use return to create new objects. Instead, you should use the new keyword directly (e.g., SumReturn obj1 = new SumReturn(50);).
  6. The main() method should declare obj2 before assigning it to obj1.addition().
  7. The display() method should be called on obj1, not obj2.
  8. Define a new class called SumReturn that has an integer field called a, a constructor that initializes it, an addition() method that returns a new SumReturn object with a + 100 as the value of a, and a display() method that prints the sum of a and 100.
  9. Define a new class called TalkersCode that contains a main() method.
  10. In the main() method, create a new SumReturn object named obj1 with an initial value of 50.
  11. Invoke the addition() method on obj1 to create a new SumReturn object named obj2 with a value of 150.
  12. Invoke the display() method on both obj1 and obj2 to print their respective values (both of which should be 150).

Conclusion :-

As a result, we have successfully learned how to define a return statement in java with an example.

The return statement returns some value on completion of the block execution in Java.

In addition to the return type, the returned value is determined by the method.

You must match the return type of the method with the value that will be returned.

This write-up explained what return does in Java with the help of some examples.

I hope this article on return statement in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪