2.0 - 2.3
Objects and Primitive Data
This chapter explores the key elements that we use in a program: objects and primitive data. We learn to create and use objects, which is basic to writing any program in an object-oriented language such as Java. We use objects to work with character strings, get information from the user, do difficult calculations, and format output. In the Graphics Track of this chapter, we explore the relationship between Java and the Web, and delve into Java's abilities to work with color and draw shapes.
Chapter Objectives
Define the difference between primitive data and objects.
Declare and use variables.
Perform mathematical computations.
Create objects and use them.
Explore the difference between a Java application and a Java applet.
Create graphical programs that draw shapes.
2.0 An Introduction to Objects
Java is an object-oriented language that requires a solid understanding of key principles:
Object: A basic part of a Java program that represents a real-world entity, such as a bank account.
Attribute: The characteristics that define the object's state, such as the current balance of a bank account.
Method: A named group of programming statements that defines the behaviors or actions of an object.
Class: The blueprint or model from which objects are created; defines data types and methods for those objects.
Encapsulation: The principle of restricting access to an object's internal state; modifications should be through its methods.
Inheritance: The mechanism for creating new classes from existing ones.
Polymorphism: The ability to refer to objects of different but related types in a uniform way.
Overview of Objects
State and Behaviors: An object has:
State: Basic characteristics defining the object (e.g., a bank account's current balance).
Behaviors: Activities associated with the object (e.g., making deposits and withdrawals).
Primitive Data: Common values such as numbers and characters, distinguished by data types.
Data Type: Defines a set of values and the operations that can be performed on those values (e.g., addition using the operator
+).
Objects usually represent something complex, containing primitive values as attributes (e.g., a bank account object storing the account balance as a numeric value).
Methods in Context
A method is used to encapsulate behavior. For example, a method to deposit money to a bank account updates the account balance accordingly.
A class serves as the definition for creating objects of that type (e.g., a class representing bank accounts). Each object created from the class has its own state and can manage its own information.
Encapsulation Example
Objects protect their internal information, allowing changes only through their own methods.
For example, operations like withdrawing money from a bank account are managed through the methods defined in the bank account class.
Inheritance and Polymorphism
Inheritance enables classes to be derived from existing classes, facilitating code reuse and organization of common characteristics into high-level classes and specific differences into child classes.
Polymorphism simplifies handling multiple object types through a single interface.
Object-Oriented Software Principles
The concepts of classes, objects, encapsulation, inheritance, and polymorphism comprise the foundation of object-oriented programming in Java.
2.1 Using Objects
Example of Using an Object
In the Lincoln program (from Chapter 1), a method was invoked through an object:
System.out.println("Whatever you are, be a good one.");
The
System.outobject represents an output device (the monitor screen), while theprintlnmethod is a behavior of that object, sending a message to print the string of characters.
Parameters
Each piece of data sent to a method is called a parameter.
The
printlnmethod takes one parameter: the string to be printed.
Print and Println Methods
The difference:
printlnprints the information and moves to the next line.printprints without moving to the next line.
Countdown Program Example
Listing 2.1 demonstrates both methods:
public class Countdown {
public static void main (String[] args) {
System.out.print("Three... ");
System.out.print("Two... ");
System.out.print("One... ");
System.out.print("Zero... ");
System.out.println("Liftoff!");
System.out.println("Houston, we have a problem.");
}
}
This prints:
Three … Two … One … Zero … Liftoff!
Houston, we have a problem.
Abstraction
An object is an abstraction: the details of how it operates are not relevant to the user.
Example of abstraction: driving a car without knowing how the engine functions.
Abstractions help manage complexity by allowing us to focus on relevant operations without needing to understand detailed processes.
2.2 String Literals
Character Strings: In Java, a string is an object defined by the class
String. A string literal is enclosed in double quotation marks.
String Concatenation
Concatenation: The
+operator adds one string to another (e.g., strings can be combined without splitting lines).Example (in Listing 2.2):
System.out.println("We present the following facts for your " + "extracurricular edification: ");
Numeric values can be concatenated to strings automatically (e.g.,
672concatenated with a string will convert to a string).
Escape Sequences
Escape sequences allow inclusion of special characters in strings:
\nfor newline\tfor tab`\