Everything is an object

“If we spoke a different language, we would perceive a somewhat
Different world.”
Ludwig Wittgenstein (1889-1951)

Although it is based on C++, Java is more of a “pure” object-oriented
Language.

Both C++ and Java are hybrid languages, but in Java the designers felt that the hybridization
Was not as important as it was in C++. A hybrid language allows multiple programming
Styles; the reason C++ is hybrid is to support backward compatibility with the C language.
Because C++ is a superset of the C language, it includes many of that language’s undesirable
Features, which can make some aspects of C++ overly complicated.

The Java language assumes that you want to do only object-oriented programming. This
Means that before you can begin you must shift your mindset into an object-oriented world
(unless it’s already there). The benefit of this initial effort is the ability to program in a
Language that is simpler to learn and to use than many other OOP languages. In this chapter
You’ll see the basic components of a Java program and learn that (almost) everything in Java
Is an object.
You manipulate objects
With references
Each programming language has its own means of manipulating elements in memory.
Sometimes the programmer must be constantly aware of what type of manipulation is going
On. Are you manipulating the element directly, or are you dealing with some kind of indirect
Representation (a pointer in C or C++) that must be treated with a special syntax?
All this is simplified in Java. You treat everything as an object, using a single consistent
Syntax. Although you treat everything as an object, the identifier you manipulate is actually a
“reference” to an object.1 You might imagine a television (the object) and a remote control
(the reference).

As long as you’re holding this reference, you have a connection to the
Television, but when someone says, “Change the channel” or “Lower the volume,” what you’re
Manipulating is the reference, which in turn modifies the object. If you want to move around
The room and still control the television, you take the remote/reference with you, not the
Television.
Also, the remote control can stand on its own, with no television. That is, just because you
Have a reference doesn’t mean there’s necessarily an object connected to it. So if you want to
Hold a word or sentence, you create a String reference:

String s;

But here you’ve created only the reference, not an object. If you decided to send a message to
S at this point, you’ll get an error because s isn’t actually attached to anything (there’s no
Television). A safer practice, then, is always to initialize a reference when you create it:

String s = “asdf”;

However, this uses a special Java feature: Strings can be initialized with quoted text.
Normally, you must use a more general type of initialization for objects.


1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)



Everything is an object