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.

How to return different data types in Java

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.

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