Fri, May 31, 2019 4:51:29 PM

Introduction to Objects 39�<br/>
complicated than the equivalent procedural program. Here, you�ll be pleasantly surprised: A <br/>
well-written Java program is generally far simpler and much easier to understand than a <br/>
procedural program. What you�ll see are the definitions of the objects that represent concepts <br/>
in your problem space (rather than the issues of the computer representation) and messages <br/>
sent to those objects to represent the activities in that space. One of the delights of object-<br/>
oriented programming is that, with a well-designed program, it�s easy to understand the code <br/>
by reading it. Usually, there�s a lot less code as well, because many of your problems will be <br/>
solved by reusing existing library code. <br/>
OOP and Java may not be for everyone. It�s important to evaluate your own needs and decide <br/>
whether Java will optimally satisfy those needs, or if you might be better off with another <br/>
programming system (including the one you�re currently using). If you know that your needs <br/>
will be very specialized for the foreseeable future and if you have specific constraints that <br/>
may not be satisfied by Java, then you owe it to yourself to investigate the alternatives (in <br/>
particular, I recommend looking at Python; see www.Python.org). If you still choose Java as <br/>
your language, you�ll at least understand what the options were and have a clear vision of <br/>
why you took that direction. <br/>
<br/>
Everything <br/>
Is an Object <br/>
�If we spoke a different language, we would perceive a somewhat <br/>
different world.� <br/>
Ludwig Wittgenstein (1889-1951) <br/>
<br/>
Although it is based on C++, Java is more of a �pure� object-oriented <br/>
language. <br/>
<br/>
Both C++ and Java are hybrid languages, but in Java the designers felt that the hybridization <br/>
was not as important as it was in C++. A hybrid language allows multiple programming <br/>
styles; the reason C++ is hybrid is to support backward compatibility with the C language. <br/>
Because C++ is a superset of the C language, it includes many of that language�s undesirable <br/>
features, which can make some aspects of C++ overly complicated. <br/>
<br/>
The Java language assumes that you want to do only object-oriented programming. This <br/>
means that before you can begin you must shift your mindset into an object-oriented world <br/>
(unless it�s already there). The benefit of this initial effort is the ability to program in a <br/>
language that is simpler to learn and to use than many other OOP languages. In this chapter <br/>
you�ll see the basic components of a Java program and learn that (almost) everything in Java <br/>
is an object. <br/>
You manipulate objects <br/>
with references <br/>
Each programming language has its own means of manipulating elements in memory. <br/>
Sometimes the programmer must be constantly aware of what type of manipulation is going <br/>
on. Are you manipulating the element directly, or are you dealing with some kind of indirect <br/>
representation (a pointer in C or C++) that must be treated with a special syntax? <br/>
All this is simplified in Java. You treat everything as an object, using a single consistent <br/>
syntax. Although you treat everything as an object, the identifier you manipulate is actually a <br/>
�reference� to an object.1 You might imagine a television (the object) and a remote control <br/>
(the reference). As long as you�re holding this reference, you have a connection to the <br/>
television, but when someone says, �Change the channel� or �Lower the volume,� what you�re <br/>
manipulating is the reference, which in turn modifies the object. If you want to move around <br/>
������������������������������������������������������������<br/>
1 This can be a flashpoint. There are those who say, �Clearly, it�s a pointer,� but this presumes an underlying <br/>
implementation. Also, Java references are much more akin to C++ references than to pointers in their syntax. In the 1st <br/>
edition of this book, I chose to invent a new term, �handle,� because C++ references and Java references have some <br/>
important differences. I was coming out of C++ and did not want to confuse the C++ programmers whom I assumed <br/>
would be the largest audience for Java. In the 2nd edition, I decided that �reference� was the more commonly used term, <br/>
and that anyone changing from C++ would have a lot more to cope with than the terminology of references, so they might <br/>
as well jump in with both feet. However, there are people who disagree even with the term �reference.� I read in one book <br/>
where it was �completely wrong to say that Java supports pass by reference,� because Java object identifiers (according to <br/>
that author) are actually �object references.� And (he goes on) everything is actually pass by value. So you�re not passing <br/>
by reference, you�re �passing an object reference by value.� One could argue for the precision of such convoluted <br/>
explanations, but I think my approach simplifies the understanding of the concept without hurting anything (well, the <br/>
language lawyers may claim that I�m lying to you, but I�ll say that I�m providing an appropriate abstraction). <br/>
�<br/>
the room and still control the television, you take the remote/reference with you, not the <br/>
television. <br/>
Also, the remote control can stand on its own, with no television. That is, just because you <br/>
have a reference doesn�t mean there�s necessarily an object connected to it. So if you want to <br/>
hold a word or sentence, you create a String reference: <br/>
String s; <br/>
But here you�ve created only the reference, not an object. If you decided to send a message to <br/>
s at this point, you�ll get an error because s isn�t actually attached to anything (there�s no <br/>
television). A safer practice, then, is always to initialize a reference when you create it: <br/>
String s = "asdf"; <br/>
However, this uses a special Java feature: Strings can be initialized with quoted text. <br/>
Normally, you must use a more general type of initialization for objects. <br/>
You must create <br/>
all the objects <br/>
When you create a reference, you want to connect it with a new object. You do so, in general, <br/>
with the new operator. The keyword new says, �Make me a new one of these objects.� So in <br/>
the preceding example, you can say: <br/>
String s = new String("asdf"); <br/>
Not only does this mean �Make me a new String,� but it also gives information about how to <br/>
make the String by supplying an initial character string. <br/>
Of course, Java comes with a plethora of ready-made types in addition to String. What�s <br/>
more important is that you can create your own types. In fact, creating new types is the <br/>
fundamental activity in Java programming, and it�s what you�ll be learning about in the rest <br/>
of this book. <br/>
Where storage lives <br/>
It�s useful to visualize some aspects of how things are laid out while the program is running�<br/>
in particular how memory is arranged. There are five different places to store data: <br/>
1. Registers. This is the fastest storage because it exists in a place different from that of <br/>
other storage: inside the processor. However, the number of registers is severely <br/>
limited, so registers are allocated as they are needed. You don�t have direct control, <br/>
nor do you see any evidence in your programs that registers even exist (C & C++, on <br/>
the other hand, allow you to suggest register allocation to the compiler). <br/>
<br/>
2. The stack. This lives in the general random-access memory (RAM) area, but has <br/>
direct support from the processor via its stack pointer. The stack pointer is moved <br/>
down to create new memory and moved up to release that memory. This is an <br/>
extremely fast and efficient way to allocate storage, second only to registers. The Java <br/>
system must know, while it is creating the program, the exact lifetime of all the items <br/>
that are stored on the stack. This constraint places limits on the flexibility of your <br/>
programs, so while some Java storage exists on the stack�in particular, object <br/>
references�Java objects themselves are not placed on the stack. <br/>
<br/>
42 Venkata Pilaka Venkata Pilaka <br/>

Comments

Popular posts from this blog

termux vnc viewer setup

../Settings.jpg

me.html