Thinking in Java - 4th Edition
1079 pág.

Thinking in Java - 4th Edition


DisciplinaProgramação Orientada A Objetos3.073 materiais48.514 seguidores
Pré-visualização50 páginas
path that allows you to use your existing code base, rather 
than trying to recode your programs in a new language. 
When faced with this bewildering array of solutions to the client-side programming problem, 
the best plan of attack is a cost-benefit analysis. Consider the constraints of your problem 
and what would be the shortest path to your solution. Since client-side programming is still 
programming, it\u2019s always a good idea to take the fastest development approach for your 
particular situation. This is an aggressive stance to prepare for inevitable encounters with the 
problems of program development. 
Server-side programming 
This whole discussion has ignored the issue of server-side programming, which is arguably 
where Java has had its greatest success. What happens when you make a request of a server? 
Most of the time the request is simply \u201cSend me this file.\u201d Your browser then interprets the 
file in some appropriate fashion: as an HTML page, a graphic image, a Java applet, a script 
program, etc. 
A more complicated request to a server generally involves a database transaction. A common 
scenario involves a request for a complex database search, which the server then formats into 
an HTML page and sends to you as the result. (Of course, if the client has more intelligence 
via Java or a scripting language, the raw data can be sent and formatted at the client end, 
which will be faster and less load on the server.) Or you might want to register your name in a 
database when you join a group or place an order, which will involve changes to that 
database. These database requests must be processed via some code on the server side, which 
is generally referred to as server-side programming. Traditionally, server-side programming 
has been performed using Perl, Python, C++, or some other language to create CGI 
programs, but more sophisticated systems have since appeared. These include Java-based 
Web servers that allow you to perform all your server-side programming in Java by writing 
what are called servlets. Servlets and their offspring, JSPs, are two of the most compelling 
reasons that companies that develop Web sites are moving to Java, especially because they 
eliminate the problems of dealing with differently abled browsers. Server-side programming 
topics are covered in Thinking in Enterprise Java at www.MindView.net. 
Despite all this talk about Java on the Internet, it is a general-purpose programming 
language that can solve the kinds of problems that you can solve with other languages. Here, 
Java\u2019s strength is not only in its portability, but also its programmability, its robustness, its 
large, standard library and the numerous third-party libraries that are available and that 
continue to be developed. 
Summary 
You know what a procedural program looks like: data definitions and function calls. To find 
the meaning of such a program, you must work at it, looking through the function calls and 
low-level concepts to create a model in your mind. This is the reason we need intermediate 
representations when designing procedural programs\u2014by themselves, these programs tend 
to be confusing because the terms of expression are oriented more toward the computer than 
to the problem you\u2019re solving. 
Because OOP adds many new concepts on top of what you find in a procedural language, 
your natural assumption may be that the resulting Java program will be far more 
38 Thinking in Java Bruce Eckel 
Introduction to Objects 39 
complicated than the equivalent procedural program. Here, you\u2019ll be pleasantly surprised: A 
well-written Java program is generally far simpler and much easier to understand than a 
procedural program. What you\u2019ll see are the definitions of the objects that represent concepts 
in your problem space (rather than the issues of the computer representation) and messages 
sent to those objects to represent the activities in that space. One of the delights of object-
oriented programming is that, with a well-designed program, it\u2019s easy to understand the code 
by reading it. Usually, there\u2019s a lot less code as well, because many of your problems will be 
solved by reusing existing library code. 
OOP and Java may not be for everyone. It\u2019s important to evaluate your own needs and decide 
whether Java will optimally satisfy those needs, or if you might be better off with another 
programming system (including the one you\u2019re currently using). If you know that your needs 
will be very specialized for the foreseeable future and if you have specific constraints that 
may not be satisfied by Java, then you owe it to yourself to investigate the alternatives (in 
particular, I recommend looking at Python; see www.Python.org). If you still choose Java as 
your language, you\u2019ll at least understand what the options were and have a clear vision of 
why you took that direction. 
Everything 
Is an Object 
\u201cIf we spoke a different language, we would perceive a somewhat 
different world.\u201d 
Ludwig Wittgenstein (1889-1951) 
 
Although it is based on C++, Java is more of a \u201cpure\u201d 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\u2019s 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\u2019s 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\u2019ll 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 
\u201creference\u201d to an object.1 You might imagine a television (the object) and a remote control 
(the reference). As long as you\u2019re holding this reference, you have a connection to the 
television, but when someone says, \u201cChange the channel\u201d or \u201cLower the volume,\u201d what you\u2019re 
manipulating is the reference, which in turn modifies the object. If you want to move around 
                                                            
1 This can be a flashpoint. There are those who say, \u201cClearly, it\u2019s a pointer,\u201d but this presumes an underlying 
implementation. Also, Java references are much more akin to C++ references than to pointers in their syntax. In the 1st 
edition of this book, I chose to invent a new term, \u201chandle,\u201d because C++ references and Java references have some 
important differences. I was coming out of C++ and did not want to confuse the C++ programmers whom I assumed 
would be the largest audience for Java. In the 2nd edition, I decided that \u201creference\u201d was the more commonly used term, 
and that anyone changing from C++ would have a lot more to cope with than the terminology of references, so they might 
as well jump in with both feet. However, there are people who disagree even with the term \u201creference.\u201d I read in one book 
where it was \u201ccompletely wrong to say that Java supports pass by reference,\u201d because Java object identifiers (according to 
that author) are actually