Java Reference
In-Depth Information
methods that operate on that data. Rather than completely separating the basic opera-
tions from the data, it seemed to make sense to include them together. This packaging
of data and operations into one entity is the central idea behind objects. An object
stores some data and has methods that act on its data.
Object
A programming entity that contains state (data) and behavior (methods).
As we said in Chapter 1, classes are the basic building blocks of Java programs.
But classes also serve another purpose: to describe new types of objects.
Class
A category or type of object.
When it is used this way, a class is like a blueprint of what the object looks like.
Once you've given Java the blueprint, you can ask it to create actual objects that
match that blueprint. We sometimes refer to the individual objects as instances of the
class. We tend to use the words “instance” and “object” interchangeably.
This concept is difficult to understand in the abstract. To help you come to grips
with what it means and how it works, we'll look at several different classes. In keep-
ing with our idea of focusing on fundamental concepts first, in this chapter we'll
study how to use existing objects that are already part of Java, but we aren't going to
study how to define our own new types of objects just yet. We'll get to that in
Chapter 8, after we've had time to practice using objects.
Using objects differs from using primitive types, so we'll have to introduce some new
syntax and concepts. It would be nice if Java had a consistent model for using all types
of data, but it doesn't. Consequently, if you want to understand how your programs
operate, you'll have to learn two sets of rules: one for primitives and one for objects.
String Objects
String objects are one of the most useful and most commonly used types of objects
in Java, so they make a good starting point. They aren't the best example of objects,
though, because there are a lot of special rules that apply only to strings. In the next
section, we'll look at a more typical kind of object.
One special property of String objects is that there are literals that represent them
(string literals). We've been using them in println statements since Chapter 1. What
we haven't discussed is that these literal values represent objects of type String
(instances of the String class). For example, in the same way that you can say
int x = 8;
you can say
String s = "hello there";
 
Search WWH ::




Custom Search