Posts

Showing posts from May, 2019

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

be accessed outside of its range. The range checking comes at the price of having a small <br/> amount of memory overhead on each array as well as verifying the index at run time, but the <br/> assumption is that the safety and increased productivity are worth the expense (and Java can <br/> sometimes optimize these operations). <br/> When you create an array of objects, you are really creating an array of references, and each <br/> of those references is automatically initialized to a special value with its own keyword: null. <br/> When Java sees null, it recognizes that the reference in question isn�t pointing to an object. <br/> You must assign an object to each reference before you use it, and if you try to use a reference <br/> that�s still null, the problem will be reported at run time. Thus, typical array errors are <br/> prevented in Java. <br/> You can also create an array of primitives. Again, the compile

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

Everything Is an Object 43�<br/> 3. The heap. This is a general-purpose pool of memory (also in the RAM area) where all <br/> Java objects live. The nice thing about the heap is that, unlike the stack, the compiler <br/> doesn�t need to know how long that storage must stay on the heap. Thus, there�s a <br/> great deal of flexibility in using storage on the heap. Whenever you need an object, you <br/> simply write the code to create it by using new, and the storage is allocated on the <br/> heap when that code is executed. Of course there�s a price you pay for this flexibility: It <br/> may take more time to allocate and clean up heap storage than stack storage (if you <br/> even could create objects on the stack in Java, as you can in C++). <br/> <br/> 4. Constant storage. Constant values are often placed directly in the program code, <br/> which is safe since they can never change. Sometimes constants are c

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 mi

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

reasonable to distribute and update client applications using these technologies, and this can <br/> save considerable time, effort, and money, especially if you need to do frequent updates. <br/> In the Graphical User Interfaces chapter, we will look at one promising new technology, <br/> Macromedia�s Flex, which allows you to create Flash-based applet-equivalents. Because the <br/> Flash Player is available on upwards of 98 percent of all Web browsers (including Windows, <br/> Linux and the Mac) it can be considered an accepted standard. Installing or upgrading the <br/> Flash Player is quick and easy. The ActionScript language is based on ECMAScript so it is <br/> reasonably familiar, but Flex allows you to program without worrying about browser <br/> specifics�thus it is far more attractive than JavaScript. For client-side programming, this is <br/> an alternative worth considering. <br/> .NET and C# <br/&g

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

designers of the Web did not foresee how rapidly this bandwidth would be exhausted for the <br/> kinds of applications people developed. For example, any sort of dynamic graphing is nearly <br/> impossible to perform with consistency because a Graphics Interchange Format (GIF) file <br/> must be created and moved from the server to the client for each version of the graph. In <br/> addition, you�ve no doubt experienced the process of data validation for a Web input form. <br/> You press the submit button on a page; the data is shipped back to the server; the server <br/> starts a CGI program that discovers an error, formats an HTML page informing you of the <br/> error, and then sends the page back to you; you must then back up a page and try again. Not <br/> only is this slow, it�s inelegant. <br/> The solution is client-side programming. Most desktop computers that run Web browsers are <br/> powerful engines capab

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

problem. For example, two processes can�t simultaneously send information to a printer. To <br/> solve the problem, resources that can be shared, such as the printer, must be locked while <br/> they are being used. So a task locks a resource, completes its task, and then releases the lock <br/> so that someone else can use the resource. <br/> Java�s concurrency is built into the language, and Java SE5 has added significant additional <br/> library support. <br/> Java and the Internet <br/> If Java is, in fact, yet another computer programming language, you may question why it is <br/> so important and why it is being promoted as a revolutionary step in computer <br/> programming. The answer isn�t immediately obvious if you�re coming from a traditional <br/> programming perspective. Although Java is very useful for solving traditional standalone <br/> programming problems, it is also important because it sol

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

Introduction to Objects 31�<br/> Where is the data for an object and how is the lifetime of the object controlled? C++ takes the <br/> approach that control of efficiency is the most important issue, so it gives the programmer a <br/> choice. For maximum runtime speed, the storage and lifetime can be determined while the <br/> program is being written, by placing the objects on the stack (these are sometimes called <br/> automatic or scoped variables) or in the static storage area. This places a priority on the <br/> speed of storage allocation and release, and this control can be very valuable in some <br/> situations. However, you sacrifice flexibility because you must know the exact quantity, <br/> lifetime, and type of objects while you�re writing the program. If you are trying to solve a <br/> more general problem such as computer-aided design, warehouse management, or air-traffic <br/> control, this is too rest

