Chapter 2 - Using Objects Study Notes
Chapter Goals
Understand the concepts of classes and objects
Ability to call methods
Learn about arguments and return values
Browse the API documentation
Implement test programs
Understand the difference between objects and object references
Write programs that display simple shapes
Objects and Classes
Objects and Functions: Just as a home builder uses parts (e.g., furnace, water heater) that fulfill specific functions, a programmer builds applications from objects, each with specific behaviors.
In Java, programs are constructed using objects.
Each object has defined behaviors that can be manipulated to achieve specific outcomes.
Using Objects
Definition of Object: An entity in a program that you can manipulate by calling one or more of its methods.
Definition of Method: A sequence of instructions that can access the data of an object.
The instructions of a method are often not visible to the user, but its behavior is clearly defined.
Example:
System.outhas aprintlnmethod that performs a specific action without the user needing to understand its internal workings.
Classes
Definition of Class: A class describes a set of objects with the same behavior.
Examples of String Objects include:
"Hello World"
"Goodbye"
"Mississippi"
All string objects can invoke the same methods.
Example of
System.out: It is a member of thePrintStreamclass, which writes output to the console.Other objects of the
PrintStreamclass can write to different destinations.All
PrintStreamobjects share the methodsprintlnandprint.
Behavior of Different Classes:
Objects of the
PrintStreamclass have different behaviors compared to objects of theStringclass.Responsibilities of Different Classes:
A string object understands its contents, such as the letters it contains.
A string does not have the capability to send itself to a console window or file.
For example, all objects of the
Windowclass share common behaviors
Calling Methods
Using Methods: You interact with an object by calling its methods.
All objects of a specific class share a common set of methods.
Example of
PrintStreamMethods:printlnprint
Example of String Methods
Methods Applicable to All String Objects:
Example:
lengthmethodString initialization:
String greeting = "Hello, World!";Method call:
int numberOfCharacters = greeting.length();Example:
toUpperCasemethodString initialization:
String river = "Mississippi";Method call:
String bigRiver = river.toUpperCase();
Method Arguments
Most methods require input values to perform their tasks.
Technical Term for Inputs: Arguments.
Example using Java: To call
println, the string being printed is an argument:System.out.println(greeting);
Multiple Arguments: Some methods may require more than one argument, while others do not need any arguments at all.
Example of a method that requires no arguments:
length()method of the String class, as it operates on the object itself and doesn't require additional input.
Return Values
Method Actions:
Some methods perform actions and do not return any values, like
println.Other methods compute and return values.
Example: The
length()method returns the count of characters in a string:int numberOfCharacters = greeting.length();
Using Return Values:
The return value computed by one method can serve as an argument for another method:
System.out.println(greeting.length());Here,
greeting.length()returns the integer value13, which is then used as the argument inprintln.
Return Value Examples
Example using
replaceMethod:Implementation:
Assume
String river = "Mississippi";The following statement constructs a new string:
river = river.replace("issipp", "our");
This method:
Replaces all occurrences of "issipp" with "our"
Returns the new String object "Missouri"
Saves the returned value in the variable
riverThe result can also be used in another method call:
System.out.println(river.replace("issipp", "our"));
Details of Method Call to replace
Detailed Analysis of Method Call:
The method call
river.replace("issipp", "our");does the following:Invokes the method on the
Stringobject "Mississippi"Accepts two arguments: "issipp" and "our"
Returns the newly constructed string "Missouri".
Method Arguments and Return Values
Table of Examples
Example Method Call:
System.out.println(greeting)Comments:
greetingis the argument forprintln.
Example Method Call:
greeting.replace("e", "3")Comments: The
replacemethod has two arguments: "e" and "3".
Example Method Call:
greeting.length()Comments: The
lengthmethod does not take arguments.
Assignment Example:
int n = greeting.length();Comments:
lengthreturns an integer value.
Method Call:
System.out.println(n);Comments:
printlndoes not return a value; its return type isvoidper API documentation.
Example Method Chain:
System.out.println(greeting.length());Comments: The return value of a method can be passed as an argument to another method.
Self Check Questions
Self Check 2.3: How can you compute the length of the string "Mississippi"?
Answer:
river.length()or"Mississippi".length()
Self Check 2.4: How can you print out the uppercase version of "Hello, World !"?
Answer:
System.out.println(greeting.toUpperCase());orSystem.out.println("Hello, World!".toUpperCase());
Self Check 2.5: Is it legal to call
river.println()? Why or why not?Answer: Not legal;
riveris aStringand does not haveprintlnas a method.
Self Check 2.6: What are the arguments in the method call
river.replace("p", "s")?Answer: The arguments are the strings "p" and "s".
Self Check 2.7: What is the result of the call
river.replace("p", "s"), whereriveris "Mississippi"?Answer: "Missississi"
Self Check 2.8: What is the result of the call
greeting.replace("World", "Dave").length(), wheregreetingis "Hello, World!"?Answer:
12