没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
"WPJEFNCBSBTTJOH
00NJTUBLFT
'PPMBSPVOEJO
UIF+BWB-JCSBSZ
)FBE'JSTU
+BWB
-FBSOIPXUISFBET
DBODIBOHFZPVSMJGF
.BLF+BWBDPODFQUT
TUJDLUPZPVSCSBJO
,BUIZ4JFSSB#FSU#BUFT
#FOEZPVSNJOE
BSPVOE
+BWBQV[[MFT
:PVS#SBJOPO+BWB"-FBSOFST(VJEF
ND%DITION#OVERS*AVA
.BLFBUUSBDUJWF
BOEVTFGVM(6*T
ix
i
Intro
Your brain on Java. (EREYOUARETRYINGTOLEARNSOMETHINGWHILEHEREYOURBRAIN
ISDOINGYOUAFAVORBYMAKINGSURETHELEARNINGDOESNTSTICK9OURBRAINSTHINKINGh"ETTER
LEAVEROOMFORMOREIMPORTANTTHINGSLIKEWHICHWILDANIMALSTOAVOIDANDWHETHERNAKED
SNOWBOARDINGISABADIDEAv3OHOWDOYOUTRICKYOURBRAININTOTHINKINGTHATYOURLIFE
DEPENDSONKNOWING*AVA
Who is this book for? xxii
What your brain is thinking xxiii
Metacognition xxv
Bend your brain into submission xxvii
What you need for this book xxviii
Technical editors xxx
Acknowledgements xxxi
Table of Contents (summary)
Intro xxi
1 Breaking the Surface: a quick dip 1
2 A Trip to Objectville: yes, there will be objects 27
3 Know Your Variables: primitives and references 49
4 How Objects Behave: object state affects method behavior 71
5 Extra-Strength Methods: flow control, operations, and more 95
6 Using the Java Library: so you don’t have to write it all yourself 125
7 Better Living in Objectville: planning for the future 165
8 Serious Polymorphism: exploiting abstract classes and interfaces 197
9 Life and Death of an Object: constructors and memory management 235
10 Numbers Matter: math, formatting, wrappers, and statics 273
11 Risky Behavior: exception handling 315
12 A Very Graphic Story: intro to GUI, event handling, and inner classes 353
13 Work on Your Swing: layout managers and components 399
14 Saving Objects: serialization and I/O 429
15 Make a Connection: networking sockets and multithreading 471
16 Data Structures: collections and generics 529
17 Release Your Code: packaging and deployment 581
18 Distributed Computing: RMI with a dash of servlets, EJB, and Jini 607
A Appendix A: Final code kitchen 649
B Appendix B: Top Ten Things that didn’t make it into the rest of the book 659
Index 677
Table of Contents (the full version)
x
You Bet
Shoot Me
2
A Trip to Objectville
I was told there would be objects. )N#HAPTERWEPUTALLOFOURCODE
INTHEMAINMETHOD4HATSNOTEXACTLYOBJECTORIENTED3ONOWWEVEGOTTOLEAVETHAT
PROCEDURALWORLDBEHINDANDSTARTMAKINGSOMEOBJECTSOFOUROWN7ELLLOOKATWHAT
MAKESOBJECTORIENTED//DEVELOPMENTIN*AVASOMUCHFUN7ELLLOOKATTHEDIFFERENCE
BETWEENACLASSANDANOBJECT7ELLLOOKATHOWOBJECTSCANIMPROVEYOURLIFE
1
Breaking the Surface
Java takes you to new places. &ROMITSHUMBLERELEASETOTHEPUBLICASTHE
WIMPYVERSION*AVASEDUCEDPROGRAMMERSWITHITSFRIENDLYSYNTAXOBJECTORIENTED
FEATURESMEMORYMANAGEMENTANDBESTOFALLTHEPROMISEOFPORTABILITY7ELLTAKEAQUICK
DIPANDWRITESOMECODECOMPILEITANDRUNIT7ERETALKINGSYNTAXLOOPSBRANCHINGANDWHAT
MAKES*AVASOCOOL$IVEIN
The way Java works 2
Code structure in Java 7
Anatomy of a class 8
The main() method 9
Looping 11
Conditional branching (if tests) 13
Coding the “99 bottles of beer” app 14
Phrase-o-matic 16
Fireside chat: compiler vs. JVM 18
Exercises and puzzles 20
Method Party()
0 aload_0
1 invokespe-
cial #1 <Method
java.lang.Object()>
4 return
Compiled
bytecode
Virtual
Machines
Chair Wars (Brad the OO guy vs. Larry the procedural guy) 28
Inheritance (an introduction) 31
Overriding methods (an introduction) 32
What’s in a class? (methods, instance variables) 34
Making your fi rst object 36
Using main() 38
Guessing Game code 39
Exercises and puzzles 42
xi
pass-by-value means
pass-by-copy
3
Know Your Variables
Variables come in two flavors: primitive and reference.
4HERESGOTTABEMORETOLIFETHANINTEGERS3TRINGSANDARRAYS7HATIFYOUHAVEA0ET/WNER
OBJECTWITHA$OGINSTANCEVARIABLE/RA#ARWITHAN%NGINE)NTHISCHAPTERWELLUNWRAP
THEMYSTERIESOF*AVATYPESANDLOOKATWHATYOUCANDECLAREASAVARIABLEWHATYOUCANPUT
INAVARIABLEANDWHATYOUCANDOWITHAVARIABLE!NDWELLFINALLYSEEWHATLIFEISTRULYLIKE
ONTHEGARBAGECOLLECTIBLEHEAP
Dog reference
D
o
g
o
b
j
e
c
t
size
24
int
fido
4
How Objects Behave
State affects behavior, behavior affects state. 7EKNOWTHATOBJECTS
HAVESTATEANDBEHAVIORREPRESENTEDBYINSTANCEVARIABLESANDMETHODS.OWWELLLOOK
ATHOWSTATEANDBEHAVIORARERELATED!NOBJECTSBEHAVIORUSESANOBJECTSUNIQUESTATE
)NOTHERWORDSMETHODSUSEINSTANCEVARIABLEVALUES,IKEhIFDOGWEIGHTISLESSTHAN
POUNDSMAKEYIPPYSOUNDELSEv,ETSGOCHANGESOMESTATE
00000111
int
X
00000111
int
Z
copy of x
foo.go(x); void go(int z){ }
Declaring a variable (Java cares about type) 50
Primitive types (“I’d like a double with extra foam, please”) 51
Java keywords 53
Reference variables (remote control to an object) 54
Object declaration and assignment 55
Objects on the garbage-collectible heap 57
Arrays (a fi rst look) 59
Exercises and puzzles 63
Methods use object state (bark different) 73
Method arguments and return types 74
Pass-by-value (the variable is always copied) 77
Getters and Setters 79
Encapsulation (do it or risk humiliation) 80
Using references in an array 83
Exercises and puzzles 88
xii
5
Extra-Strength Methods
Let’s put some muscle in our methods. 9OUDABBLEDWITHVARIABLES
PLAYEDWITHAFEWOBJECTSANDWROTEALITTLECODE"UTYOUNEEDMORETOOLS,IKE
OPERATORS!NDLOOPS-IGHTBEUSEFULTOGENERATERANDOMNUMBERS!NDTURN
A3TRINGINTOANINTYEAHTHATWOULDBECOOL!NDWHYDONTWELEARNITALLBYBUILDING
SOMETHINGREALTOSEEWHATITSLIKETOWRITEANDTESTAPROGRAMFROMSCRATCH
-AYBEA
GAMELIKE3INKA$OT#OMSIMILARTO"ATTLESHIP
6
Using the Java Library
Java ships with hundreds of pre-built classes. 9OUDONTHAVETO
REINVENTTHEWHEELIFYOUKNOWHOWTOFINDWHATYOUNEEDFROMTHE*AVALIBRARYCOMMONLY
KNOWNASTHE
*AVA!0)9OUVEGOTBETTERTHINGSTODO)FYOUREGOINGTOWRITECODEYOU
MIGHTASWELLWRITEONLYTHEPARTSTHATARECUSTOMFORYOURAPPLICATION4HECORE*AVALIBRARY
ISAGIANTPILEOFCLASSESJUSTWAITINGFORYOUTOUSELIKEBUILDINGBLOCKS
!
"
#
$
%
&
'
!SK-ECOM
0ETSCOM
'OCOM
We’re gonna build the
Sink a Dot Com game
h'OODTOKNOWTHERESAN!RRAY,ISTIN
THEJAVAUTILPACKAGE"UTBYMYSELFHOW
WOULD)HAVElGUREDTHATOUTv
- Julia, 31, hand model
Building the Sink a Dot Com game 96
Starting with the Simple Dot Com game (a simpler version) 98
Writing prepcode (pseudocode for the game) 100
Test code for Simple Dot Com 102
Coding the Simple Dot Com game 103
Final code for Simple Dot Com 106
Generating random numbers with Math.random() 111
Ready-bake code for getting user input from the command-line 112
Looping with for loops 114
Casting primitives from a large size to a smaller size 117
Converting a String to an int with Integer.parseInt() 117
Exercises and puzzles 118
Analying the bug in the Simple Dot Com Game 126
ArrayList (taking advantage of the Java API) 132
Fixing the DotCom class code 138
Building the real game (Sink a Dot Com) 140
Prepcode for the real game 144
Code for the real game 146
boolean expressions 151
Using the library (Java API) 154
Using packages (import statements, fully-qualifi ed names) 155
Using the HTML API docs and reference books 158
Exercises and puzzles 161
剩余689页未读,继续阅读
资源评论
coly20032003
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功