Fri, May 31, 2019 4:50:54 PM

Introduction to Objects 29�<br/> The solution to most problems in object-oriented design seems flippant: You create another <br/> type of object. The new type of object that solves this particular problem holds references to <br/> other objects. Of course, you can do the same thing with an array, which is available in most <br/> languages. But this new object, generally called a container (also called a collection, but the <br/> Java library uses that term in a different sense so this book will use �container�), will expand <br/> itself whenever necessary to accommodate everything you place inside it. So you don�t need <br/> to know how many objects you�re going to hold in a container. Just create a container object <br/> and let it take care of the details. <br/> Fortunately, a good OOP language comes with a set of containers as part of the package. In <br/> C++, it�s part of the Standard C++ Library and is often c

Fri, May 31, 2019 4:50:48 PM

If you write a method in Java (as you will soon learn how to do): <br/> void doSomething(Shape shape) { <br/> shape.erase(); <br/> // ... <br/> shape.draw(); <br/> } <br/> This method speaks to any Shape, so it is independent of the specific type of object that it�s <br/> drawing and erasing. If some other part of the program uses the doSomething( ) method: <br/> Circle circle = new Circle(); <br/> Triangle triangle = new Triangle(); <br/> Line line= new Line(); <br/> doSomething(circle); <br/> doSomething(triangle); <br/> doSomething(line); <br/> The calls to doSomething( ) automatically work correctly, regardless of the exact type of <br/> the object. <br/> This is a rather amazing trick. Consider the line: <br/> doSomething(circle); <br/> What�s happening here is that a Circle is being passed into a method that�s expecting a <br/> Shap

Fri, May 31, 2019 4:50:42 PM

<br/> Of course, once you see this design it becomes clear that the base class �cooling system� is not <br/> general enough, and should be renamed to �temperature control system� so that it can also <br/> include heating�at which point the substitution principle will work. However, this diagram <br/> is an example of what can happen with design in the real world. <br/> When you see the substitution principle it�s easy to feel like this approach (pure substitution) <br/> is the only way to do things, and in fact it is nice if your design works out that way. But you�ll <br/> find that there are times when it�s equally clear that you must add new methods to the <br/> interface of a derived class. With inspection both cases should be reasonably obvious. <br/> Interchangeable objects <br/> with polymorphism <br/> When dealing with type hierarchies, you often want to treat an object not as the specific typ

Fri, May 31, 2019 4:50:35 PM

do anything else, the methods from the base-class interface come right along into the derived <br/> class. That means objects of the derived class have not only the same type, they also have the <br/> same behavior, which isn�t particularly interesting. <br/> You have two ways to differentiate your new derived class from the original base class. The <br/> first is quite straightforward: You simply add brand new methods to the derived class. These <br/> new methods are not part of the base-class interface. This means that the base class simply <br/> didn�t do as much as you wanted it to, so you added more methods. This simple and primitive <br/> use for inheritance is, at times, the perfect solution to your problem. However, you should <br/> look closely for the possibility that your base class might also need these additional methods. <br/> This process of discovery and iteration of your design happens regularly in object-

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

Introduction to Objects 21�<br/> (This UML diagram indicates composition with the filled diamond, which states there is one <br/> car. I will typically use a simpler form: just a line, without the diamond, to indicate an <br/> association.5) <br/> Composition comes with a great deal of flexibility. The member objects of your new class are <br/> typically private, making them inaccessible to the client programmers who are using the <br/> class. This allows you to change those members without disturbing existing client code. You <br/> can also change the member objects at run time, to dynamically change the behavior of your <br/> program. Inheritance, which is described next, does not have this flexibility since the <br/> compiler must place compile-time restrictions on classes created with inheritance. <br/> Because inheritance is so important in object-oriented programming, it is often highly <br/> emphasize