Sub topic 16
Calls, Object Creation, and Expression Statements
Java expressions can do more than calculate numbers. They can call methods and create objects. Some expressions can also stand on their own as statements.
Calling a method
A method is a named set of instructions that performs a task. To use it, you write a method call: the method’s name followed by parentheses. An argument is a value you put inside the parentheses for the method to use.
For example, message.append("!") calls the append method on message. It gives the method the quoted text "!" as an argument.
Some methods return a value, meaning they give a result back to the caller. You can save that result or use it in another expression. Other methods, such as System.out.println, return no value. You can call them to perform a task, but you cannot save a result from them.
Creating an object
An object is a piece of data that can have its own behavior. A class describes a kind of object. Java’s StringBuilder class describes objects that hold text you can change.
To create an object, use new followed by a class name and parentheses. The parentheses can contain arguments for a constructor. A constructor is the part of a class that sets up a new object.
new StringBuilder("Hi")This expression creates a StringBuilder with the text "Hi". It produces a reference, which is a way to refer to the new object. You can save that reference in a variable and use it later.
Which expressions can be statements?
A statement is an instruction in a Java program. An expression statement is an expression used as a statement, followed by a semicolon. Java allows these forms as expression statements:
- A method call, such as
message.append("!"). - Object creation, such as
new StringBuilder("unused"). - An assignment, such as
count = 2. - An increment or decrement, such as
count++.
Not every expression can stand alone. For example, count + 1 calculates a value, but count + 1; is not a valid Java statement. You could save the result instead.
Try it
Save this program as Main.java, then compile and run it.
public class Main {
public static void main(String[] args) {
StringBuilder message = new StringBuilder("Hi");
message.append("!");
int size = message.length();
System.out.println(message.toString());
System.out.println(size);
message.length();
new StringBuilder("unused");
int count = 1;
count = count + 1;
count++;
System.out.println(count);
// count + 1; is not a valid statement.
}
}The program prints Hi!, then 3, then 3, each on its own line. The append call changes the text in message. The length call returns the length of that text, which the program first saves in size.
Later, message.length(); is valid, but its result is not saved or printed. Similarly, the program creates another StringBuilder without saving its reference. Both statements are allowed, but neither is useful here.
Exercise
Exercise
Why is message.length(); valid while count + 1; is not? How could you use the result of message.length()?
Show answer
A method call can be an expression statement, but a calculation with + cannot. You could save the result with int length = message.length();.