Calling Methods that Return Values
Overview: 2.5.0
- all methods have
return types
void
means it doesn’t return anything- others
return
a value back
Get methods: 2.5.1
- return the value of instance variables
getWidth()
gets the value of width
- you need to do something with the value returned from this variable
toString: 2.5.2
toString
is a common method which converts an object to a string- not mentioned this method is inherited from
java.lang.Object
, so every object has this
- not mentioned this method is inherited from
Methods with Arguments and Return Values: 2.5.3
- Methods with return values and arguments are like math functions
- given an input, they produce an output
return statement
returns the value indicated in the return typereturn 5;
returns the integer 5
Summary: 2.5.5
- some methods
return
values - the return value of a method must be stored in a variable or used in an expression
- return types have data types
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.