Return type in java: return statement in java

  • Basically return type is used in java methods.
  • Method signature includes this return type.
  • public int show(){ // }
  • we will use methods to do a particular task after completion of task if we want to return something to the calling place these return types will be used.
  • Based on the type of data to be returned will mention it as int , char , float double etc as return type in method signature and return statement should be the last statement of the method body.
  • In Java, the return statement is used to exit a method and return a value to the calling method. The value returned can be of any data type that is specified in the method's return type. 
  • For example, if a method has a return type of int, it can return an integer value. Here's an example of a simple method that uses the return statement to return an int value:


public int add(int a, int b) {
    int sum = a + b;
    return sum;
}

  • In this example, the add method takes two int parameters, a and b, and returns the sum of the two values. The method uses the return statement to return the value of the sum variable back to the calling method.
  • You can also use return statement without returning any value, in this case method should have void return type.


public void printHelloWorld() {
    System.out.println("Hello, World!");
    return;
}

  • In this case the method printHelloWorld doesn't return any value and the calling method doesn't need to assign the return to any variable, just invoking the method will do the job.
  • It is also worth noting that when a return statement is executed, it immediately exits the current method, regardless of where it is in the method's execution flow. So, any code after a return statement will not be executed.

Type of declaration of methods based on return type and arguments:

1.Method with out return type  and without arguments: return statement in java


  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public void add(){
  5.  
  6. int a=40;
  7. int b=50;
  8. int c=a+b;
  9. System.out.println(c);
  10.  
  11. }

  12. public static void main(String args[]) // ->method prototype.
  13.  
  14. sample obj= new sample();
  15. obj.add();
  16.  
  17.  
  18.  }
  19. }

2.Method with out return type and with arguments.

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public void add(int a, int b){
  5.  
  6. int c=a+b;
  7. System.out.println(c);
  8.  
  9. }

  10. public static void main(String args[]) // ->method prototype.
  11.  
  12. sample obj= new sample();
  13. obj.add(13,24);
  14.  
  15.  
  16.  }
  17. }

3.Method with return type  and without arguments.


  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public int add(){
  5. int a=40;
  6. int b=50;
  7. int c=a+b;
  8. return c;
  9. }

  10. public static void main(String args[]) // ->method prototype.
  11.  
  12. sample obj= new sample();
  13. int x=obj.add(); 
  14. System.out.println(x);
  15.  
  16.  }
  17. }

4.Method with return type and with arguments.

 

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public int add(int a, int b){
  5.  
  6. int c=a+b;
  7. return c;
  8. }

  9. public static void main(String args[]) // ->method prototype.
  10.  
  11. sample obj= new sample();
  12.  
  13. int x=obj.add(1,2);
  14. System.out.println(x);
  15.  
  16.  }
  17. }


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

3 comments for Return type / return statement in java example

Select Menu