6. return statement
- If any method returns any primitive value or any type of object then before closing that method we need to return that corresponding value or object.
Syntax:
return ;
-------------------------------------------------------------------------------------------
Program name : ReturnDemo1.java
Output :
m1 method returns nothing
m2 method returns int value
m3 method returns String value
class ReturnDemo1
{
public static void main(String args[])
{
public void m1()
{
System.out.println("m1 method returns nothing");
}
public int m2()
{
System.out.println("m2 method returns int value");
return 123;
}
public String m3()
{
System.out.println("m3 method returns String value");
return "Java";
}
public static void main(String args[])
{
ReturnDemo1 r = new ReturnDemo1();
r.m1();
r.m2();
r.m3();
}
}
}
Compile : javac ReturnDemo1.java
Run : java ReturnDemo1
Output :
m1 method returns nothing
m2 method returns int value
m3 method returns String value
-------------------------------------------------------------------------------------------
Thanks for your time.
- Nireekshan