Introduction to Programming with Java · Web view957. Глава 5. Условни...
-
Upload
truonghanh -
Category
Documents
-
view
226 -
download
0
Transcript of Introduction to Programming with Java · Web view957. Глава 5. Условни...
Introduction to Programming with Java
2
15
35
1. 95
2. 127
3. 149
4. 171
5. 201
6. 217
7. 235
8. 257
9. 281
10. 333
11. 363
12. 389
13. 433
14. 475
15. 559
16. 579
17. 613
18. , - 657
19. 693
20. - 727
21. 771
22. ?819
23. 30.09.2005 .867
24. 8.04.2006 .911
25. 11.12.2005 .937
959
Java
, 2008
Java
(), 2008 .
:
1. :
;
- , , , ;
;
, , .
2. :
, .
, ;
, , .
:
:
http://www.introprogramming.info
ISBN 978-954-400-055-4
()
,
:
( Java, C# C++)
:
Java Software Engineer / Java Web Developer
.NET Software Engineer / ASP.NET Web Developer
C++ Software Engineer / Embedded C Developer
PHP & MySQL Web Developer
,
!
350
SAP, VMware, Telerik, Johnson Controls, Sirma Group, Software A.G., Seeburger Informatik, ICB, EGT,
http://academy.devbg.org
www.devbg.org
() , .
.
, .
http://itboxing.devbg.org
"IT Boxing " " - ". , ( .NET Java), - , , .
, . , . .
SAP
SAP 25 .
SAP Labs Bulgaria
SAP Labs Bulgaria 2000 SAP Labs Network . SAP Netweaver, , SAP .
SAP Java. SAP , Java (Java Enterprise Edition, SCA, SOA, BPM, ESB ..). , NW Java Application Server Java EE 5 .
, .
:
www.sap.com/bulgaria
www.citagency.eu
() .
, , , .
()
,
:
( Java, C# C++)
:
Java Software Engineer / Java Web Developer
.NET Software Engineer / ASP.NET Web Developer
C++ Software Engineer / Embedded C Developer
PHP & MySQL Web Developer
,
!
350
SAP, VMware, Telerik, Johnson Controls, Sirma Group, Software A.G., Seeburger Informatik, ICB, EGT,
http://academy.devbg.org
2
15
35
?35
36
?37
?38
?38
Java?39
Java39
Java 40
Java?41
Java 6 Eclipse 3.441
?42
!42
?43
?43
Google43
Microsoft44
()44
?44
!45
45
" "46
?46
50
?51
51
52
54
62
63
64
1. 64
2. 64
3. 64
4. 64
5. 65
6. 65
7. 65
8. 65
9. 66
10. 66
11. 66
12. 66
13. 66
14. 67
15. 67
16. 67
17. 67
18. , - 68
19. 68
20. - 68
21. 69
22. ?69
23, 24, 25. 69
69
?70
70
71
71
72
73
73
(Tsachev)73
74
74
74
74
75
75
75
76
76
77
77
77
77
77
79
79
79
79
80
80
ProSyst Labs83
VMWare84
Google86
SAP Labs Bulgaria86
Microsoft88
Mobiltel88
SAP Labs Bulgaria89
MusalaSoft90
telerik90
Software AG90
SAP AG90
!90
91
91
91
92
92
93
93
1. 95
95
...95
" "?96
96
97
97
98
98
98
99
()99
100
100
101
101
Java 101
Java ?101
Java !102
103
104
Java104
104
Bytecode105
Java Virtual Machine (JVM) Java105
Java106
Java 107
Java 109
, Java?111
Java 111
Eclipse118
118
Eclipse?118
Java 119
120
122
123
123
124
125
2. 127
127
...127
?128
128
128
128
129
130
133
134
()134
135
135
136
138
138
139
139
140
142
142
142
143
143
144
145
145
146
146
3. 149
149
...149
150
?150
Java150
Java152
153
154
155
156
157
158
?:160
161
162
(implicit) 163
(explicit) 164
166
167
167
168
4. 171
171
...171
?172
172
?172
?172
173
174
-176
176
177
177
System.out178
print() println()178
printf()181
189
System.in189
System.in.read()191
Scanner192
BufferedReader192
Scanner193
195
197
197
198
199
199
5. 201
201
...201
202
202
203
203
204
206
if if-else208
if208
if-else209
if 210
switch-case212
switch-case ?212
switch213
213
switch-case214
214
215
6. 217
217
...217
""?218
while218
while 219
1 N 219
220
break221
221
do-while222
do-while 223
223
[N...M] 224
for224
for 225
for 225
225
N^M 226
For- 226
continue227
for227
228
229
230
231
6/49 231
232
233
7. 235
235
...235
""?236
236
236
() new237
. 238
238
239
240
241
242
243
243
244
for 245
for (for-each)246
246
" "? ""?247
247
248
248
249
250
250
251
251
253
255
8. 257
257
...257
258
258
259
259
?259
260
261
263
263
264
265
265
266
266
267
267
268
268
269
int long Java270
Big-Endian Little-Endian271
271
float double Java273
275
276
277
278
9. 281
281
...281
282
""?282
?282
- - 283
283
283
, 283
284
284
284
285
286
286
(modifiers)287
() 288
288
289
289
290
?291
291
292
292
295
(var-args)301
(method overloading)307
309
312
315
315
316
317
return317
?319
320
322
325
328
330
331
10. 333
333
...333
?334
334
335
335
335
335
336
336
336
337
N 338
339
342
?344
?345
346
347
348
349
350
351
353
355
358
359
360
11. 363
363
...363
364
- ?364
?364
?365
, 365
366
Java366
Java?366
367
368
368
370
371
372
373
Java 376
382
Java?382
382
383
384
384
386
387
12. 389
389
...389
?390
390
390
390
Java391
, 391
?392
Java392
try-catch393
394
Stack Trace396
Stack Trace 397
"Stack Trace"?397
( throw)398
Java398
" "398
Checked exceptions399
(Errors)400
Unchecked exceptions400
400
Throwable401
"" Stack Trace?403
Error404
Exception405
RuntimeException405
throws 405
?407
main() 407
407
try-finally408
409
410
411
411
413
checked 414
415
415
417
418
420
?420
?421
!423
, !423
!424
426
!426
!427
!428
, !428
429
430
13. 433
433
...433
434
()?434
java.lang.String434
435
Escaping 436
436
437
438
439
440
444
()444
446
448
450
453
455
456
. StringBuilder456
: !456
StringBuilder460
StringBuilder?461
StringBuilder - 462
463
464
toString()465
String.format()466
472
474
14. 475
475
475
...475
476
: ?476
?476
477
479
?479
479
480
482
.java 482
. Unicode483
()485
485
public485
default486
private486
486
486
488
488
this489
489
489
491
491
final494
494
494
495
496
497
this498
(scope overlapping)499
501
public502
default503
private504
?506
507
?507
508
510
512
520
522
(overloading)523
(implicit constructor)526
final 528
(properties)531
Java531
532
Java534
537
(static members)538
?538
?538
539
(constants)543
544
545
550
, 552
552
553
554
554
556
15. 559
559
...559
560
?560
, 560
Java 561
562
564
java.util.Scanner 564
565
. 567
569
java.io.PrintStream569
1 20 569
570
571
571
572
572
573
576
576
16. 579
579
...579
580
?580
?580
580
581
581
ArrayList592
(generics)594
ArrayList595
LinkedList603
603
Stack605
607
Queue608
610
611
17. 613
613
...613
614
614
614
, 615
616
622
626
634
645
647
647
650
651
653
654
655
18. , - 657
657
...657
""658
"" 658
"" ( , )659
- 660
-665
-665
- 670
675
""685
""685
686
- HashSet687
- TreeSet688
690
691
19. 693
693
...693
?694
694
695
696
698
-, - 698
699
699
699
702
?702
(Array)702
/ (LinkedList)703
(ArrayList)704
(Stack)704
(Queue)705
, - (HashMap)705
, (TreeMap)706
, - (HashSet)707
, (TreeSet)708
Java 708
710
710
714
717
719
723
724
724
20. - 727
727
...727
: 728
- ()728
728
(Inheritance)729
(Abstraction)742
(Encapsulation)747
(Polymorphism)749
(cohesion coupling)755
(cohesion)755
(coupling)757
(spaghetti code)759
Cohesion coupling 760
- (OOM)761
- 761
762
762
762
763
UML763
Use case ( )763
Sequence 764
Statechart 765
Activity 765
766
Singleton766
Factory Method767
769
769
770
21. 771
771
...771
?772
772
?772
-773
774
, 774
776
777
779
780
780
781
781
781
784
786
787
789
789
?789
?790
Strong Cohesion Loose Coupling791
?792
792
794
794
794
/796
, , 797
799
799
801
?802
?803
803
804
807
807
Assertions808
809
810
811
(Refactoring)814
815
816
816
817
22. ?819
819
...819
820
!820
!821
!821
822
: 824
!825
: 826
!827
!829
?830
832
!833
?834
834
835
!836
!837
838
846
!847
?848
849
?849
850
851
(regression testing)855
856
860
860
863
23. 30.09.2005 .867
867
...867
1: HTML 868
868
868
870
871
871
?871
871
1 872
2 874
3 876
4 878
880
?882
885
2: 885
886
887
?887
888
888
890
890
?890
1 Cell891
2 892
3 - 893
4 894
897
3: 897
898
899
: 899
906
907
909
24. 8.04.2006 .911
911
...911
1: 912
912
912
913
914
914
1 914
2 915
3 917
4 918
5 918
6 919
919
923
2: 923
924
924
924
924
924
1 924
2 925
3 925
4 926
926
928
3: 928
928
928
929
929
1 929
2 930
3 931
4 932
5 932
932
933
934
935
25. 11.12.2005 .937
937
...937
1: 938
938
938
939
939
: 939
942
2: 942
943
943
943
943
944
945
946
947
947
949
3: 949
950
950
950
: 950
956
956
957
959
()
,
:
( Java, C# C++)
:
Java Software Engineer / Java Web Developer
.NET Software Engineer / ASP.NET Web Developer
C++ Software Engineer / Embedded C Developer
PHP & MySQL Web Developer
,
!
350
SAP, VMware, Telerik, Johnson Controls, Sirma Group, Software A.G., Seeburger Informatik, ICB, EGT,
http://academy.devbg.org
, . ! , . , , . , , 15 .
, Java , . -, , , , . , , .
, 15 . , , , . , , , . , , , , HTML, XML, SQL .
, Java , . , Java! , .
?
. , . . , , , , , .
, . . . . . .
, , ! . , , . , , , , -, . ! , . ( ).
, , , . " (, , -)", " - ", " ?" " ". , , - ( UML design patterns) . , !
. , . . , , . , , , , , , , , , , . . .
, , . , , , . , , .
, , , , . .
, . , , , , . . - , -.
, , ! . , . , .
?
. , ( ), , , , , , . - , , ( , -), . - . . . , .
?
" "! Java , , . Skype, Firefox MS Word. , .
. , . . , . , - , , -, , SQL JavaServer Faces ASP.NET.
, , -. ! , . , , . -. -, , .
, . , , -. .
?
, , . , , , .
. , , . .
, - . , , .
, , . ( ) , . . : 1-2 , 15 . , , .
Java?
, Java, . Java . C# C++, .
, Java.
Java e .
, Java , JavaScript , , Java . , Java, .
Java
Java - . Java : , , , , .
Java , C# C++ Delphi, VB.NET C. Java -. , , . , Java .
Java - . . - IBM, Oracle SAP Java Java . "" Java Microsoft, , Java , .NET Framework Java, C#.
Java Sun Microsystems, 2006 . Java Java Sun. Java Java .
Java , , Java Runtime Environment (JRE). . . Java (JVM) , . Java , .
Java
Java , Java. Java - , Java. Java, Java (JVM), Java , , Java (Java bytecode).
, Java, . . Java Runtime Enviroment (JRE). , Java , Java . JRE , , Java. JRE Windows , Adobe Flash Player" , Flash.
Java Java Development Kit (JDK). , Java , . JDK , Java . JRE.
Java?
Java . , , . Java ( C C++). , . , , - .
Java - . , . - () , - , ( , , ). ( , ..) ( , .).
Java . , , Java - ( C#, PHP C++).
, , , . , . Java, ! , Java, , .
Java 6 Eclipse 3.4
6 Java, . Eclipse 3.4 , .
, , Java 6 Eclipse 3.4, Java Eclipse, , . !
?
, . , ! , . ! , .
, , . . , Eclipse , Eclipse . ! -, . . , , , .
! , .
, , . , . . . , , , . .
!
. ! . , , . . , . , . , ! . , . , , , , - -.
! .
?
. , , , .
, , , 2 4-5 , . , .
830 , ( ), . , , .
220 . , ( ). , .
, . , . , .
?
. - ! , , , framework API. - , .
Google 100% , . (. " ") ( GIF ) (. " "). Google , . , , . , , , Google. , . .
Microsoft
Microsoft , . (. " ") (. " "). Google Microsoft , , (multithreding), , . , Microsoft, .
()
, . , , -. ().
?
() , 15- . (Java, .NET, C++, Embedded C, , .). " " . - .
!
( ). " " , . . , . , . , , .
, ( ).
-. , . , :
: (8 ). , 6 .
" ".
, : , , , .
, . , , . !
, . , , , . , .
, . ( ) , , , . . -.
" "
" ", "". , . , : , , , , , , , , , , , . , !
, . , . , . , . , . .
?
, , . - , , .
, , , . , , , , . , , PowerPoint .
. , . , . ( ) . - - . - , .
, , . , .
. , , . .
. , . , . , , . . .
, , . , , .
, .
, .
- -. , . , . , ( ), ( , , e-mail ). . 1 2 . , , ( ).
, , , - . , : , , , , , , desktop , , , (source control repository) , .
, . , .
, - . , 2-3 . .
, , , . . , ( ).
, , . , , , , , , . , .
-, . ( 2008) , 100. SAP, VMWare, Telerik, Software A. G., Seeburger Informatik, Sirma Group , . , .
, , , . , . , , . .
, , . - . , .
, : , , , , . - ( ) . . - . - .
, . ( ) .
, . , , . , " , ", . " , , ". . , , , - , .
, . , , , "" . .
, . , , .
-, , , - 1-2 , . , , , . , , , , .
, , . , . , .
?
, . -, , , , , ( ). , , , . , , , .
- . 1 , 2-3 4 . , , , " ".
, - , . ( 2008):
" Java" . Java, Microsoft .NET .
" C#" , C#, . Microsoft .NET, Java .
" C" " C++" , , , C C++ , .
1-2 . . - , .
, , , . , , . -, , , .
, ( 3 6 ). " ", . , ( Java, .NET ):
, .
, SQL .
.
.
.
. : Java .NET.
"Core Java Developer" Java / Java EE :
Java , Java : JDK, - Java, , , , Java, - . Eclipse.
(RDBMS) Oracle. CASE Studio. SQL PL/SQL. Oracle SQL Developer.
Java : JDBC ORM (Hibernate JPA).
XML Java: XML, XSchema, DTD, XPath, XSLT, XML Java (DOM, SAX, StAX JAXP).
Java AWT Swing.
, , HTML, CSS JavaScript.
Java Servlets, JSP, Tomcat, , Unified EL, JSTL, JavaServer Faces (JSF) AJAX.
. Subversion TortoiseSVN, JUnit, Log4J Ant.
"Core .NET Developer" . - Microsoft .NET :
C# , .NET : .NET Framework, - C#, , , , , - , LINQ . Microsoft Visual Studio.
(RDBMS) MS SQL Server. SQL Server Management Studio. SQL T-SQL.
.NET : ADO.NET LINQ-to-SQL.
XML .NET Framework: XML, XSchema, DTD, XPath, XSLT, XML (DOM, SAX StAX).
.NET Windows Forms.
, , HTML, CSS JavaScript.
ASP.NET, ASP.NET AJAX .NET Framework.
. Visual Studio Team System (VSTS), NUnit, Log4Net MSBuild.
"Core Java Developer" "Core .NET Developer" Java .NET. "Java Web Development" "ASP.NET Web Development", - (- ). " ", .
Java .NET , , , , . , :
Embedded Software Engineering and Real Time Operations Systems C (- ). ( 64 KB 1 MHz ), , .
Web Sites Development with PHP and MySQL PHP, MySQL, DHTML, JavaScript AJAX.
Core C++ Developer C++. C++ , STL, Boost .
, . Adobe Flex, Microsoft Silverlight, , : , (Quality Assurance) . , : http://academy.devbg.org.
, , , , (). . , - :
1.
2.
3.
4. " " 1
5. (Core Java / Core .NET) 3 6
6.
7.
.
1:
(http://academy.devbg.org) , , . , .
! , , .
, !
, . , . , , .
, , . , , . , , . , .
, , ( , , ..), , , . , .
" ", " ", " ", " " . . , . . .
2:
. , , , . .
, , .
. , . - .
3:
, , , ( ) .
, , , , . .
4: " "
" ", , . " Java" " C#" , .
. - . , , , , .
" " , - . , . , , - .
, , , - 4-5 , . .
, - 4-5 . , !
5:
" " "Core Java Developer", "Core .NET Developer" .
- , . 3 6 . 5 ( ). , , , .
, - 5-6 . , , ( 6 ) -.
. , - 5-6 !
, , , , , .
6:
, , . .
, .
. , . , : , , . . , . , . "" . , . , , . .
?
:
1. , . , .
2. . , , .. .
3. ( ) . . " - " " ", , . , .
, . , . , . " ?" . .
, , SQL , XML, HTML, , . , , . , , . !
4. . , . 3 , 3 3 . . , , . - .
, , . . . - , " , " " , " " , ". , " 1 , ". , ! .
, , ( !) , , - . , , , , . , , , ..
5. . . " ". , . . , !
. -, , . , - , . , - , ? - , . - , .
6. . : . .
, , ( ) , ( ).
, ! " ", " ", " " . , , , . .
" ". , , , : " , . , , . , , -, , ".
", , ". , - , - . . , .
, . , , . . . " , ". , 100% . " ". , , " Oracle ?". , , , , , .. , , , (, ).
7. , , . , " , ". . .
, . , - .
. , . , . . . !
, . , . . . , . . e , .
7:
. . - , , . , . -. , - . .
. , , , . - , - , . , , . , , , , . . . - .
- . : , , , . , .
2005 . 2008 . (Core .NET, Core Java, Core C++ Embedded C) 350 :
Core Java Developer 166 Java .
Core .NET Developer 136 .NET .
Core C++ Developer 25 C++ .
Core C & Embedded C Developer 31 Embedded C .
7% , , , , , .
, -, 12% . !
. , " " ( 1000 ), .
. , .
?
, , . , SAP, Telerik, Siemens Johnson Controls.
, , 2005-2008 .:
Core Java Developer ( ): Abritus, Acsior, Antipodes, Bank Service, Bianor, Borica, Comsoft, Fadata, GB Services, Glaxo SK, I:FAO, Insight Technologies, ITSS, Johnson Controls, Lex BG, Logical Factor, MAG Studio, Merkatis, Micro Focus, MusalaSoft, Ocean Solutions, Prolet IT, Rila Solutions, Rushmore Digital, SAP, Sciant, Seeburger Informatik, Sentido, Siemens, Solitron, Stemo, , , , .
Core .NET Developer ( ): Amexis, Applied Language Solutions, Easy Credit, Insight Technologies, Interconsult Bulgaria (ICB), Kontrax, MAG Studio, Micro Focus, Musala Soft, Omnisoft, Portelus, Propeople, Rila Solutions, RISK Engineering, Sysconsulting, Telerik, Unicoders, Wasmar Solutions, Wohnfinder, , , , .
C++ Euro Games Technology (EGT), .
Embedded C Johnson Controls .
Telerik, SAP, InterConsult Bulgaria (ICB), Johnson Controls, Euro Games Technology (EGT), Acsior, Fadata Seeburger Informatik 10 .
, , . . . : http://academy.devbg.org.
. , .
1.
" " . . . Java Java Java . , Java. Java, , , Eclipse. Java, - .
2.
" " Java . , , , , . , , , . , - "". .
3.
", " , . , . , . .
4.
" " . , , . Java . - System.in, System.out System.err, Scanner .
5.
" " Java, . : if if-else switch. , , - .
6.
"" , . (while do-while ) for-. , . ( ).
7.
"" . , , . . , . , .
8.
" " . , , . , : , , - .
9.
"" . . () . . .
10.
"" . , . , . .
11.
" " - Java. (). , . , .
12.
" " Java - . try-catch, throws . . , . - .
13.
" " : Java . ; , , . - , , . , .
14.
" " . , . . . ( ), .
15.
" " Java. , . . , . , .
16.
" " . - . "", "" "" . .
17.
" " . . , . . , . . , . - , . "", . Java .
18. , -
", - " - , - . - . "" - . "" . , .
19.
" " , , , (, , ..). . -, , , , - . Java . , . .
20. -
" - " - : , , , . (cohesion coupling). - . UML - . " " , .
21.
" " . (, , ), , . . Sun Java, JavaBeans . , .
22. ?
" ?" . , ( , ) . , , . ( ), .
23, 24, 25.
" " , 30.09.2005 ., 8.04.2006 . 11.12.2005 . " ".
, , . :
- Java ( ) .
Java ( "", "" ""). - .
( "framework" "deployment") . ( ), .
?
. , , . . Java , . ( ), , , . , Java, . , . C , Java C#. , , .
. , .
. () 2005 ., " C#". Java " Java". .
( 2008) " Java" 720 PowerPoint , 110 200 , 15 . , - , . , , , . ? , , .
, , " " , , .
. . , .
. - ( ), ( ) . : 1 . , , . .
Google Code http://code.google.com/p/introjavabook/, .
, , . , , , .
. . . . , .
- 90%, . , . 30- , , .
. , , , (, , , ..) 1 , . 800-1000 , , 6 . , 4-5 .
. , .
, ( " .NET Framework"). , , . , , , .
, , , . , . . . - , , , , , , .
, , .
( ).
CSC (www.csc.com). " ", . . , , C/C++ Java. , . e-mail: [email protected].
, . , C/C++, .NET Java. , , , , . , . .NET . ". ". e-mail: [email protected]. : http://veskokolev.blogspot.com.
(Tsachev)
(). - open-source , Java . , .
(), Java.
" " ". ". Java. " " .
http://info.tsachev.org/.
, ". Insight Technologies (www.insight-bg.net) (). . Java Unidevelop (http://unidevelop.org/) -. e-mail: [email protected].
, , , . .NET Framework, Visual Studio Microsoft SQL Server. () - , , . e-mail: [email protected].
Ontotext Lab (www.ontotext.com). " ". . Java SOA . . e-mail: [email protected].
SAP Coca Cola HBC IT Shared services. - , Universita degli Studi di Pavia. () Sun Certified Java Programmer. - Java EJB, Seam, JSF SAP ERP . e-mail: [email protected].
Nonillion (www. nonillion.com), Sun Java . , freelance developer Java. . , , . http://peshev.net/blog.
Insight Technologies (www.insight-bg.net) (academy.devbg.org). . , , ". ", . enterprise Java , .NET Web . e-mail: [email protected].
" " . . .
" Java ", " .NET Framework", " Java ", " " " ". . .
" .NET Framework". Java .NET Framework . .
IT . Java, Java EE, SOA, Spring ().
Siemens, HP, EDS , , , .
: http://mihail.stoynov.com/blog/.
". ", " ". ". ", " " , , " ".
.
Sun Java 2 Platform, SE 5.0.
2002-2005 . , " " " ".
.
, eBG.bg . , . . " ", "- C++" Visual C++" " ", ", " Java", ", " .NET" " Java". , Java .NET . ". ". , . : http://www.nedyalkov.com.
. " ". Java. . - . , . e-mail: [email protected].
(CERN) www.cern.ch. ". " . ". ", " .NET Framework". Java , .NET , .
Fadata (www.fadata.bg). . , " " . , . mail: [email protected].
a ICB (www.icb.bg). .NET . ". ". . e-mail: [email protected].
" " (), .
, Java , .NET , , , .
15- , , , Assembler, Basic Pascal C C++ PHP, Java C#. , , , , , ERP .
, , . ". ", " ", " Java", " ", " .NET Framework", " Java ", " " " ". (), "Core Java Developer" "Core .NET Developer", (Java EE, Hibernate, SQL, Software Engineering ).
, , " Java", "Java " " .NET Framework ( 1 2)". 100 .
". " .
4 .
2003 . " " . 2004 . " " .
(www.devbg.org) .
: http://www.nakov.com/blog/.
Wizefish (www. wizefish.com). ". ". , . - , , RIA (Rich Internet Applications). ; .
Fadata (www. fadata.bg). -, . , Jva. , . e-mail: [email protected].
(). .NET Java . ". ". , , . http://tsvyatkokonov.blogspot.com/.
, . ( ).
" ".
" ", " " " ".
"" "".
"".
" - ".
" ", " ", " " " ".
" ".
( ).
"", " 8.04.2006".
" " " ".
"", "" " ".
, , . , , , . , Microsoft, Google, SAP AG, telerik .
- Java , . , , !
, . - , . - , . .
15 , , . , , . - . - . . .
. . , .
2000 . Java . , 25 4 000 Java . , . . 25 , . Arrays.sort(), - - . , O(n*log(n)), O(n*n). , . , , . , , . Java , - . , .
. , "" , . , . 15 . , Java C#, , Pascal Ada. , - . . . calculate rezultat. a1, a2, suma. , , . , , , , . , .
, , . . , . , . , , . , , . Java. - . - , . - , , - . - . , . , - , . , , .
. - , - . - , , . , . , , ? . - !.
Java !
Java Enterprise Service Oriented Architecture (SOA). BEA Systems Oracle Corporation. Sun, BEA Oracle. , J2EE, " " ". ". Oracle (, , , , ) J2EE . IEEE. () www.devbg.org. email: nick {at} manchev.org.
ProSyst Labs
, , , . 30 . !
" Java" , . , , , . . - . , .
, , . , , , , .
, , , -, , , . , , -. , . - , .
" Java" , , , , . - , " ".
. , , . , . . ! - , . !
- (www.prosyst.com), 15 , , , . . OSGi Alliance (www.osgi.org) Java Community Process (www.jcp.org), Eclipse Foundation (www.eclipse.org). Miele, Philips, Siemens, BMW, Bosch, Cisco Systems, France Telecom, Renault, Telefonica, Telekom Austria, Toshiba, HP, Motorola, Ford, SAP . , OSGi , , Java Enterprise . .
VMWare
, . - , .
VMware Sciant .
. , -, , , (n3). , , . - . , ! , . , , .
AJAX, , -, . , , , - ArrayList ?
, . , , , . , , ? , . .
VMware (www.vmware.com) 10 Java . VMware , enterprise Java . VMware , Sciant SAP Labs .
, , 1998 , , 1997. (ACM ICPC). 2001/2002 " " ". ". () www.devbg.org.
, , !
Google , , , . , . , - . - . , , . , - . Python JavaScript . , .
, -. , - - - . , , , - . ?
Java . , - , - . , , , Java. , .
Google, Google Maps Google Translate. Motorola Biometrics Metalife AG.
, , 2002 . ". " (ACM ICPC) .
SAP Labs Bulgaria
- , Java - , , . , Java.
, Java, C#, Flash, SQL . . , , , . , . : . ? ? , . - Java C# J2EE AJAX , , .
, , , , middleware, , embedded, , . , , Java.
, . . , . , , , .
, , 19, 20, 21 22 , , . ( 19 22), .
... ! Eclipse Java, , .
SAP Labs Bulgaria, SAP NetWeaver Java application server. , . , , , . Mercury Interactive, Jaguar Formula 1, Ford WRC, DHL, Belair . , , . 1992 .
Microsoft
" Java" , , , .
, . , , . - - . , , , , . , .
, , , , . , , .
, ! . - . !!!
, , Microsoft
Mobiltel
, - . , , , , .
5 10 , , , , , .., . . , .
, , , .
, , . , . , . , .
, -, , , , , , . , , .
. . , .
!
"Data Services Research and Development" , telecom .
SAP Labs Bulgaria
! , . - , , .
"" SAP Labs Bulgaria, SAP (www.sap.com). 15 , . Java .
MusalaSoft
! , , , . ! . , . !
, ,
telerik
, . . "" , .
, ,
Software AG
, , , - Java. .
, ,
webMethods RnD, Software AG
SAP AG
( Wikipedia ) , .
, SAP AG
"=++;"
!
, , . , , .
, :
:
1. Java", (http://www.nakov.com) (http://academy.devbg.org).
2. :
() Java";
-;
;
;
3. :
http://www.introprogramming.info
4. , . .
5. , .
3. :
;
- , , , ;
;
, , .
4. :
, .
, ;
.
1. , .
2. , , , .
3. , , .
4. , , .
() ( ) , , .
" Java" : http://www.introprogramming.info. , .
,
,
(),
14.07.2008 .
92 Java
93
()
,
:
( Java, C# C++)
:
Java Software Engineer / Java Web Developer
.NET Software Engineer / ASP.NET Web Developer
C++ Software Engineer / Embedded C Developer
PHP & MySQL Web Developer
,
!
350
SAP, VMware, Telerik, Johnson Controls, Sirma Group, Software A.G., Seeburger Informatik, ICB, EGT,
http://academy.devbg.org
1.
...
. .
.
Java Java Java .
, Java.
Java, , , Eclipse.
Java API, - .
" "?
- , , , , . , , . , , .
, . , , (), . , , , .
, , , , . , . , . (RAM) , , .
. , , . , . . , , . .
, . - ( RAM ), , . . , HTTP.
. , , . . - , , . . - , . , .
. - . - , , . - . , . , .
. () . , . . , .
, . . . , , , C. , . Java, C++, C#, Visual Basic, Python, Ruby, PHP .
Java. . .
, . , . , :
;
;
;
();
;
.
, , .
. , , . , , - Google Maps. .
- , , . , . , , . . .
, . , , () . , . :
, (GUI application), - , , Rich Internet Application (RIA) peer-to-peer ;
, , , SOA ;
, - Java C++ C# ;
, : ( Java EE, Microsoft .NET ), ( Oracle, SQL Server, MySQL ), ( Flash, JavaServer Faces, Eclipse RCP, ASP.NET, Windows Forms ), ( Hibernate, JPA LINQ to SQL), ( Jasper Reports) , .
, ( );
, , .
( , ..).
, , . - . , .
, - , (). , () () . , .
()
. , . , , . , , , . , , .
() quality assurance (QA) . (). , .
- . , .
. , - -. - . - , - . , , . Microsoft Windows . , .
, . , .
. . .
. , , , , , QA , .
, . . , .
, , , , , , . , , .
, -, .
Java
Java Java , , Java.
class HelloJava {
public static void main(String[] arguments) {
System.out.println("Hello, Java");
}
}
, , "Hello, Java" . , . - , .
Java ?
:
;
main();
main().
HelloJava. - class, . HelloJava.
main()
() main(), . Java main() :
public static void main(String[] arguments)
, public, static void, main String. public static . arguments, , . args argv.
, , , , .
main()
, . System.out println(), "Hello, Java". Java .
main() , .
" ", " ", " ".
Java !
, class, public, static void , System.out.
, ! , Java . Class class System.out SYSTEM.OUT.
, , , ..
, , , , . .
class HelloJava {
public static void main(String[] arguments) {
System.out.println("Hello, Java");
}
}
- . :
class HelloJava {
public static void main(String[] arguments) {
System.out.println("Hello, Java");
}
}
:
class HelloJava{public static void main(String[]arguments){ System.out.println("Hello, Java");}}
:
class
HelloJava
{public
static void main(
String[
] arguments) {
System.
out.println("Hello, Java");}
}
, - .
! .
- ;
- ;
{ , ;
} , ;
;
.
Java . , .java. .
, HelloJava.java.
Java
Java Sun Microsystems 1995 , Java . GNU, Microsoft, IBM, Oracle .
, Java , , , . Windows Windows Linux, Apple OSX , Java . , Java.
Bytecode
Java . , , , - bytecode. bytecode , , Java Virtual Machine (JVM).
Java Virtual Machine (JVM) Java
Java Java Virtual Machine, , .
JVM . .
Sun Microsystems , (PDA). Sun, JavaTM 2 SDK JavaTM 2 Runtime Environment, - Win32 Solaris . , , . bytecode, , bytecode.
Java, bytecode, class . , bytecode, .
Java
Java e - . C C++, , . Java .java. Java javac , .class. Java bytecode , .
Java :
abstract
continue
for
new
switch
assert
default
goto
package
synchronized
boolean
do
if
private
this
break
double
implements
protected
throw
byte
else
import
public
throws
case
enum
instanceof
return
transient
catch
extends
int
short
try
char
final
interface
static
void
class
finally
long
strictfp
volatile
const
float
native
super
while
. const goto. , . . - . 1.2 strictfp, 1.4 assert, 1.5 enum.
Java , , , , , , .
, , .
- Java . . , Java.
, " " " " (Garbage Collector). , .
Java
Java , Java, Java . Java , , :
Java Platform Standard Edition (Java SE)
Java . :
Java ;
Java Virtual Machine ;
;
TCP/IP ;
XML;
;
(native) (JNI);
(JDBC);
(RMI-IIOP);
2D/3D ;
;
;
.
.
Java Platform Enterprice Edition (Java EE)
J2EE, , - . Java EE Java SE, .
Java EE , - , .
HTTP + Servlet ;
EJB ;
(JMS);
;
.
Java Platofrm Micro Edition (Java ME)
J2ME, - , . - , (PDA) .
, Java ME Java SE . , JMV.
Java
Java . , , Java. , Java . . .
, . Java, . , , . .
, . , , , - . Java . , .
, , . , Java .
Application Programming Interface (API)
Java , . , , Application Programming Interface API. Java API, Java , , . Java API. API, , API , API .. API, .
Java API
API, . , . .
Java API HTML , , , , . , , .
Java Platform, Standard Edition 6 API Specification (http://java.sun.com/javase/6/docs/api/). Java , , .
, Java?
, Java : - Java - . Java , Java.
Java JDK JRE
Java SE, Java EE Java ME Windows, Linux, Mac OS X, Solaris, AIX .
Java SE , . :
Java Development Kit (JDK);
Java Runtime Environment (JRE).
Java JDK. JDK , Java , . JRE JDK. JRE , Java, . JDK Java Standard Development Kit Java SDK.
. .
Java
, Java. . :
HelloJava.java;
;
HelloJava.java HelloJava.class;
HelloJava.class JVM .
, !
, JDK ! .
. Windows, Solaris Linux. Solaris Linux , . (). - , :
HelloJava.java
class HelloJava {
public static void main(String[] arguments) {
System.out.println("Hello, Java");
}
}
Java Windows
, .
introToJava C:\. C:\introToJava HelloJava.java, Notepad.
, , "Yes". .
[Ctrl-S] [Alt-F4]. , C:\introToJava\HelloJava.java. . javac.exe.
, Windows javac. , Java, :
Java ;
Java JRE ( JDK);
JDK, JAVA_HOME/bin Windows javac.exe, .
, JDK. javac.exe, .
javac , C:\introToJava\HelloJava.class. , . java.exe. , :
java HelloJava
"Hello, Java", . , .
class, java! .
Windows
javac.exe java.exe, . , Windows . Windows : My Computer --> Properties --> Advanced --> Environment Variables --> System Variables --> Path --> Edit:
bin JDK. :
C:\Program Files\Java\jdk1.6.0_07\bin
Path , . JDK . , .
. , ( ). (cmd.exe), Java. , Java :
Java Linux Solaris
, Linux Solaris . .
JDK, . .
Linux UNIX JDK , . , (rpm -i, yum install, apt-get install, ) . Debian Ubuntu Linux JDK :
apt-get install sun-java6-jdk
, JDK. , . introToJava . ~/introToJava HelloJava.java, vi.
, "i":
. , Esc, , ":wq" Enter:
, vi, , vi, . vi - mcedit pico ( , ).
~/introToJava/HelloJava.java . javac:
javac , ~/introToJava/HelloJava.class. , . java ( ). , :
java HelloJava
"Hello, Java", .
Eclipse
Java . , - , , . , - .
. , . , , , . , , , , , (Integrated Development Environment, IDE). - , , unit , , ..
Eclipse?
Eclipse (www.eclipse.org) . Eclipse . - , , , .
, - Eclipse. . (views). , . - Java . :
Package Explorer , ;
Problems ;
Outline () Java ;
Console , .
Java . - . Eclipse .
, .
Java
Eclipse, . , . .
:
File -> New -> Project;
"Java Project";
- "introToJava";
"Finish".
Package Explorer.
.
Eclipse :
:
File-> New -> Class;
- "HelloJava";
"public static void main(String[] args)" main() ;
[Finish].
Package Explorer , . Eclipse , main() :
main() :
Eclipse :
;
;
Java bytecode .
Eclipse, . . . , . "Problems".
, "" Package Explorer. , , , . Problems Eclipse, , .
, Run -> Run As -> Java Application Ctrl+Alt+X J ( Ctrl, Alt X , J).
"Console":
. Java , main() .
, , . . :
;
, ;
, ;
, , .
.
Eclipse :
, , . Eclipse , , .
, , (breakpoints). . . .
. - .
1. System Java API .
2. System.out.println(), , Java API .
3. () Eclipse.
4. , , " !".
1. http://java.sun.com/javase/6/docs/api/ System.
2. System out, println().
3. , .
4. , .
124 Java
1. 125
()
,
:
( Java, C# C++)
:
Java Software Engineer / Java Web Developer
.NET Software Engineer / ASP.NET Web Developer
C++ Software Engineer / Embedded C Developer
PHP & MySQL Web Developer
,
!
350
SAP, VMware, Telerik, Johnson Controls, Sirma Group, Software A.G., Seeburger Informatik, ICB, EGT,
http://academy.devbg.org
2.
...
Java - . , , , , . , , , . , - "". . , , , .
?
, . , , . , , , . , , , . , .
, . , , () . . .
, .
:
;
( );
(default value).
:
byte, short, int, long;
float double;
boolean;
char;
Object;
String.
- (byte, short, int, long, float, double, boolean, char, Object String), :
byte
0
-128
+127
short
0
-32768
+32767
int
0
-2147483648
+2147483647
long
0L
-9223372036854775808
+9223372036854775807
float
0.0f
-3.4+38
+3.4+38
double
0.0d
-1.7+308
+1.7+308
boolean
false
true false
char
'\u0000'
0
+65535
Object
null
String
null
byte, short, int, long, float, double, boolean char , Java - .
Object String , ( ). , Java, , Java.
byte, short, int long. , .
byte. 8- , , 2 8, .. 256 . 0. , , -128, +127.
short. 16- . byte - . 0. , -32768, - +32767.
, int. 32- . , , . 0. , -2 147 483 648, +2 147 483 647.
, , long. 64- 0L. L , long ( int). , long , -9 223 372 036 854 775 808, +9 223 372 036 854 775 807.
byte int long, byte. .
, , . , - , " " " ".
// Declare some variables
byte centuries = 20;
short years = 2000;
int days = 730480;
long hours = 17531520;
// Print the result on the console
System.out.println(centuries + " centuries is " + years +
" years, or " + days + " days, or " + hours + " hours.");
- . byte, long.
.
20 centuries is 2000 years, or 730480 days, or 17531520 hours.
, , float double. , .
32- float. 0.0f 0.0F ( ). "f" , float ( double). 6 9 ( ). , , -3.4+38, +3.4+38.
, , double. 64- 0.0d 0.0D. 15 17 . , , -1.7+308, +1.7+308.
:
// Declare some variables
float floatPI = 3.14f;
double doublePI = 3.14;
float double. , , ( , ). 6 9 , 15 17 .
, , . float double.
// Declare some variables
float floatPI = 3.141592653589793238f;
double doublePI = 3.141592653589793238;
// Print the result on the console
System.out.println("Float PI is: " + floatPI);
System.out.println("Double PI is: " + doublePI);
, :
Float PI is: 3.1415927
Double PI is: 3.141592653589793
- , double float, . , , float, 7- , double 15- . , , double - , .
Java ( IEEE 754) : (1 -1), ( ) ( ), . 9 float 17 double. , . ( -). . ( 1.4 * 10-45 float) ( 3.4 * 1038 float), .
" ".
, -, , , (32 64) . 0.1 ( ) (.. float double) .
float double! 0.1 float 0.099999994.
Java , ( ).
. . 1/3, 2/7 .
, , , . , , -:
// Declare some variables
float sum = 0.1f + 0.1f + 0.1f + 0.1f + 0.1f +
0.1f + 0.1f + 0.1f + 0.1f + 0.1f;
float num = 1.0f;
// Is sum equal to num
boolean equal = (num == sum);
// Print the result ot the console
System.out.println("num = " + num + " sum = " + sum + " equal = " + equal);
.
num = 1.0 sum = 1.0000001 equal = false
, 0.1f 1.0f. , 0.1f 0.099999994f float. f, , () float " ".
boolean. , true false. false. - .
, , , . , -:
// Declare some variables
int a = 1;
int b = 2;
// Which one is greater?
boolean greaterAB = (a > b);
// Is it equal to 1?
boolean equalA1 = (a == 1);
// Print the result on the console
if (greaterAB) {
System.out.println("A > B");
} else {
System.out.println("A >>
==, !=, >, =, >>=
+
(type), instanceof
., new, (), [], ?:
, .
, , :
()
(unary)
(binary)
(ternary)
-, , , , . (:?) - ( ).
Java , . +. (int, long, float .), . , / .
:
int z = 4 + 8;
System.out.println(z); // 12
String firstName = "Lachezar";
String lastName = "Bozhkov";
// Do not forget the interval between them
String fullName = firstName + " " + lastName;
System.out.println(fullName); // Lachezar Bozhkov
+ , .
Java
. - -. () , .
Java:
-
++, -- ( ), new, (type)
++, -- ( ), +, - (), !, ~
*, /, %
+ ( )
+, -
, >>>
, =, instanceof
==, !=
&, ^, |
&&
||
?:
-
=, *=, /=, %=, +=, -=, =, >>>= &=, ^=, |=
, - - , , . .
- . :
// Ambiguousx + y / 100
// Unambiguous, recommended
x + (y / 100)
, , . , - .
+, -, * . , . / (integer), ( ). %. (increment) ++ , -- (decrement) .
++ -- ( ) , , ( ) , .
:
int squarePerimeter = 17;
double squareSide = squarePerimeter / 4.0;
double squareArea = squareSide * squareSide;
System.out.println(squareSide); // 4.25
System.out.println(squareArea); // 18.0625
int a = 5;
int b = 4;
System.out.println(a + b); // 9
System.out.println(a + b++); // 9
System.out.println(a + b); // 10
System.out.println(a + (++b)); // 11
System.out.println(a + b); // 11
System.out.println(14 / a); // 2
System.out.println(14 % a); // 4
(true false). (&&), (||), (^) (!).
Java , :
x
y
!x
x && y
x || y
x ^ y
true
true
false
true
true
false
true
false
false
false
true
true
false
true
true
false
true
true
false
false
true
false
false
false
, , "" , , . "" , . . , true , false. . true, true. true, false.
. :
boolean a = true;
boolean b = false;
System.out.println(a && b); // false
System.out.println(a || b); // true
System.out.println(!b); // true
System.out.println(b || true); // true
System.out.println((5>7) ^ (a==b)); // false
:
!(a && b) == (!a || !b)
!(a || b) == (!a && !b)
, ( ) ( ) .
, .
+ (String). , . String, , String, String.
, :
String first = "Star";
String second = "Craft";
System.out.println(first + second); // StarCraft
String output = first + second + " ";
int number = 2;
System.out.println(output + number);
// StarCraft 2
String . println(), . . output. output 2 ( number) . String, .
( ) . StringBuilder StringBuffer ( ) .
" " , , .
, . . . 55 00110111.
, , " " -5V, " " +5V.
" ", , .
. , , . true false ( ), , 0 1 (). , "" (&), "" (|), (~) "" (^).
:
x
y
~x
x & y
x | y
x ^ y
1
1
0
1
1
0
1
0
0
0
1
1
0
1
1
0
1
1
0
0
1
0
0
0
. " " , . ^". !", "~".
, . (). , . : (), , , , . 3 )
- (=)
- ( y)); // true
System.out.println("x < y : " + (x < y)); // false
System.out.println("x >= y : " + (x >= y)); // true
System.out.println("x b" : "b 0); // Check the loop condition
System.out.println("n! = " + factorial);
n=7:
n = 7
n! = 5040
, -, .
[N...M]
[NM]. num. num. product, num 1 num m.
Scanner input = new Scanner(System.in);
// Read the end values of the interval n...m from the console
int n = input.nextInt();
int m = input.nextInt();
int num = n;
long product = 1;
do {
product *= num;
num++;
} while (num 1 2 3 4 5n=6->1 2 3 4 5 6
1 2 3 41 2 3 4 5
1 2 31 2 3 4
1 21 2 3
11 2
1
:
Triangle.java
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
// Entering the value of the variable n
System.out.print("n = ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
System.out.println();
// Printing the upper part of the triangle
for (int line = 1; line = 1; line--) {
printLine(1, line);
}
}
private static void printLine(int start, int end) {
for (int i = start; i = 37) {
System.out.println("You are ill!");
}
input.close();
}
}
, , . , , . , , -, , , . convertFahrenheitToCelsius(), temperatureF, double, . Java.
, , , main() .
Scanner, , "Enter your body temperature in Fahrenheit degrees".
convertFahrenheitToCelsius(), temperature.
printf() System.out, "Your body temperature in Celsius degrees is X", X temperature.
, if, - 37 , , .
:
Enter your body temperature in Fahrenheit degrees:
100
Your body temperature in Celsius degrees is 37,777778.
You are ill!
, , ( "What time is it?"). , , . , , "The time is HH:mm now.", , mm . , "Incorrect time!".
, , :
.
.
.
, , , .. . , 0 23 , 0 59 . ( ) , , .
:
DataValidation.java
import java.util.Scanner;
public class DataValidation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What time is it?");
System.out.print("Hours: ");
int hours = input.nextInt();
System.out.print("Minutes: ");
int minutes = input.nextInt();
boolean isValidTime =
validateHours(hours) && validateMinutes(minutes);
if (isValidTime) {
System.out.printf(
"The time is %d:%d now.%n", hours, minutes);
} else {
System.out.println("Incorrect time!");
}
}
public static boolean validateHours(int hours) {
boolean result = (hours >= 0) && (hours < 24);
return result;
}
public static boolean validateMinutes(int minutes) {
boolean result = (minutes >= 0) && (minutes = 0) && (hours < 24);
return result;
}
, , . validateMinutes(), , boolean. , -, 0 59 , true, false:
public static boolean validateMinutes(int minutes) {
boolean result = (minutes >= 0) && (minutes numbers[j]) {
int tempVar = numbers[i];
numbers[i] = numbers[j];
numbers[j] = tempVar;
}
}
} // End of the sorting logic
return numbers;
}
printNumbers(int...) :
SortingEngine.java
public class SortingEngine {
public static int[] sort(int... numbers) {
// The sorting logic:
for (int i = 0; i < numbers.length - 1; i++) {
// Loop that is operating over the un-sorted part of
// the array
for (int j = i + 1; j < numbers.length; j++) {
// Swapping the values
if (numbers[i] > numbers[j]) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
} // End of the sorting logic
return numbers;
}
public static void printNumbers(int... numbers) {
for (int i = 0; i < numbers.length; i++) {
System.out.printf("%d", numbers[i]);
if (i < (numbers.length - 1)) {
System.out.print(", ");
}
}
}
public static void main(String[] args) {
int[] numbers = sort(10, 3, 5, -1, 0, 12, 8);
printNumbers(numbers);
}
}
, , , :
-1, 0, 3, 5, 8, 10, 12
" " , , . :
, . strong cohesion. , - -. !
, . , , sortNumbers(), number() processing() method2(). , - .
( , ), findSmallestElement() sort(int[] arr) readInputData().
Java . camelCase, .. , , .
, , . . , , . . " ".
(- , ). , , , , ( ). loose coupling.
, - " ". , , - "" .
, , , , . , , , , , . , , .
1. , "Hello, !" ( "Hello, Peter!"). , .
2. getMax() (int) , - . , - , getMax().
3. , . : 512 "two"; 1024 "four".
4. , . , .
5. , , , -, - .
6. , , - , -1, .
7. , . 256, 652.
8. , n! n [1..100].
9. , :
.
.
a * x + b = 0.
.
, , .
:
.
.
a 0.
10. , .
1. String.
2. Max(a, b, c) = Max(Max(a, b), c).
3. 10 switch .
4. (int[]) (int).
5. , .
6. , .
7. :
: num. num 0 (num % 10) num 10.
: String for .
8. , 100! long. , . 512 {2, 1, 5}. , ( ).
9. . (1 - , 2 - , 3 - ) 1 3.
10. , .
330 Java
9. 329
()
,
:
( Java, C# C++)
:
Java Software Engineer / Java Web Developer
.NET Software Engineer / ASP.NET Web Developer
C++ Software Engineer / Embedded C Developer
PHP & MySQL Web Developer
,
!
350
SAP, VMware, Telerik, Johnson Controls, Sirma Group, Software A.G., Seeburger Informatik, ICB, EGT,
http://academy.devbg.org
10.
...
. , . , . .
?
, . .
, .
, . .
. :
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
. 1, .. :
F1 = F2 = 1
Fi = Fi-1 + Fi-2 ( i > 2)
, n- :
public static long fib(int n) {
if (n 0
. . , .
0! = 1
1! = 1 = 1.1 = 1.0!
2! = 2.1 = 2.1!
3! = 3.2.1 = 3.2!
4! = 4.3.2.1 = 4.3!
5! = 5.4.3.2.1 = 5.4!
:
n! = n.(n-1)!
n = 0, 1.
, n-1 n. , , 0 n .
, .
public static long factorial(int n) {
// The bottom of the recursion
if (n == 0) {
return 1;
}
// Recursive call: the method calls itself
else {
return n * factorial(n - 1);
}
}
, , , :
RecursiveFactorial.java
import java.util.Scanner;
public class RecursiveFactorial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("n = ");
int n = input.nextInt();
long factorial = factorial(n);
System.out.printf("%d! = %d%n", n, factorial);
input.close();
}
public static long factorial(int n) {
// The bottom of the recursion
if (n == 0) {
return 1;
}
// Recursive call: the method calls itself
else {
return n * factorial(n - 1);
}
}
}
, 5 n:
n = 5
5! = 120
, , , - .
, , , () .
, , -:
public static long factorial(int n) {
long result = 1;
for (int i = 1; i at Junk.b(Junk.java:18)
at Junk.a(Junk.java:12)
at Junk.main(Junk.java:4) ( -)
"" -. .
Error
Error ( ). , . Error.
Error Throwable, : stack trace , getMessage(), printStackTrace() getStackTrace() , getCause().
OutOfMemoryError, , .
Exception
. checked . Exception, Throwable. checked stack trace, Throwable.
( ).
IOException, - . .
RuntimeException
. unchecked . RuntimeException, Exception, Throwable. unchecked Throwable (stack trace, message, cause .). .
throws
, . , , . . , , throws. :
public static void readFile(String fileName)
throws FileNotFoundException, IOException {
}
, - FileNotFoundException IOException. FileNotFoundException , IOException. - . - Runtime , .
. , , , throws FileNotFoundException, IOException.
public static void readFile(String fileName)
throws FileNotFoundException, IOException {
FileInputStream fis = null;
fis = new FileInputStream(fileName);
BufferedReader in = new BufferedReader(
new InputStreamReader(fis));
String tmp = null;
while ((tmp = in.readLine()) != null) {
System.out.println(tmp);
}
in.close();
fis.close();
}
, . - , , . , , . - throws .
(GUI) , . . , .
:
, , . .
, main() , ( ) .
?
checked -. . -, checked. , -, , , -. , , , RuntimeException -. - - A-. , (nested exceptions).
main()
main() 1.4 JDK . . Java 5 main() , :
public static void main(String a[])
throws FileNotFoundException, IOException {
readFile("C:\\tools\\eclipse\\eclipse.ini");
}
main() stack trace (System.err). , main() , .
- . . () . readFile() :
public static void main(String[] args) {
try {
readFile("C:\\tools\\eclipse\\eclipse.ini");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readFile(String fileName) throws IOException{
try {
FileInputStreamfis = new FileInputStream(fileName);
BufferedReader in = new BufferedReader(
new InputStreamReader(fis));
String tmp = null;
while ((tmp = in.readLine()) != null) {
System.out.println(tmp);
}
} catch (FileNotFoundException e) {
System.out.println("The file \"" + fileName +
"\" does not exist! Unable to read it.");
}
}
readFile() main() . readFile() FileNotFoundException, IOException main() .
main() FileNotFoundException readFile(). main() .
- .
try-finally
try finally. finally try , try . finally return.
finally , try !
finally :
try {
Some code that could or could not cause an exception
} finally {
// Code here will allways execute
}
try finally catch. catch finally .
try {
some code
} catch (...) {
// Code handling an exception
} catch (...) {
// Code handling another exception
} finally {
// This code will allways execute
}
finally . , return, continue, break .
, , . - try- finally , . :
public static void readFile(String fileName) throws IOException{
FileInputStream fis = new FileInputStream(fileName);
BufferedReader in =
new BufferedReader(new InputStreamReader(fis));
// Using the streams here
in.close();
fis.close();
}
FileInputStream BufferedReader. . , :
.
.
.
return.
.
, . , . , continue break, .
try-finally. , .
:
FileInputStream fis = null;
try {
fis = new FileInputStream("fileName.txt");
// Using "fis" here ...
} finally {
// Always cl