
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Importance of return type in Java?
A return statement causes the program control to transfer back to the caller of a method. Every method in Java is declared with a return type and it is mandatory for all java methods. A return type may be a primitive type like int, float, double, a reference type or void type(returns nothing).
There are a few important things to understand about returning the values
- The type of data returned by a method must be compatible with the return type specified by the method. For instance, if the return type of some method is boolean, we can not return an integer.
- The variable receiving the value returned by a method must also be compatible with the return type specified for the method.
- The parameters can be passed in a sequence and they must be accepted by the method in the same sequence.
Example1
public class ReturnTypeTest1 { public int add() { // without arguments int x = 30; int y = 70; int z = x+y; return z; } public static void main(String args[]) { ReturnTypeTest1 test = new ReturnTypeTest1(); int add = test.add(); System.out.println("The sum of x and y is: " + add); } }
Output
The sum of x and y is: 100
Example2
public class ReturnTypeTest2 { public int add(int x, int y) { // with arguments int z = x+y; return z; } public static void main(String args[]) { ReturnTypeTest2 test = new ReturnTypeTest2(); int add = test.add(10, 20); System.out.println("The sum of x and y is: " + add); } }
Output
The sum of x and y is: 30
- Related Articles
- Covariant return type in Java\n
- What is covariant return type in Java?
- Can we change return type of main() method in java?
- What is the return type of a Constructor in Java?
- Does a constructor have a return type in Java?
- Importance of HashSet in Java
- Importance of @JsonFilter annotation in Java?
- Importance of clone() method in Java?
- Importance of isDaemon() method in Java?
- Importance of @Override annotation in Java?
- Importance of Thread Priority in Java?
- Importance of XOR operator in Java?
- Importance of yield() method in Java?
- Importance of commenting properly in Java?
- What is return type of db.collection.find() in MongoDB?

Advertisements