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

Thinking in Java - 4th Edition

Disciplina:Programação Orientada a Objetos1.262 materiais32.145 seguidores
Pré-visualização50 páginas
Nested classes .................... 256 
Classes inside interfaces ............ 257 
Reaching outward from

a multiplynested class ............... 259 
Why inner classes? ............. 259 

Closures & callbacks .................. 261 
Inner classes &

control frameworks ................... 263 
Inheriting from

inner classes ....................... 269 
Can inner classes

be overridden? ................... 269 
Local inner classes .............. 271 
Inner-class identifiers ........ 272 
Summary ............................ 273 

Holding Your Objects 275 
Generics and

type-safe containers ........... 276 
Basic concepts .................... 278 
Adding groups

of elements ......................... 279 
Printing containers ............ 281 
List ..................................... 283 
Iterator ............................. 286 

ListIterator ............................ 288 
LinkedList ....................... 289 
Stack ................................. 291 
Set ...................................... 292 
Map ................................... 295 
Queue ................................ 298 

PriorityQueue ........................ 299 
Collection vs. Iterator ... 301 
Foreach and iterators ......... 304 

The Adapter Method idiom ...... 306 
Summary ............................ 308 

Error Handling
with Exceptions 313 

Concepts ............................. 313 

Basic exceptions.................. 314 
Exception arguments ................. 315 

Catching an exception ........ 315 
The try block ............................. 316 
Exception handlers .................... 316 

Creating your

own exceptions ................... 317 
Exceptions and logging .............. 319 

The exception

specification ....................... 322 
Catching any exception ..... 323 

The stack trace .......................... 324 
Rethrowing an exception ........... 325 
Exception chaining .................... 327 

Standard Java

exceptions .......................... 330 
Special case:

RuntimeException ............... 330 
Performing cleanup

with finally ....................... 332 
What’s finally for? .................... 333 
Using finally during return .... 335 
Pitfall: the lost exception .......... 336 

Exception restrictions ....... 338 
Constructors ...................... 340 
Exception matching ........... 344 
Alternative approaches ...... 345 

History ...................................... 346 
Perspectives ............................... 347 
Passing exceptions

to the console ............................ 349 
Converting checked

to unchecked exceptions ........... 350 
Exception guidelines ......... 352 
Summary ............................ 352 

Strings 355 
Immutable Strings ............355 
Overloading ‘+’ vs.

StringBuilder ................. 356 
Unintended recursion ....... 359 
Operations on Strings ....... 361 
Formatting output ............. 362 

printf() .................................... 363 
System.out.format() ............ 363 
The Formatter class ............... 363 
Format specifiers ...................... 364 
Formatter conversions ........... 366 
String.format() ..................... 368 

Regular expressions ........... 370 
Basics .........................................370 
Creating regular expressions ..... 372 
Quantifiers ................................. 374 
Pattern and Matcher ............. 375 

split() ........................................382 
Replace operations .................... 383 
reset() .......................................384 
Regular expressions

and Java I/O .............................. 385 
Scanning input ................... 386 

Scanner delimiters ................. 388 
Scanning with

regular expressions ................... 389 
StringTokenizer ............. 389 
Summary ............................ 391 

Type Information 393 
The need for RTTI .............. 393 
The Class object ................ 395 

Class literals ............................... 399 
Generic class references ............ 401 
New cast syntax ........................ 403 

Checking before a cast ....... 404 
Using class literals .................... 409 
A dynamic instanceof .............. 411 
Counting recursively .................. 412 

Registered factories ........... 413 
instanceof vs. Class

equivalence......................... 416 
Reflection: runtime

class information ................ 417 
A class method extractor ........... 418 

Dynamic proxies ................ 420 
Null Objects ........................ 424 

Mock Objects & Stubs ................ 429 
Interfaces and

type information ................ 430 
Summary ............................ 436 

Generics 439 
Comparison with C++ ........ 440 
Simple generics .................. 440 

A tuple library ............................ 442 
A stack class ............................... 444 
RandomList ............................ 445 

Generic interfaces .............. 446 
Generic methods ................ 449 

Leveraging type

argument inference ...................450 
Varargs and generic methods .... 452 
A generic method

to use with Generators............ 453 
A general-purpose Generator . 453 
Simplifying tuple use ................. 455 
A Set utility................................ 456 

Anonymous

inner classes ....................... 459 

Building

complex models ................. 460 
The mystery of erasure ...... 462 

The C++ approach .................... 464 
Migration compatibility ............ 466 
The problem with erasure ......... 467 
The action at the boundaries .... 468 

Compensating

for erasure ........................... 471 
Creating instances of types ........ 472 
Arrays of generics ...................... 475 

Bounds ............................... 479 
Wildcards ........................... 482 

How smart is the compiler? ...... 484 
Contravariance .......................... 485 
Unbounded wildcards ............... 488 
Capture conversion ................... 492 

Issues ................................. 493 
No primitives

as type parameters .................... 493 
Implementing

parameterized interfaces ........... 495 
Casting and warnings ............... 496 
Overloading ............................... 498 
Base class hijacks an interface .. 498 

Self-bounded types ............ 500 
Curiously recurring generics .... 500 
Self-bounding ............................ 501 
Argument covariance ................ 503 

Dynamic type safety .......... 506 
Exceptions ......................... 507 
Mixins ................................ 509 

Mixins in C++ ........................... 509 
Mixing with interfaces ............... 510 
Using the Decorator pattern ....... 511 
Mixins with dynamic proxies .... 512 

Latent typing ....................... 514 
Compensating for

the lack of latent typing ...... 518 
Reflection ................................... 518 
Applying a method

to a sequence .............................. 519 
When you don’t happen

to have the right interface .......... 521 
Simulating latent typing

with adapters ............................. 523 
Using function objects

as strategies ....................... 526 
Summary: Is casting

really so bad? ...................... 531 
Further reading .......................... 533 

Arrays 535 
Why arrays are special ........535 

Arrays are

first-class objects ............... 536 
Returning an array ............. 539 
Multidimensional

arrays .................................. 540 
Arrays and generics ........... 543 
Creating test data ............... 546 

Arrays.fill() ............................. 546 
Data Generators ...................... 547 
Creating arrays

from Generators ..................... 551 
Arrays utilities .................. 555 

Copying an array ........................ 555 
Comparing arrays ...................... 556 
Array element comparisons ...... 557 
Sorting an array .........................560 
Searching a sorted array ............