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
- package com.instanceofjava;
- class sample{
- public void add(){
- int a=40;
- int b=50;
- int c=a+b;
- System.out.println(c);
- }
- public static void main(String args[]) // ->method prototype.
- {
- sample obj= new sample();
- obj.add();
- }
- }
2.Method with out return type and with arguments.
- package com.instanceofjava;
- class sample{
- public void add(int a, int b){
- int c=a+b;
- System.out.println(c);
- }
- public static void main(String args[]) // ->method prototype.
- {
- sample obj= new sample();
- obj.add(13,24);
- }
- }
3.Method with return type and without arguments.
- package com.instanceofjava;
- class sample{
- public int add(){
- int a=40;
- int b=50;
- int c=a+b;
- return c;
- }
- public static void main(String args[]) // ->method prototype.
- {
- sample obj= new sample();
- int x=obj.add();
- System.out.println(x);
- }
- }
4.Method with return type and with arguments.
- package com.instanceofjava;
- class sample{
- public int add(int a, int b){
- int c=a+b;
- return c;
- }
- public static void main(String args[]) // ->method prototype.
- {
- sample obj= new sample();
- int x=obj.add(1,2);
- System.out.println(x);
- }
- }
Best website to clear doubts.
ReplyDeleteaweswome
ReplyDeleteit simple logic and easy way of finding solutions,super
ReplyDelete