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

Thinking in Java - 4th Edition

Disciplina:Programação Orientada a Objetos1.261 materiais32.118 seguidores
Pré-visualização50 páginas
with errors ............... 31 
Concurrent programming ... 32 
Java and the Internet .......... 33 

What is the Web? ......................... 33 

Client-side programming ............ 34 
Server-side programming ............ 38 

Summary .............................. 38 

Everything Is an Object 41 
You manipulate objects

with references ..................... 41 
You must create

all the objects ....................... 42 
Where storage lives ...................... 42 
Special case: primitive types ....... 43 
Arrays in Java .............................. 44 

You never need to

destroy an object .................. 45 
Scoping ........................................ 45 
Scope of objects ........................... 46 

Creating new data types:

class ..................................... 46 
Fields and methods ..................... 47 

Methods, arguments,

and return values ................. 48 
The argument list ......................... 49 

Building a Java program ...... 50 
Name visibility ............................. 50 
Using other components ............. 50 
The static keyword ..................... 51 

Your first Java program ....... 52 
Compiling and running ............... 54 

Comments and embedded

documentation ..................... 55 
Comment documentation ............ 55 
Syntax .......................................... 56 
Embedded HTML ........................ 56 
Some example tags ...................... 57 
Documentation example ............. 59 

Coding style .......................... 60 
Summary .............................. 60 
Exercises .............................. 60 

Operators 63 
Simpler print statements ..... 63 
Using Java operators ........... 64 
Precedence ........................... 64 
Assignment .......................... 65 

Aliasing during method calls ....... 66 
Mathematical operators....... 67 

Unary minus

and plus operators ....................... 68 

Auto increment and

decrement ............................ 69 
Relational operators ............ 70 

Testing object equivalence ........... 70 
Logical operators .................. 71 

Short-circuiting ............................ 72 
Literals .................................. 73 

Exponential notation ................... 74 
Bitwise operators .................. 75 
Shift operators ......................76 
Ternary if-else operator ......79 
String operator

+ and += .............................. 80 
Common pitfalls

when using operators ........... 81 
Casting operators .................. 81 

Truncation and rounding ........... 82 
Promotion ................................... 83 

Java has no “sizeof” ............. 83 
A compendium

of operators .......................... 84 
Summary ............................... 91 

Controlling Execution 93 
true and false..................... 93 
if-else .................................. 93 
Iteration ............................... 94 

do-while ..................................... 95 
for ................................................ 95 
The comma operator................... 96 

Foreach syntax ......................97 
return ................................. 99 
break and continue .......... 99 
The infamous “goto” ........... 101 
switch ................................104 
Summary ............................ 106 

Initialization & Cleanup 107 
Guaranteed initialization

with the constructor ........... 107 
Method overloading .......... 109 

Distinguishing

overloaded methods .................. 110 
Overloading with primitives ....... 111 
Overloading on return values .... 114 

Default constructors ........... 114 
The this keyword ............... 116 

Calling constructors

from constructors ...................... 118 
The meaning of static ............... 119 

Cleanup: finalization

and garbage collection ........ 119 

What is finalize() for? ............. 120 
You must perform cleanup ......... 121 
The termination condition ......... 121 
How a garbage collector works .. 122 

Member initialization ......... 125 
Specifying initialization ............. 126 

Constructor initialization ... 127 
Order of initialization ................ 127 
static data initialization ........... 128 
Explicit static initialization ...... 130 
Non-static

instance initialization ................ 132 
Array initialization ............. 133 

Variable argument lists ............. 137 
Enumerated types ............... 141 
Summary ............................ 143 

Access Control 145 
package:

the library unit ................... 146 
Code organization ...................... 147 
Creating unique

package names ........................... 148 
A custom tool library .................. 151 
Using imports

to change behavior ..................... 152 
Package caveat ........................... 153 

Java access specifiers .......... 153 
Package access ........................... 153 
public: interface access ............ 154 
private: you can’t touch that! .. 155 
protected: inheritance access . 156 

Interface

and implementation .......... 158 
Class access ........................ 159 
Summary ............................ 162 

Reusing Classes 165 
Composition syntax ........... 165 
Inheritance syntax ............. 168 

Initializing the base class ........... 169 
Delegation ........................... 171 
Combining composition

and inheritance ................... 173 
Guaranteeing proper cleanup .... 174 
Name hiding ............................... 177 

Choosing composition

vs. inheritance .................... 178 
protected ......................... 180 
Upcasting ............................ 181 

Why “upcasting”? ...................... 181 
Composition vs. inheritance

revisited ..................................... 182 
The final keyword ............. 182 

final data ................................... 183 

final methods ............................ 186 
final classes ............................... 187 
final caution .............................. 188 

Initialization

and class loading ................ 189 
Initialization with inheritance ... 189 

Summary ............................. 191 

Polymorphism 193 
Upcasting revisited ............. 193 

Forgetting the object type .......... 194 
The twist ............................. 196 

Method-call binding .................. 196 
Producing the right behavior ..... 196 
Extensibility ............................... 199 
Pitfall: “overriding”

private methods ...................... 202 
Pitfall: fields

and static methods .................. 203 
Constructors and

polymorphism ................... 204 
Order of constructor calls ......... 204 
Inheritance and cleanup ........... 206 
Behavior of polymorphic

methods inside constructors .... 210 
Covariant return types ........ 211 
Designing

with inheritance .................. 212 
Substitution vs. extension ......... 213 
Downcasting and

runtime type information ......... 215 
Summary ............................. 217 

Interfaces 219 
Abstract classes

and methods ....................... 219 
Interfaces ........................... 222 
Complete decoupling ......... 225 
“Multiple inheritance”

in Java ................................ 230 
Extending an interface with
inheritance .......................... 231 

Name collisions when

combining Interfaces ................233 
Adapting to an interface .... 234 
Fields in interfaces ............ 235 

Initializing fields in interfaces .. 236 
Nesting interfaces .............. 237 
Interfaces and factories ..... 239 
Summary ............................. 241 

Inner Classes 243 
Creating inner classes ........ 243 

The link to

the outer class .................... 244 
Using .this and .new ........ 246 
Inner classes

and upcasting ..................... 247 
Inner classes in

methods and scopes ........... 249 
Anonymous

inner classes ........................ 251 
Factory Method revisited .......... 254