The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!

πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

πŸ“±

Unit 2

2.5 Calling a Non-Void Method

2 min readβ€’september 12, 2020

Peter Cao


Void methods are used to complete actions and represent tasks. However, most of the time we want to use a method as part of an expression or stored as a variable. This is what non-void methods are for. In place of a void keyword, there is a return type, which is the type that the method returns for use by the program. Non-void methods can return both primitive and reference data. You can store the results of methods in variables as well as follows:
dataType variableName = methodName(parameterListOptional);
To make a non-void method, we use the following method header format (with static being optional):
public (static) dataType methodName(parameterListOptional)

Returning Integers/Doubles

Sometimes, we want to calculate a value to use in future calculations. This is when we can return an integer or double which will be used in the future. Usually, these methods perform some type of calculation by themselves and return the result of these calculations. The printRectanglePerimeter() method from Topic 2.4 can be written as a non-void method with a few modifications as follows:
public static double rectanglePerimeter(double length, double width) { return 2 * (length + width); some other code }
The most important change is the substitution of the print statement with the return statement. The return statement takes the expression on that line, evaluates it, and returns it to the program that called the method for use. The return statement stops the method execution and ignores any code below the return statement. In the example above, the "some other code" will never be run since it is after the return statement.

Returning Booleans

Methods that return booleans are usually used to show whether a condition is true or not. The method has to do with the evaluation of a boolean expression, which shows whether something is true or not. We will learn more about boolean expressions in Unit 3! When naming boolean-return methods, we commonly name the method using the isCondition() name format to show whether a condition is true or not. Here are some method signatures of methods which return booleans that we can make once we learn more Java:
isPrime(int number) isEven(int number) isLeapYear(int year) isPalindrome(String word)

Returning Strings

Methods that return strings usually deal with string manipulation or string construction. These will have to do with making or modifying new strings. We don't know much about strings yet, but we will learn a lot more about them in the next two topics!

Returning Objects and Other Reference Types

Methods which return objects or other reference types are similar to those which return strings in which they either create new reference data or modify parts of this reference data. We will learn about these in Units 5-8!

Was this guide helpful?

πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
πŸ™
Exam Reviews
πŸ–±
Unit 10: Recursion
βž•
Unit 1: Primitive Types
πŸ–₯
Unit 3: Boolean Expressions and if Statements
πŸ•Ή
Unit 4: Iteration
βš™οΈ
Unit 5: Writing Classes
⌚️
Unit 6: Array
πŸ’Ύ
Unit 7: ArrayList
πŸ’»
Unit 8: 2D Array
πŸ–²
Unit 9: Inheritance
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
πŸ“± Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741
Β© 2021 Fiveable, Inc.