Summaries based on Chapter 2.1 from the "Introduction to Programming in Java" textbook to assist in revising for the CS114 tests & exams. For much lower prices, have a look at the bundle of notes for several chapters. More summaries will be added soon :)
Static methods
• A function is a static method
• Implement mathematical functions
• Can have strings & other data types as their range/domain &
can produce side effects (e.g. printing output)
• Whenever you can separate tasks within programs (into
methods), you should do so
• Methods control flow
• Define static methods in a .java file by specifiying
o Method signature
o Sequence of statements that constitute the method
Control flow
• Harmonic has 2 static methods → harmonic() and main()
• First statement that Java executes is the first statement in main()
• “Call” = harmonic(arg)
• Next few statements operate as usual, except that the code
harmonic(arg) causes a “transfer of control” to the first line of
code in harmonic() each time it’s encountered
• Parameter variable n in harmonic() initialized to value of arg in
main() at the time of the call
• Executes statements in harmonic() until it reaches a return
statement → control back to main()
• Return value assigned to variable value
Function-call trace
• Approaches to following control flow
o Imagine each function prints its name &
argument value(s) when it’s called
o Added indentation exposes flow of control &
helps us check each function has the effect
we expect
• Adding calls on StdOut.println() helps understand
what a program is doing (debugging)
• If return value match expectations, we don’t need to
tract the function code in detail → saves us work
1
, Terminology
Static method definition
• Signature = first line of definition
• Also specifies type of each parameter variable & return type of
method
• Return statement transfers control back to point where static
method was called & returns result
• Body may declare local variables (used only in the method
they’re declared in)
Function calls
• Method call = method name + arguments → separated by commas & in
parentheses (same form for math functions)
• Method call is an expression (can use it to build more complicated
expressions)
• Argument is an expression
Multiple arguments
• Static method can take more than one argument
& therefore more than one parameter variable
• Parameter variables are generally different Multiple return statements
types • Control goes back to calling program as soon as
• Type & name of each parameter variable are first return statement is reached
declared in the function signature (declarations • May be multiple return statements → any static
for each variable separated by commas) method returns a single value each time it’s
invoked (value after first return statement
Multiple methods encountered)
• Some programmers insist only having one
• Can have many static methods in a .java file
return statement per method (but not strict in
• Each method has a body with sequence of
this textbook)
statements enclosed in curly braces
• Methods are independent & can be in any order
in the file
• Static method can call any other static method Single return value
in the file/in a Java library • Java method provides only 1 return value to the
caller of type declared in method signature
Overloading • Java data types can contain more info than the
• Static methods with different signatures are value of a single primitive type
different static methods
• Overloading = using same name for different
methods → common practice in Java programs
• Another use = define 2 versions of a method (1
takes an argument & 1 uses a default value for
that argument)
2
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller notesfromaCSmajor. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $2.76. You're not tied to anything after your purchase.