Using Objects and Methods
Java programs build results by evaluating typed expressions, updating variables, and calling methods on classes and objects.
- Exam weight
- 15–25%
- Topics
- 15
Introduction
Java programs build results by evaluating typed expressions, updating variables, and calling methods on classes and objects.
This unit establishes the execution model used everywhere else in the course. Trace one statement at a time: evaluate inputs, identify the result type, update state, and record output. Keep primitive values separate from references, and keep references separate from the objects they identify.
algorithm → source → compilation → execution
variables, expressions, assignment, casts, and input
signatures, arguments, return values, and APIs
identity, construction, aliases, instance methods, and immutable strings
Java patterns and contracts
Use each pattern only after checking its type, range, and object-state conditions.
Introduction to Algorithms, Programming, and Compilers
How does a Java algorithm become a running program?
A programmer expresses an algorithm as source code; the compiler checks and translates it, and the runtime executes the resulting bytecode.
Variables and Data Types
What does a declared Java variable promise about its values?
A declaration gives a variable a name and a type, which limits the values and operations Java permits for that storage location.
Expressions and Output
How do types and operator order determine a Java expression?
Java evaluates operators by precedence and associativity, while operand types determine whether arithmetic is integral, floating-point, or string concatenation.
Assignment Statements and Input
How do assignment and input change program state?
Assignment evaluates the right side using the old state and stores that result on the left; input supplies values from outside the program that must be interpreted before use.
Casting and Range of Variables
What information can a cast change or lose?
A cast explicitly converts a value to another type; narrowing can discard information, and fixed-width integer arithmetic can overflow.
Compound Assignment Operators
What sequence is hidden inside a compound assignment?
A compound assignment reads the current left-side value, applies an operator with the right side, converts as required, and stores the result back.
Application Program Interface (API) and Libraries
How does an API let code use behavior without knowing its implementation?
An API publishes method contracts—names, parameters, return types, and behavior—so callers can use a library through its documented interface.
Documentation with Comments
Which information belongs in a useful program comment?
Useful comments explain purpose, contracts, assumptions, and non-obvious choices that the executable code cannot express clearly on its own.
Method Signatures
How does Java decide which method a call means?
Java matches the method name and ordered parameter types to an applicable signature, then uses the declared return type to type the result.
Calling Class Methods
What happens when a program calls a static method?
A class-method call evaluates its arguments, initializes a new set of parameter variables, executes the method body, and substitutes the returned value.
Math Class
Which mathematical operations does the Math class provide, and what ranges do they return?
Math supplies static numerical methods; correct use depends on each signature, return type, and range contract.
Objects: Instances of Classes
How are an object, its class, and a reference related?
A class defines a type and its behavior; an object is one runtime instance with its own state, and a reference is a value that can identify that object.
Object Creation and Storage (Instantiation)
What exactly changes when `new` creates an object?
new selects the constructor whose signature matches the arguments, initializes a distinct object, and produces a reference that can be stored or passed.
Calling Instance Methods
How does the receiver affect an instance-method call?
The reference before the dot selects the receiver object, whose state the method can read or modify while its arguments initialize separate parameters.
String Manipulation
How do zero-based indices and immutability shape String operations?
A String is an immutable sequence indexed from zero; methods inspect it or return new strings while the original object remains unchanged.
Using objects and methods review
Trace short Java programs without blending compilation, expression evaluation, reference identity, and output.
State and types
- Identify each variable's declared type and current value.
- Evaluate integer arithmetic before widening the result.
- Place casts at the operation whose behavior must change.
Calls and objects
- Match actual arguments to a method signature.
- Separate a reference variable from the object it identifies.
- Treat String operations as producing new values rather than changing the original string.
Debugging
- Classify errors as compile-time, runtime, or logical.
- Check index and substring bounds before a call.
- Record output only when the print statement executes.