answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Return statement is commonly used with Method which return value.

Example,

int sum(int a,int b)

{

return (a+b);

}

This method gives sum of two numbers which can be assigned to any int variable.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

When declaring a method, when you type in "public (or private) int", the int gives the return type. The return statement gives the value to return.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are return statements in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the pane in which the Java programming statements are located called?

The pane in which the Java programming statements are located is called


What are the three looping statements in java PL?

There are 4 different looping statements in Java. They are:While loopDo-While loopFor loopFor Each loop


What are the statements in java?

There are many kinds of statements that are used in Java and they are predominantly used for database connectivity using JDBCEx:PreparedStatement - for normal SQL QueriesCallableStatement - for stored procedures


What is a method in Java?

A Java method is a sequence of statements. It is comparable to a function, subroutine, or procedure in other languages.


What is the return type of finally method in java?

The final and finally keywords have no impact on the return type of a method in Java.


What are the uses of java database connectivity?

Java Database Connectivity, a Java API that enables Java programs to execute SQL statements. This allows Java programs to interact with any SQL-compliant database.


How are java program statements terminated?

Each statement in Java ends with a semicolon, for example: int a; a = 5; int b = 10;


How many values can a method return in Java?

A method in java can declare only one return value and type at a time. For ex: a single method cannot have a code that returns a string in some cases and an integer in other cases. Java compiler does not let you do that. You can only have one return type for every method in java.


What is default return type for method in java?

default return type is : true


What is syntax for exceptional handling in java?

try{ statements; } catch(Exception e) { message }


Different controls structures in java?

posted by Suhit s Kalubarme Software Developer Solapur 9260069199 Controls are components that allow the user to interact with your application in various ways e.g. a commonly use the control is command button. Various controls: 1. Label 2. Button 3. Check Box 4. Choice Box 5. TextArea 6. TextField


Control statements in java?

A programming language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program. control statements are mainly three types in java. they are selection, iteration, and jump.