Can a Void Method Have a Return Statement

A method returns to the code that invoked information technology when information technology

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

whichever occurs beginning.

You declare a method's return blazon in its method declaration. Within the body of the method, yous apply the return statement to render the value.

Whatsoever method declared void doesn't return a value. It does non need to comprise a render argument, only it may do and then. In such a example, a render statement tin can be used to branch out of a control flow block and go out the method and is simply used like this:

If you try to render a value from a method that is alleged void, you volition get a compiler error.

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

The information blazon of the return value must friction match the method'south declared render blazon; you tin can't return an integer value from a method declared to return a boolean.

The getArea() method in the Rectangle Rectangle grade 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 tin can likewise render 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 summate which bike is      // faster, given each cycle'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 afterwards you take finished the lesson on interfaces and inheritance.

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

The class hierarchy for ImaginaryNumber

The class hierarchy for ImaginaryNumber

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

public Number returnANumber() {     ... }          

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

You lot 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 blazon is immune to vary in the same direction equally the subclass.


Notation: Y'all too can use interface names as render types. In this case, the object returned must implement the specified interface.


downshurs1948.blogspot.com

Source: https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

0 Response to "Can a Void Method Have a Return Statement"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel