How to return different data types in Java

A method returns to the code that invoked it when it

  • completes all the statements in the method,
  • reaches a return statement, or
  • throws an exception (covered later),

whichever occurs first.

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.

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like this:

If you try to return a value from a method that is declared void, you will get a compiler error.

Any method that is not declared void must contain a return statement with a corresponding return value, like this:

The data type of the return value must match the method's declared return type; you can't return an integer value from a method declared to return a boolean.

The getArea() method in the Rectangle Rectangle class that was discussed in the sections on objects returns an integer:

// a method for computing the area of the rectangle public int getArea() { return width * height; }

This method returns the integer that the expression width*height evaluates to.

The getArea method returns a primitive type. A method can also return a reference type. For example, in a program to manipulate Bicycle objects, we might have a method like this:

public Bicycle seeWhosFastest(Bicycle myBike, Bicycle yourBike, Environment env) { Bicycle fastest; // code to calculate which bike is // faster, given each bike's gear // and cadence and given the // environment (terrain and wind) return fastest; }

Returning a Class or Interface

If this section confuses you, skip it and return to it after you have finished the lesson on interfaces and inheritance.

When a method uses a class name as its return type, such as whosFastest does, the class of the type of the returned object must be either a subclass of, or the exact class of, the return type. Suppose that you have a class hierarchy in which ImaginaryNumber is a subclass of java.lang.Number, which is in turn a subclass of Object, as illustrated in the following figure.

The class hierarchy for ImaginaryNumber

Now suppose that you have a method declared to return a Number:

public Number returnANumber() { ... }

The returnANumber method can return an ImaginaryNumber but not an Object. ImaginaryNumber is a Number because it's a subclass of Number. However, an Object is not necessarily a Number — it could be a String or another type.

You can override a method and define it to return a subclass of the original method, like this:

public ImaginaryNumber returnANumber() { ... }

This technique, called covariant return type, means that the return type is allowed to vary in the same direction as the subclass.

Note: You also can use interface names as return types. In this case, the object returned must implement the specified interface.

1 answer to this question.

Related Questions In Java

  • All categories
  • Apache Kafka (84)
  • Apache Spark (596)
  • Azure (131)
  • Big Data Hadoop (1,907)
  • Blockchain (1,673)
  • C# (124)
  • C++ (268)
  • Career Counselling (1,060)
  • Cloud Computing (3,356)
  • Cyber Security & Ethical Hacking (145)
  • Data Analytics (1,266)
  • Database (853)
  • Data Science (75)
  • DevOps & Agile (3,500)
  • Digital Marketing (111)
  • Events & Trending Topics (28)
  • IoT (Internet of Things) (387)
  • Java (1,148)
  • Kotlin (3)
  • Linux Administration (384)
  • Machine Learning (337)
  • MicroStrategy (6)
  • PMP (423)
  • Power BI (516)
  • Python (3,154)
  • RPA (650)
  • SalesForce (92)
  • Selenium (1,569)
  • Software Testing (56)
  • Tableau (608)
  • Talend (73)
  • TypeSript (124)
  • Web Development (2,989)
  • Ask us Anything! (66)
  • Others (1,004)
  • Mobile Development (37)

Subscribe to our Newsletter, and get personalized recommendations.

Already have an account? Sign in.

How can I return multiple data types in Java?

We can use following solutions to return multiple values..
If all returned elements are of same type..
If returned elements are of different types..
Using Pair (If there are only two returned values) We can use Pair in Java to return two values..
If there are more than two returned values. ... .
Returning list of Object Class..

Can you return different types in Java?

No. Java methods can only return one result ( void , a primitive, or an object), and creating a struct -type class like this is exactly how you do it.

Can a function return different data types?

No. The return type of a C++ function can only vary based on explicit template parameters or the types of its arguments. It cannot vary based on the value of its arguments.

Can a function have two return types in Java?

No, you don't have two return types. It's a generic method you are seeing.

Chủ đề