Comp 110 Return Values

public String getCoolHat(){
    return "CPUHat";

Return values the values that are returned from a method. In the example above, the return type of the method is String, so the return value of the method must be of type String as well.

You can see that the return value of the above method is "CPUHat", which is of type String, so it is a valid return value for the getCoolHat() Method.

If you were to do something like:

public int getAge(){
    return "I am 18 years old";//Wrong!!!

The method would be invalid, as the return type in the method signature is int, and the return type of the return value is String.

public Emoji getEmoji(){//Emoji is a class we defined!
    return new Emoji();

Notice here that I returned a new instance of the Emoji class. You do not have to only return a variable, you can do many things in a return statement. For example:

public int addThreeNumbers(int x, int y, int z){
    return x + y + z;

In short, a method must return a value that is of the same type as the return type declared by the method.