difference between return and return value: –
Sr No. | return | return value |
1 | It can be used only inside void methods. | It can be used only inside not a void method. |
2 | It is optional to use. | inside not a void method “return value keyword” is mandatory. |
3 | It returns control to the method calling statement. | ‘return value’ will return control & value back to the method calling statement. |
Example 1
public class A {
public static void main(String[] args) {
A a1 = new A();
a1.test();
return;
}
public void test() {
System.out.println(100);
return;
}
}
Output : 100
You Also Learn –
- What is Abstract Class in Java
- What is Lambda Expression in Java 8
- What is Exception Handling in Java
- Types of Exceptions in Java
- What is inheritance in Java
- What is the Final Keyword in Java
- What is Interface in Java
- What is Wrapper Class in Java
- Break Keywords in Java with Example
- What is Method In Java With Example
- What is Constructor in Java With Example
- What is Polymorphism in java
- What is a non-static variable in java
- Types of access specifiers in java
- What is the local variable in java
- Types of loops in java with program
- Remove duplicate elements in an Array in java
- What is var type in java
- What is a static variable in java
Note: If You Write Anything After the return Keyword then That Line of Code Will Never Run, and Hence You Will Get an Unreachable Code Error.
public class A {
public static void main( String[] args) {
A a1 = new A();
int x=a1.test();
System.out.println(x);
}
public int test () {
return 100;
System.out.println(10); //Unreachable Code Error
}
}