What is the difference between return and return value?

difference between return and return value: –

Sr No.returnreturn value
1It can be used only inside void methods.It can be used only inside not a void method.
2It is optional to use.inside not a void method “return value keyword” is mandatory.
3It returns control to the method calling statement.‘return value’ will return control & value back to the method calling statement.
difference between return and return value

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 –

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
	}
}

Leave a Comment