What are return statements in java?

Java

1 answer

Answer

1166917

2026-09-06 22:20

+ Follow

Java
Java

you can understand what is the purpose of return in Java after this simple example

Basically return use in Java to return some values from a method

note: this sample will work on all Java versions, i test it on JDK 1.4

enjoy the code.

file name : Class1.Java

-----

package mypackage1; //package name

public class Class1 //class name + file name the

{

public String add(int a,int b) //a method in the class; its type is String and it take a and b variables; a and b are int

{

int answer = a+b; //this is some code in the class; maybe some operations; no need to know whats happening in this method

System.out.println("I'm in Class1"); //this is some code in the class; maybe some operations; no need to know whats happening in this method

return " I'm the returned value "+ answer; ////this is what you need to take from the method

}

public static void main(String [] arg)

{

Class1 c1 = new Class1(); //creating c1 object from Class1 Class

System.out.println(c1.add(10,6)); //printing the returned values from the method;

}

}

-----------------

output:

I'am in Class1

I'am the returned value 16

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.