Tuesday, July 28, 2020

Java Return Type

Return Type                

The type of data that a method returns-

  • A method can return or give a value.
  • Consider this mathematical function: f(x) = x+1
    •    x is a parameter to this function.
    •  x+1 is the return value of this function.
    •  This function returns a number (return type).
  •  The same thing applies for methods or functions in programming.
    • A method can return a value.

Example:

1)    getUserName();

 Get the name of the user and return the value.

  The return type is a text (String).

 

2)    getUserAge();

Get the age of the user and return the value.

The return type is a number (int, double, …..).

 

The void return type

main() has a void return type.

  • void means nothing.
  • main does not return a value.

Example:

printUserName();

  •    we just want to print the name of the user.
  •    We do not want to get any value from this method.
  •   The return type is void.

Note that every method has a specific role—

1)    getUserName();
2)    printUserName();

No comments:

Post a Comment