897
Java SE Сервлеты и JSP Паттерны GoF JUnit Log4J Ant UML SQL JPA Hibernate Методы программирования Java Java И.Н.БЛИНОВ В.С.РОМАНЧИК

МЕТОДЫ JavaJava - EPAM | Перспективная работа … ОГЛАВЛЕНИЕ Часть 1. Основы Java Глава 1. ВВЕДЕНИЕ В ООП И КЛАССЫ

  • Upload
    lamhanh

  • View
    376

  • Download
    16

Embed Size (px)

Citation preview

  • Java SE JSP GoFJUnitLog4JAntUMLSQLJPAHibernate

    JavaJava.. ..

    ,

    - , Sun Certified Java Programmer & Web

    Component Developer.

    Java JEE.

    - .

    , - , - . - , -. 100 .

    EPAM Systems

    : 1993 . : , , -; , -; , , ; - .

    : , , , , , , , , , , , .

    : (IAOP) EPAM The 2012 Global Outsourcing 100. The 2012 Global Services 100 EPAM . 10 - R&D- 2012, Zinnov.

    .

    .

    ..

  • . . . .

    Java

    -

    2013

  • 004.434 32.973.26-018.2

    69

    : - . . ,

    - , . .

    -

    , 1-31 03 01 ( ),

    1-31 03 01-05 ( )

    ,..,,..Java. : .-. / . . , . . .

    : , 2013. 896 . ISBN 978-985-7058-30-3.

    , Java SE, JEE . Java - . Java, , , , - L. - , JSP . .

    . , UML, SQL, Ant, XML, Log4J, JUnit, JPA Hibernate.

    004.43432.973.26-018.2

    ISBN978-985-7058-30-3 . ., . ., 2013 .

    , 2013

    69

  • 3

    1.Java

    1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131 6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150

    2. 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 9. / . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .290 12. JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .342 13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .376 14. XML & JAVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .395

    3.web 15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .456 16. JAVA SERVER PAGE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .485 17. C, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .522 18. JSP STANDARD TAG LIBRARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .573

    4. 20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .592 21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .605 22. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .629 23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .695 1. JUNIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .751 2. LOG4J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .766 3. UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .780 4. SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .793 5. APACHE ANT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .813 6. JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .827 7. HIBERNATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .853 8. IDE ECLIPSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .868

  • 4

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10

    1.Java

    1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Base code conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

    2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

    3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 , . . . . . . . . . . . . . . . . . . . . . . . . 55 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 final. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 native . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 synchronized. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 . . . . . . . . . . . . . . . . . . . . . . . . . . 73 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 . . . . . . . . . . . . . . . . . . . . . . . . . 87

  • 5

    4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 final. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 super this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 . . . . . . . . . . . . . . . . . . . . . . . . . 103 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 . . . . . . . . . . . . . . . . . . . . . . . . . . 107 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 . . . . . . . . . . . . . . . . . . . . . . . . 118 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 . . . . . . . . . . . . . . . . . . . . . . . 123

    5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131 (inner) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 (nested) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 (anonymous) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

    6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

    2.

    7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 StringBuilder StringBuffer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

    8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 finally. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 assertion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

  • 6

    9. / . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 /. . . . . . . . . . . . . . . . . . . . . . . 225 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Scanner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

    10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 ListIterator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Comparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 LinkedList Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Deque ArrayDeque . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

    11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .290 Thread Runnable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 . . . . . . . . . . . . . . . . . . . . . . . 293 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 synchronized. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 synchronized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 wait(), notify() notifyAll(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 TimeUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 synchronized. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327ExecutorService Callable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329Phaser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332

    12. JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .342, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

  • 7

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 . . . . . . . . . . . . . . . . . . . 350. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358Data Access Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358DAO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360DAO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362DAO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

    13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .376 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 TCP/IP. . . . . . . . . . . . . . . . . . . . . . . . . 382 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 UDP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

    14. XML & JAVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .395 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398DTD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 XSD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403JAXB. . . . . . . . . . . . . . . . . . . . . . . . . 412JAXB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417JAXP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424 . . . . . . . . . . . . . . . . . . . . 424 . . . . . . . . . . . . . . . . . . . . . . . . . . 425 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426SAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432DOM JAXP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436StAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438XSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

    3.web

    15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .456 . . . . . . . . . . . . . . . . . 462 JSP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 JSP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 ServletContext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 ServletConfig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 HttpServletRequest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

  • 8

    HttpServletResponse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

    16. JAVA SERVER PAGE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .485 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 expression language . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494Expression Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 EL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502 JSP JSP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515

    17. C, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .522 () . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522 Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535

    18. JSP STANDARD TAG LIBRARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544JSTL core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545 . . . . . . . . . . . 548 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554 . . . . . . . . . . . . . . . . . . . . . . . . . . 557JSTL formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558JSTL sql . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563JSTL xml. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564JSTL functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569

    19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .573 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586

    4.

    20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .592 GRASP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593 Expert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593 Creator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595 Low Coupling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596

  • High Cohesion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599 Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602

    21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .605 Factory Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606 Abstract Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611 Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615 Singleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620 Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623

    22. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .629 Chain of Responsibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630 Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638 Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 Mediator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652 Memento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657 Observer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668 Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678 Template Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682 Visitor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684 Interpreter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689

    23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .695 Bridge. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695 Decorator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703 Faade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709 Composite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713 Adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719 Flyweight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723 Proxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .735

    1. JUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .751 2. Log4J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .766 3. UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .780 4. SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .793 5. Apache Ant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .813 6. JPA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .827 7. Hibernate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .853 8. IDE Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .868

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .895

  • Java. - Java 2. 2005 Java. 2007 .

    Java -- - , EPAM Systems, Oracle Java. Java , Java .

    , , . Java-, , Java . , , - Java-. . , .

    . - Java - -. , , - XML. - , JSP. .

    - . .

    , - , Java-, Log4J JUnit, Apache Ant -, UML SQL.

    EPAM Systems -, .

  • 1

    Java

    , Java

    - .

  • 12

    1

    , .

    , .

    Java - , - .

    - - . , , 510 .

    - (), - .

    , , - . .

    , - .

    - - .

    ( ), , , . .

    . - .

    . ()

    ( ). , .

  • 13

    , . , -

    , , , , .

    , :

    id=35, masonry="brick", numberFloors=2, numberWindows=7.

    - : ; ; , , . (encapsulation) , , -

    , . , .

    (inheritance) , -, .

    : ( )

    ();

    ( Java ). (polymorphism) ,

    , - . - - .

    - ,

    . 2.1. () . 2.2.

  • JAVA

    14

    , . () - . , , - .

    : , - . , .

    . -, . , - . , - ( Object) .

    Java- Java, Sun

    Microsystems 1995 - , . Java - -, - , . - , . JavaScript, Adobe Flash .

    Java , - ++. Java ++. Java .

    ( ++) , , - . - . Java .

    Java , - ,

  • 15

    , , . , , JVM (Java Virtual Machine) .

    Java (heap) , , -, (stack). - , C++. , Java , - . , Java , - , , () . JVM - , . Java - : - . - ( ), - new. .

    C++, Java , - , , , , . Java , - ( ), goto const, .

    Java:

    abstract ontinue for new switchassert default goto* package synchronizedboolean do if private thisbreak double implements protected throwbyte else import public throwscase enum instanceof return transientcatch extends int short trychar final interface static voidclass finally long strictfp volatileconst* float native super while

    Java : null, true, false, - .

  • JAVA

    16

    .

    // # 1 # # OracleSlogan.java

    public class OracleSlogan { public static void main(String[ ] args) { // System.out.println("Enabling the Information Age"); }}

    . -, - , , , . System.out.println("Enabling the Information Age");

    . - , , , . - , , - , . , . , ? ( ) . - . , -- :

    /* # 2 # - # FirstProgram.java */

    package by.bsu.simple;public class FirstProgram { public static void main(String [ ] args) { // firstObject SloganAction firstObject = new SloganAction(); // , firstObject.printSlogan(); }}

  • 17

    // # 3 # # SloganAction

    class SloganAction {void printSlogan() { //

    // System.out.println("Enabling the Information Age");

    }}

    FirstProgram , main(), Java - . main() String[]args, - , (public) . , main() . static , - , . - . main() SloganAction firstObject = new SloganAction();

    firstObject.printSlogan();

    Enabling the Information Age println()(ln ) out System, java.lang. FirstProgram.java (.java ), - public-.

    package by.bsu.simple;

    by.bsu.simple, - . , , . package (unnamed), . - , - .

    2 3 , FirstProgram.java SloganAction.java. , - .

  • JAVA

    18

    /* # 4 # - # FirstProgram.java */

    package by.bsu.simple.run;import by.bsu.simple.action.SloganAction; // public class FirstProgram {

    public static void main(String[ ] args) {SloganAction firstObject = new SloganAction ();firstObject.printSlogan();

    }}

    // # 5 # # SloganAction.java

    package by.bsu.simple.action;public class SloganAction {

    public void printSlogan() {// System.out.println("Enabling the Information Age");

    }}

    - , by, bsu :javac by/bsu/simple/action/SloganAction.javajavac by/bsu/simple/run/FirstProgram.java

    FirstProgram.class SloganAction.class, . Java:java by.bsu.simple.run.FirstProgram

    FirstProgram.class by.bsu.simple.run, .

    , , :http://www.oracle.com/technetwork/java/javase/downloads/

    -. JDK ( Windows) c:\Java\jdk7, -, Java , : LASSPATH=.;c:\Java\jdk7\.

    . -, , :\workspace - .

    , PATH PATH=c:\Java\jdk7\bin.

  • 19

    javac.exe java.exe. JDK .

    , - . , . - , . - .

    , main(), - . , , - String[]args. args[0]- . args.length. args.length - , , -.

    /* # 6 # # PrintArguments.java */

    package by.bsu.arg;public class PrintArguments { public static void main(String[ ] args) { for (String str : args) { System.out.printf("A-> %s%n", str); } }}

    for printf(). forfor (int i = 0; i < args.length; i++) { System.out.println("A-> " + args[i]);}

    - :java by.bsu.arg.PrintArguments 2012 Oracle "Java SE 7"

    :

    ->2012->Oracle->JavaSE7

  • JAVA

    20

    , , - .

    Java

    Java , , , . C++: - ; , , ; - public,private,protected , - , , , ++; private, . :[] class [extends ] [implements _] {

    /* */ }

    public ( - ), final ( ), abstract ( , ). , , - (friendly). . friendly . , - - .

    , - extends, , - implements. - , - .

    : , . , - , . , , - , . , - . House numberWindows, . private , , , .

  • 21

    Coin .

    // # 7 # # Coin.java

    package by.bsu.fund.bean;public class Coin {

    public double diameter; // private double weight; // public double getDiameter() {

    return diameter;}public void setDiameter(double value) {

    if (value > 0) { diameter = value;

    } else {diameter = 0.01; //

    }}public double takeWeight() { // :

    return weight;}public void setWeight(double value) {

    weight = value;}

    }

    Coin diameter weight, public private. weight , , setWeight (double value). - , . diameter Coin. , , - , - , :

    // # 8 # # Runner.java

    package by.bsu.fund.run;import by.bsu.fund.bean.Coin;public class Runner {

    public static void main(String[ ] args) {Coin ob = new Coin();ob.diameter = -0.12; // : ob.setWeight(100);

    // ob.weight = -150; // : compile error}

    }

  • JAVA

    22

    ob.diameter = -0.12;

    , diameter Coin private double diameter;

    .

    // # 9 # (Java Bean) # Coin.java

    package by.bsu.fund.bean;public class Coin { private double diameter; // private double weight; // public double getDiameter() { return diameter; } public void setDiameter(double value) { if(value > 0) { diameter = value; } else { System.out.println("O !"); } } public double getWeight() { // return weight; } public void setWeight(double value) { weight = value; }}

    - setDiameter(doublevalue) - . public- .

    /* # 10 # , # CompareCoin.java # Runner.java */

    package by.bsu.fund.action;import by.bsu.fund.bean.Coin;public class CompareCoin { public void compareDiameter(Coin first, Coin second) { double delta = first.getDiameter() - second.getDiameter(); if (delta > 0) { System.out.println(" " + delta); } else if (delta == 0) {

  • 23

    System.out.println(" ");} else {

    System.out.println(" " + -delta);}

    }}package by.bsu.fund.run;import by.bsu.fund.bean.Coin;import by.bsu.fund.action.CompareCoin;public class Runner {

    public static void main(String[ ] args) {Coin ob1 = new Coin();ob1.setDiameter(-0.11); // ob1.setDiameter(0.12); // ob1.setWeight(150);Coin ob2 = new Coin();ob2.setDiameter(0.21);ob2.setWeight(170);CompareCoin ca = new CompareCoin();ca.compareDiameter(ob1, ob2);

    }}

    - :O!0.09.

    . - . new , :Coin ob1; // ob1 = new Coin(); //

    :Coin ob1 = new Coin(); /* */

    new , , -, , -. , - .

    compareDiameter(Coinfirst,Coinsecond) , : . , . - int .

  • JAVA

    24

    /* # 11 # # */

    public int compareDiameter(Coin first, Coin second) {int result = 0;double delta = first.getDiameter() - second.getDiameter();if (delta > 0) {

    result = 1;} else if (delta < 0) {

    result = -1;}return result;

    }

    .

    Java , .

    , , . , , equals(Object ob). Object, - .

    /* # 12 # # ComparisonStrings.java */

    package by.bsu.strings;public class ComparisonStrings {

    public static void main(String[ ] args) {String s1, s2;s1 = "Java";s2 = s1; // System.out.println(" " + (s1 == s2)); // true// s2 = new String("Java"); // s2 = new String(s1);System.out.println(" "+ (s1 == s2)); // falseSystem.out.println(" " + s1.equals(s2)); // true

    }}

    s2=s1 , - . == true , .

    s2=newString(s1), ,

  • 25

    , s1. , - , . false, . - , equals(Objecto).

    - , , - equals(Objecto) - , , .

    , - .

    ( ) System.in . -. , , .

    // # 13 # System.in # ReadCharRunner.java

    package by.bsu.console;public class ReadCharRunner { public static void main(String[ ] args) { int x; try { x = System.in.read(); char c = (char)x; System.out.println(" : " + c + " =" + x); } catch (java.io.IOException e) { System.err.println(" " + e); } }}

    IOException, - / - , main()

  • JAVA

    26

    try-catch. , try{}, , - catch{}.

    - Scanner, - : , , , , , .

    // # 14 # # RunScanner.java

    package by.bsu.console;import java.util.Scanner;public class RunScanner {

    public static void main(String[ ] args) {System.out.println(" :");Scanner scan = new Scanner(System.in);String name = scan.next();System.out.println(", " + name);scan.close();

    }}

    , , ::,.

    Scanner, - , , , .

    Base code conventions , , ,

    . - . , .

    a : Coin, Developer. ,

    : AncientCoin, FrontendDeveloper.

    : perform(), execute(). ,

    : performTask(), executeBaseAction().

  • 27

    , - : weight,price.

    , , - : priceTicket,typeProject.

    : DISCOUNT, MAX_RANGE.

    . , : 10 . .

    1

    A

    1. - .

    2. .

    3. .

    4. -.5. , -

    () .6. , ,

    .

    B

    n . :1. .2. .3. , 3 9.4. , 5 7.5. , .6. ,

    .7. .8. .9. .10. .11. .

  • JAVA

    28

    12. : f0 = f1 = 1, f (n) = f (n1) + f (n2).13. -, -

    .14. , .15. p = m/n

    n m, .16. .

    1

    1.1.

    :A)public class Quest21 {public static void main (String [] args) {System.out.println ("Hello, java 7");}}

    B)public class Quest22 {String java = "Java 7";public static void main (String [] args) {System.out.println (java);}}

    C)public class Quest23 {{System.out.println ("Java 7");}}

    , ( 1).1) AB2) BC3) ABC4) A5) AC

    1.2.

    (2):1) ;2) ,

    ;3) ;4) .

  • 29

    1.3. ,

    (1):1) 2) 3) 4)

    __________ , .

    1.4. :

    String s; // 1if ((s = "java") == "java") {// 2

    System.out.println (s+ " true");} else {

    System.out.println (s+ " false");}

    (1)?1) 1, 2) 2, if3) javatrue4) javafalse

    1.5. :

    public class Quest5 {private static void main (String [] args) {

    System.out.println (args [2]);}

    } , :

    java Quest5 Java 7"" (1)?1) : Java 72) NullPointerException3) ArraylndexOutOfBoundsException4) : Java 7 5) : 6) 7)

  • JAVA

    1.6. :

    public class Quest6 {public static void main (String [] args) {

    System.out.print ("A");main ("java7");

    }private static void main (String args) {

    System.out.print ("B");}

    }

    (1)?1) 2) BA3) AB4) AA5) ,

    1.7. :

    class Book {private String book;public void setBook (String b) {book = b;}

    }public class Quest7 {

    public static void main (String [] args) {Book book1 = new Book (); book1.setBook ("Java 7");Book book2 = new Book (); book2.setBook ("Java 7");if (book1.equals (book2)) {

    System.out.println ("True");} else {

    System.out.println ("False");}

    }}

    (1)?1) True2) 3) False4) , if .

  • 31

    2

    .

    -. .

    Java - ,

    . - , - . -, - , (heap). - - .

    , (. 2.1.).

    Java . . - .

    Java , : 35 - , 071 , 051b -, 0b1010 ( Java 7). int. long, L (: 0xffffL). , int (2147483647), Java , long.

    Java 7 _ , intm=7000000 intm=7_000_000. . : _12 21_.

  • JAVA

    32

    1.618 - 0.112E-05 double. , - double. - float, F f. - IEEE 754 +Infinity Infinity - NaN (Not a Number), , , - .

    true false. null - .

    ('a', '\n', '\141', '\u005a'). Unicode, - . Unicode , - ASCII , C++. '\ucode', code - . Java , ; '\n' , '\r' , '\f' , '\t' , '\b' ,'\uxxxx' Unicode, '\ddd' - . Java 7 Unicode 6.0.0 - Character.

    , , - , String.

    . 2.1.

  • 33

    String. String - + - () . - - . '\0', ASCII-, () .String s = "clown";// s += '2';s = s + 4;s += new Double(3.14159D);// "+", // s-="c"; // , . "-"

    byteshortintlong floatdouble.

    , , . , double long double.

    Java byte short int - . - (). :int i = 5;byte b = (byte)i; // int byte

    - - , . - , . - ++, - (+=, /= . .). () - .

    /* # 1 # , */

    byte b = 1, b1 = 1 + 2;final byte B = 1 + 2;//b = b1 + 1; // int byte /* b1 b = b1 + 1; , b1 + 1 byte- */

  • JAVA

    34

    b = (byte)(b1 + 1);b = B + 1; // /* B - , , B + 1, byte , *///b = -b; // b = (byte) -b;//b = +b; // b = (byte) +b;int i = 3;//b = i; // , int , byteb = (byte) i;final int D = 3;b = D; // /* D . , byte, , */final int D2 = 129;//b=D2; // , .. 129 , 127 b = (byte) D2;

    b += i++; // b += 1000; // b1 *= 2; // float f = 1.1f;b /= f; // /* , . , b /= f; b = (byte)(b / f); */

    Java , . , - , '#'. '$' '_' -, . .

    , , , . - , - , - . {}, .

    Java /* */ //, , C++. /***/, - :

  • 35

    . 2.2.

    . 2.3. Object

  • JAVA

    36

    @author ;@version ;@exception ;@param , ;@return , ;@deprecated , -

    ;@since , ( , ) ;@throws , ;@see .

    java-, , javadoc.exe html-. - Java , - (. 2.3.).

    /* # 2 # Object # Object.java */

    package java.lang;/*** Class {@code Object} is the root of the class hierarchy.* Every class has {@code Object} as a superclass. All objects,* including arrays, implement the methods of this class. ** @author unascribed* @see java.lang.Class* @since JDK1.0 */public class Object {/**

    * Indicates whether some other object is "equal to" this one.*

    * MORE COMMENTS HERE* @param obj the reference object with which to compare.* @return {@code true} if this object is the same as the obj

    * argument; {@code false} otherwise.* @see #hashCode()* @see java.util.HashMap

    */ public boolean equals(Object obj) { return (this == obj); }

    /*** Creates and returns a copy of this object.* MORE COMMENTS HERE* @return a clone of this instance.

  • 37

    * @exception CloneNotSupportedException if the object's class does not * support the {@code Cloneable} interface. Subclasses * that override the {@code clone} method can also * throw this exception to indicate that an instance cannot * be cloned.

    * @see java.lang.Cloneable */ protected native Object clone() throws CloneNotSupportedException;

    // more code here }

    , , . - . , -. , , - .

    Java C++ - , 2.4. - Java , C: * (); &; -->. , , - . + += String. ==, != = , . - , -. -, .

    . 2.4.

    =

  • JAVA

    38

    ::

    int a = 2, b = 3, c = 4, d = 5, r;r = a + b * c - d;

    , , - .

    , .

    + / (

    ) += ( ) /= ( )

    % ( -

    ) =

    ( ) %=

    ( ) * ++ (

    ) *=

    ( ) (

    )

    | >> |= ( ) >>= ( ) & >>> &= (c ) >>>=

    ^

  • 39

    || && !

    boolean Boolean (true false).

    // # 3 # %

    System.out.println("5%1=" + 5%1 + " 5%2=" + 5%2); int b1 = 0b1110; //14 int b2 = 0b1001; // 9 int i = 0;System.out.println(b1 + "|" + b2 + " = " + (b1|b2));System.out.println(b1 + "&" + b2 + " = " + (b1&b2));System.out.println(b1 + "^" + b2 + " = " + (b1^b2));System.out.println( "~" + b2 + " = " + ~b2);System.out.println(b1 + ">>" + ++i + " = " + (b1>>i));System.out.println(b1 + ">" + i +" = " + (b1>>>i));

    :5%1=05%2=114|9=1514&9=814^9=7~9=-1014>>1=714>2=3

    - instanceof ?: (if-then-else).

    ?: :boolean_ ? _ : _

    boolean_ true, _, , - _. ,int defineBonus(int purchaseItem) { int bonus; bonus = purchaseItem > 3 ? 10 : 0 ; return bonus;}

    , bonus - , .

  • JAVA

    40

    , , :experience > requirements ? acceptToProject() : learnMore();

    instanceof true, - . , :

    /* # 4 # : */

    class Course {/* */}class BaseCourse extends Course {/* */}class FreeCourse extends BaseCourse {/* */}class OptionalCourse extends Course {/* */}

    instanceof doAction(Coursec):void doAction(Course c) { if (c instanceof BaseCourse) {/* BaseCourse FreeCourse */ } else if (c instanceof OptionalCourse) {/* OptionalCourse */ } else {/* Course null */}}

    instanceof , , . Object , - Object. - null , null - - . - null - . null , instanceof.

    , Java -

    - (wrapper-) java.lang: Boolean, Character, Integer,Byte, Short, Long, Float, Double. , .

    - , .

    . 2.5.

  • 41

    . , , java.lang, Number Comparable. - int compareTo(Tob). - null.

    - String .

    - - Value() .

    Character Number, - , - . Character - . Character, , - String.

    /* # 5 # */

    Float ft = new Float(1.7); // double FloatShort s = new Short((short)5); // int ShortShort sh = new Short("5"); // String Shortdouble d = s.doubleValue(); // Short double

    . 2.6. -

  • JAVA

    42

    byte b = (byte)(float)ft; // Float byteCharacter ch = new Character('3'); int i = Character.digit(ch.charValue(), 10); /* Character int */

    - String valueOf(Stringstr),decode(Stringstr) parse(Stringstr) - , , - . Character. , . - try-catch .

    :

    /* # 6 # # StringToInt.java */

    package by.bsu.transformation;public class StringToInt { public static void main(String[ ] args) { String arg = "71"; // 071 0x71 0b1000111 try { int value1 = Integer.parseInt(arg); // int int value2 = Integer.valueOf(arg); // Integer int value3 = Integer.decode(arg); // Integer int value4 = new Integer(arg); /* Integer, */ } catch (NumberFormatException e) { System.err.println(" " + e); } }}

    .

    ( int) :int value = 71;String arg1 = Integer.toString(value); // String arg2 = String.valueOf(value); // String arg3 = "" + value; //

    java.math.BigInteger java.math.BigDecimal, .

    5.0, (/

  • 43

    ). - new:Integer iob = 71; // Integer iob = new Integer(71);

    - - , Float f = 7; // (float)7 7F 7

    .C , :

    Float f = new Float("7");

    - - . intValue(), doubleValue() - .

    , - , / - :// autoboxing & unboxing: Integer i = 71; // +++i; // ++ + int j = i; //

    NullPointerException null Integer, null - :Integer j = null; // ! !int i = j; //

    , - -, -. equals().int i = 128; // 127 !!!Integer a = i; // +Integer b = i;System.out.println("a==i " + (a == i)); // true System.out.println("b==i " + (b == i)); // trueSystem.out.println("a==b " + (a == b)); /* false( ) */System.out.println("equals ->" + a.equals(i)

    + b.equals(i)+ a.equals(b)); // true, true, true

    equals() , -, . a.equals(b)- true.

  • JAVA

    44

    equals(). :i.equals(a); //

    , , Number, :Number n1 = 1; // new Integer(1)Number n2 = 7.1; // new Double(7.1)

    .

    . , -

    - .

    if :

    if (boolean_) { /* */ } // 1else { /* */ } // 2

    boolean_ true, - 1, 2. else , , - if( 2), - if.if (counter > 1) {

    System.out.println("Value is Valid");} else {

    System.out.println("Value is Broken");}

    if (counter > 1) {

    System.out.println("Value is Valid");}System.out.println("More Opportunities"); //

    if , if (condition)

    baseCode(); // restOfCode();

  • 45

    , -, checkRole() baseCode(). - , baseCode()- condition.if (condition)

    checkRole(); // baseCode(); // ! restOfCode();

    . - . , , .if (condition) {

    checkRole();baseCode();

    }restOfCode();

    . - if-else-if. switch:

    switch(value) { case val1: /* */

    break; /* */ ... case valN: /* */

    break; default: /* */ }

    value val1 - . , break, , break. value case, - default. val1,,valN int, byte, short, char enum. Java7 String:

    /* # 7 # String switch */

    public int defineLevel(String role) { int level = 0; switch (role) { // role.toLowerCase()

    case "guest": level = 1;break;

  • JAVA

    46

    case "client": level = 2; break; case "moderator": level = 3; break; case "admin": level = 4; break; default: throw new IllegalArgumentException(); // } return level; }

    String - case. - --, , - .

    , . if , , else . if else, , null.if (obj == null) { throw new IllegalArgumentException(); // }

    switch case, , - , default. if-else-if switch-case - .

    Java . :while (boolean_) { /* */ } //

    do { /* */ } while (boolean_); //

    for (_1; boolean_; _3) { /* */ } //

    , boolean_ true. , , _1 -

    , boolean_ , _3 -, ( , - ).

    5.0 , -:for ( : ) { /* */ } //

  • 47

    .int[] arr = {1, 3, 5};for (int elem : arr) { // System.out.printf("%d ", elem); // }

    . , Iterable Iterator.

    : for -

    . while do {} while - , -, , . while(true){} - ;

    for ; for break; ; .

    ; . Java break

    continue, - , :int j = -3;OUT: while(true) { for(;;) { while (j < 10) { if (j == 0) { break OUT; } else { j++; System.out.printf("%d ", j); } } }}System.out.print("end");

    break , OUT. goto . - .

  • JAVA

    48

    Java , - , . , , . , - . , : int a[]. int[]a.

    Java . : new - . , , null .

    /* # 8 # */

    int arRef[ ], ar; // float[ ] arRefFloat, arFloat; // ! // int arDyn[ ] = new int[10]; // 10 String str[ ] = new String[7]; // 7 null- /* */ int arInt[ ] = { 5, 7, 9, -5, 6, -2 }; // 6 arInt[ ] = new int[ ] { 5, 7, 9, -5, 6, -2 }; // byte arByte[ ] = {1, 3, 5 };// 3 /* Object */ Object arObj = new float[5]; // // arRef = arDyn; arDyn = arInt; arRefFloat = (float[ ])arObj; arObj = arByte; // arRefFloat = (float[ ])arObj; // // () // arInt = arByte; // arInt = (int[ ])arByte;

    Object . -, . , . .

  • 49

    arDyn=arInt , - arDyn arInt, .

    , . length.

    Java , . , :int arr[ ][ ] = { { 1 }, { 2, 3 }, { 4, 5, 6 }, { 7, 8, 9, 0 } };

    , , arr[2][0] , , 4.

    . , - null. -.

    2

    A

    -, , . /** */, html- . .

    n .1. .

    .2. ()

    .3. , () ,

    .4. , .

    , .5. , ,

    .

  • JAVA

    50

    6. , . , .

    7. , . -, .

    8. -. , .

    B

    1. k (n, m], [n, m), (n, m), [n, m].

    2. 1 k N x N .3. .

    .4. 1 12. , -

    . .

    C

    n- a [n] [n]. - -n n -.1. ()

    k- ().2. k

    (, , ).3. () -

    , .4. ,

    .5. .6. .7. 90 (180, 270) .8. .9. , -

    .10. ()

    , .11. , , .12. -

    .13. , ,

    , .

  • 51

    14. .15. . ( -

    i, j, i, j i-

    j- ).16. , , -

    .17. . (

    , . - , .)

    18. . ( - , .)

    19. , , - . ( - .)

    20. , , - (2, 2), - (3, 3) . ., .

    2

    2.1.

    , (3):1) int var1 = 356f2) double var2 = 356f3) float var3 = 356f4) char var4 = 356f5) long var5 = 356f6) byte var6 = 356f7) Integer var7 = 356f8) Character var8 = 356f9) Object var9 = 356f

    2.2.

    , javadoc- - (2):1) @see2) @author3) @param4) @version

  • JAVA

    52

    5) @throws6) @deprecated

    2.3.

    :public class Quest4 {

    public static void main (String [] args) {double x=0, y=2, z;z = y/x;System.out.println ("z="+z);

    }}

    (1)?1) 2) z=Infinity3) z=NaN4) java.lang.ArithmeticException

    2.4.

    (1)?public class Quest {

    public static void main (String [] args) {MedicalStaff medic = new HeadDoctor ();if (medic instanceof Nurse) {

    System.out.println ("Nurse");} else if (medic instanceof Doctor) {

    System.out.println ("Doctor");} else if (medic instanceof HeadDoctor) {

    System.out.println ("HeadDoctor");}

    }}class MedicalStaff {}class Doctor extends MedicalStaff {}class Nurse extends MedicalStaff {}class HeadDoctor extends Doctor {}1) Nurse2) Doctor3) HeadDoctor4)

  • 2.5.

    if (e1) if (e2) S1; else S2; (e1, e2, S1, S2 java-). (2)?1) if (e1) {if (e2) S1; else S2;}2) if (e1) {if (e2) S1;} else S2;3) if (e1) if (e2) S1; else; else S2;4) if (e1) if (e2) S1; else S2; else;

    2.6.

    100 (2)?1) i = 1; sum = 0; for (; i

  • 54

    3

    .

    - , , .

    , - . , - , , . . , , - .

    . - / . (: , ), (: C, ).

    - ( Object) :[public] [final] [abstract] class { {} // //

    // private // public // // ( ) protected //

    // public // // ( ) protected // private //

    // public //

  • 55

    // ( ) protected // private // }

    public (enclosing) , .

    ,

    / - () , , - , .

    . :[c] ;

    static - , . static , . , , public, private, protected . -, -. , -, .

    final . final - , , , . , - .

    , - :

    /* # 1 # # Order.java */

    package by.bsu.entity;// public class Order { private int id; // static int bonus; // public final int MIN_TAX = 8 + (int)(Math.random()*5); // public final static int PURCHASE_TAX = 6; //

  • JAVA

    56

    // // public double calculatePrice(double price, int counter) { /* */ double amount; // // amount++; // , amount = (price - bonus) * counter; // double tax = amount * PURCHASE_TAX /100; return amount + tax; // }}

    , , - . , new.

    Java , - . - - Java : private ; (package-private) , -

    ; protected , ,

    ; public .

    (, ), public, . public - public- , . , .

    , private, - . , ( ) . (private) - .

    , - -. . public- public- -.

  • 57

    , , - , , protected. , - - . protected .

    , .

    , - new. - , - .

    , ; , new . , -. , .

    Java , ( ). finalize(), . , , .

    Account c (), - .

    /* # 2 # # Account.java */

    package by.bsu.transfer.bean;public class Account { private long id; private double amount; // public Account() { // super();

    /* , */

    } // public Account(long id) {

  • JAVA

    58

    super(); /* , */

    this.id = id; } // public Account(long id, double amount) { this.id = id; this.amount = amount; } public double getAmount () { return amount; } public void setAmount (double amount) { this.amount = amount; } public long getId() { return id; } public void setId(long id) {

    // this.id = id; } public void addAmount (double amount) { /* */ this.amount += amount; }}

    () this , . , . id this.id, , id this.id . , , this . , .

    Account , : Account a = new Account(); /* , id */ Account b = new Account(71L); /* */ Account = new Account(71L, 0.7); /* */

    new , - , .

  • 59

    , , , , : 0, false, null. , . . - . , super(_). .

    . Java -

    . : ( ) { // return ; /* ( void)*/}

    , return :return ;

    void. void , . ( ):objectName.methodName();

    , , . - ( void) -. , - , .[] [static] [abstract] [final] [synchronized] [native] []

    () [throws ]

    , public, private, protected . - .

  • JAVA

    60

    , - .

    - -, , . . , - .

    /* # 3 # # TransferAction.java */

    package by.bsu.transfer;import by.bsu.transfer.bean.Account;public class TransferAction { private double transactionAmount; public TransferAction(double amount) { // if (amount > 0) { this.transactionAmount = amount; } else { throw new IllegalArgumentException(); // } } public boolean transferIntoAccount(Account from, Account to) { // double demand = from.getAmount() - transactionAmount; // if (demand >= 0) { from.setAsset(demand); to.addAsset(transactionAmount); return true; } else { return false; } } public double getTransactionAmount () { return transactionAmount; }// }

    - .

    /* # 4 # # Runner.java */

    package by.bsu.transfer;import by.bsu.transfer.bean.Account;public class Runner { public static void main(String[ ] args) {

  • 61

    Account from = new Account(78031864L, 258.5); Account to = new Account(58510009L, 12.1); TransferAction action = new TransferAction(52.0); boolean complete = action.transferIntoAccount(from, to); if (complete) { System.out.println(": " + action.getTransactionAmount() + " "); System.out.print(" ID=" + to.getId()); System.out.println(" : " + to.getAmount()); } else { System.out.println(" ."); System.out.print(" ID=" + from.getId()); System.out.println(" ."); } }}

    ::52.0ID=58510009:64.1

    Runner -. ReportAction .

    , - public. private , . protected .

    , static, - . . , . , static. - . , this , . - , . this

  • JAVA

    62

    , , , - . - , . TransferAction .

    // # 5 # # TransferAction.java

    public class TransferAction { public static double transactionAmount; // private int id; // public static boolean transferIntoAccount(Account from, Account to) { // increaseAmount(); // // this.id // this // id // // double demand = from.getAmount() - transactionAmount; // if (demand >= 0) { from.setAsset(demand); to.addAsset(transactionAmount); return true; } else { return false; } } public void increaseAmount() { // transactionAmount ++; }}

    transferIntoAccount() :TransferAction.transferIntoAccount(from, to);

    .

    TransferAction ob1 = new TransferAction();TransferAction ob2 = new TransferAction();

    ob1.transactionAmount ob2.transactionAmount 0, - . . :TransferAction.transactionAmount = 70.5;

    - , . , ,

  • 63

    - , .

    , , .

    - , .

    final

    final , . , final, . , - final, . :

    /* # 6 # final- # Card.java */

    package by.bsu.finalvar;public class Card { // public final int ID = (int)(Math.random() * 10_000_000); // public final long BANK_ID; // ! // { BANK_ID = 11111111L; } // !!! public Card (long id) { // BANK_ID = id; // !!! } public final boolean checkRights(final int NUMBER) { final int CODE = 72173394; // : " " // ID = 1; // ! // NUMBER = 1; // ! // CODE = 1; // ! return CODE == NUMBER + ID; }}

    , -. , {}, , - . . - . - .

  • JAVA

    64

    , .

    /* # 7 # # AbstractCardAction.java */

    public abstract class AbstractCardAction { private Long account; public AbstractCardAction () { } /* */ public abstract void doPayment(double amountPayment); public void setAccount(Long account) { this.account = account; }}

    AbstractCardAction ap = new AbstractCardAction(); // compile error

    abstract , .

    , , .

    .

    native

    Java , ++. native, , . :public native int loadCripto(int num);

    , native, .

    synchronized

    , . synchronized, , - .

  • 65

    notifyAll(), notify() wait() Object ( Java) synchronized, .

    . , , :{ /* */ }static { /* */ }

    , . , - , - , . this, .

    static. () - .

    /* # 8 # # Department.java # DemoLogic.java */

    package by.bsu.logic;public class Department { { System.out.println("logic (1) id=" + this.id); // } static { System.out.println("static logic"); /* , () */ } private int id = 7; public Department(int id) { this.id = id; System.out.println(" id=" + id); }

  • JAVA

    66

    public int getId() { return id; } { /* */ System.out.println("logic (2) id=" + id); }}package by.bsu.logic;public class DemoLogic { public static void main(String[ ] args) { new Department(71); new Department(17); }}

    :

    staticlogiclogic(1)id=0logic(2)id=7id=71logic(1)id=0logic(2)id=7id=17

    id , , -. id, 7, , - .

    , - , . , . . - , - . , - .

    , , .

    , - .

  • 67

    /* # 9 # # NumberInfo.java */

    package by.bsu.overload;public class NumberInfo { public static void viewNum(Integer i) { // 1 System.out.printf("Integer=%d%n", i); } public static void viewNum(int i) { // 2 System.out.printf("int=%d%n", i); } public static void viewNum(Float f) { // 3 System.out.printf("Float=%.4f%n", f); } public static void viewNum(Number n) { // 4 System.out.println("Number=" + n); } public static void main(String[ ] args) { Number[ ] num = {new Integer(7), 71, 3.14f, 7.2 }; for (Number n : num) { viewNum(n); } viewNum(new Integer(8)); viewNum(81); viewNum(4.14f); viewNum(8.2); }}

    , - , :Number=7Number=71Number=3.14Number=7.2Integer=8int=81Float=4,1400Number=8.2

    . , - num. , ( ), , .

    - .

  • JAVA

    68

    , , - - , , - .

    : ; ; -

    .

    J2SE 5 -

    (generic) , , . generic- - . , , - .

    generic- :

    /* # 10 # # Post.java */

    package by.bsu.forum;public class Post { private T1 message; private T2 id; // }

    T1, 2 , . T2 - Number. - .

    Post , , :Post post1 = new Post();Post post2 = new Post();

    Post post2 = new Post(); // diamond Java 7

    . post1=post2 .

    - ,

  • 69

    , - :post1 instanceof Post

    , post1 instanceof Post

    Message - , .

    /* # 11 # # Message.java # Runner.java */

    package by.bsu.template;public class Message { private T value; public Message() { } public Message (T value) { this.value = value; } public T getValue() { return value; } public void setValue(T value) { this.value = value; } public String toString() { if (value == null) { return null; } return value.getClass().getName() + " :" + value; }}package by.bsu.template;public class Runner { public static void main(String[ ] args) { // Integer Message ob1 = new Message(); ob1.setValue(1); // Integer setValue int v1 = ob1.getValue(); System.out.println(v1); // String Message ob2 = new Message("Java"); String v2 = ob2.getValue(); System.out.println(v2);

    // ob1 = ob2; //

  • JAVA

    70

    // Object Message ob3 = new Message(); // warning raw type ob3 = ob1; // System.out.println(ob3.getValue()); ob3.setValue(new Byte((byte)1)); ob3.setValue("Java SE 7"); System.out.println(ob3); /* , */ ob3.setValue(71); System.out.println(ob3); ob3.setValue(null); }}

    :1Javanulljava.lang.String:JavaSE7java.lang.Integer:71

    Message: ob1 - Integer ob2 String -. generic- Object, - , ob3. - , .

    generic- , - , - . Object. , - .

    , - :public class ValueExt { private T value; // , , }

    , , () T, , , - (bound) .

    ,

  • 71

    . ?. extends .

    /* # 12 # # Exam.java # Runner.java */

    package by.bsu.exam;public class Exam { private String name; private T mark; // public Exam(T mark, String name) { // this.name = name; this.mark = mark; } public T getMark() { // return mark; } private int roundMark() { return Math.round(mark.floatValue()); // Number } public boolean equalsToMark(Exam ob) { // return roundMark() == ob.roundMark(); }}package by.bsu.exam;public class Runner { public static void main(String[ ] args) { Exam md1 = new Exam(71.41D,"Progr");// 71.5d Exam md2 = new Exam(71.45D, "Progr");// 71.5d System.out.println(md1.equalsToMark(md2)); Exam mi = new Exam(71,"Progr"); // md1.equalsToMark(mi); // : }}

    :

    true

    Exam , . equalsToMark() Exam, , - ? :

    public boolean equalsToMark(Exam ob) { return roundMark() == ob.roundMark();}

  • JAVA

    72

    md1.equalsToMark(mi) Exam, . - .

    generic- . , - generic-:class FailedOne { private T value = new T();}

    , .

    generic- , - generic- generic-, :

    /* # 13 # # FailedTwo.java */

    class FailedTwo { static T1 value; T2 id; static T1 takeValue() { return value; } static void use() { System.out.print(id); }}

    (generic) , , - , , , : returnType methodName(T arg) { } T[ ] methodName(int count, T arg) { }

    . - , , - , extends. - .

    Generic- , .

  • 73

    generic-. , , , -. generic-.

    /* # 14 # # SimpleActionCourse.java */

    package by.bsu.genericmethod;public class SimpleActionCourse { public SimpleActionCourse(T1 course) { // // } public SimpleActionCourse() { // // } public float calculateMark(T3 course) { // } public boolean printReport(T4 course) { // } public void check() { // }}

    - :SimpleActionCourse sap = new SimpleActionCourse(new Course());sap.printReport(new Course(7112));

    SimpleActionCourse sa = new SimpleActionCourse();

    sa.check();

    , . - . -, .

  • JAVA

    74

    Java 5, - , - . -, , , - . printf() .

    :(... args)

    (1 t1, 2 t2, N tn, ... args)

    /* # 15 # # DemoVarargs.java */

    package by.bsu.varargs;public class DemoVarargs { public static int defineArgCount(Integer... args) { if (args.length == 0) { System.out.print("No arg "); } else { for (int element : args) { System.out.printf("arg:%d ", element); } } return args.length; } public static void main(String args) { System.out.println("N=" + defineArgCount(7, 71, 555)); Integer[] arr = { 1, 2, 3, 4, 5, 6, 7 }; System.out.println("N=" + defineArgCount(arr)); System.out.println(defineArgCount()); // defineArgCount(arr, arr); // // defineArgCount(71, arr); // }}

    :

    arg:7arg:71arg:555N=3arg:1arg:2arg:3arg:4arg:5arg:6arg:7N=7Noarg0

    . defineArgCount() - . - . . .

  • 75

    , - :methodName([ ]... args) { }

    :void methodName(Integer...args) { }void methodName(int x1, int x2) { }

    :void methodName(Integer ... args) { }void methodName(Integer[ ] args) { }

    - . - Objectargs , :

    /* # 16 # # DemoOverload.java */

    package by.bsu.overload;public class DemoOverload { public static void printArgCount(Object... args) { // 1 System.out.println("Object args: " + args.length); } public static void printArgCount(Integer[ ]...args){ // 2 System.out.println("Integer[ ] args: " + args.length); } public static void printArgCount(int... args) { // 3 System.out.print("int args: " + args.length); } public static void main(String[ ] args) { Integer[] i = { 1, 2, 3, 4, 5 }; printArgCount(7, "No", true, null); printArgCount(i, i, i); printArgCount(i, 4, 71); printArgCount(i); // 1 printArgCount(5, 7); // printArgCount(); // ! }}

    :

    Objectargs:4Integer[]args:3Objectargs:3Objectargs:5intargs:2

  • JAVA

    76

    printArgCount() i Objectargs. , . Integer[]args , Object[]args. Integer[]args - - Objectargs.

    - - .

    . , args

    , : (1args,2obj) (1args,2args)- .

    (typesafe enums) Java java.lang.Enum. class enum. - - new. - .

    - :

    /* # 17 # # MusicStyle.java # MelomanRunner.java */

    package by.bsu.enums;public enum MusicStyle { JAZZ, CLASSIC, ROCK, BLUES}package by.bsu.enums;public class MelomanRunner { public static void main(String[ ] args) { MusicStyle ms = MusicStyle.CLASSIC; // System.out.print (ms); switch (ms) { case JAZZ: System.out.println(" is Jazz"); break; case CLASSIC: System.out.println(" is Classic"); break;

  • 77

    case ROCK: System.out.println(" is Rock"); break; default: System.out.println(" Unknown music style: " + ms); } }}

    case -, switch.

    Enum , - , . enum :

    staticenumType[]values() , - ;

    staticTvalueOf(ClassenumType,Stringarg) , ;

    staticenumType valueOf(Stringarg) , - ;

    intordinal() ;Stringname() ;intcompareTo(Tobj) - .

    :

    MusicStyle ms = MusicStyle.valueOf("RocK".toUpperCase());

    , , , .

    /* # 18 # # Shape.java */

    package by.bsu.enums;public enum Shape { RECTANGLE, TRIANGLE, CIRCLE; // public double defineSquare(double ... x) { // switch (this) { case RECTANGLE: return x[0] * x[1]; case TRIANGLE: return x[0] * x[1] / 2; case CIRCLE:

  • JAVA

    78

    return Math.pow(x[0], 2) * Math.PI; default: throw new EnumConstantNotPresentException(this.getDeclaringClass(),this.name());

    }}

    }

    /* # 19 # # ShapeRunner.java */

    package by.bsu.enums;public class ShapeRunner {

    public static void main(String args[ ]) {double x = 2, y = 3;Shape sh;sh = Shape.RECTANGLE;System.out.printf("%10s = %5.2f%n", sh, sh.defineSquare (x, y));sh = Shape.TRIANGLE;System.out.printf("%10s = %5.2f%n", sh, sh.defineSquare (x, y));sh = Shape.CIRCLE;System.out.printf(" %10s = %5.2f%n", sh, sh.defineSquare (x));

    }}

    :

    RECTANGLE=6,00TRIANGLE=3,00CIRCLE=12,57

    - , defineSquare(). throw- , - . - . case.

    , . - . public protected, . - , .

    /* # 20 # # TaxiStation.java # TaxiRunner.java */

    package by.bsu.enums;public enum TaxiStation {

    MERCEDES(10), TOYOTA(7), VOLVO;

  • 79

    private int freeCabs; // TaxiStation() { // } TaxiStation(int cabs) { // freeCabs = cabs; } public int getFreeCabs() { return freeCabs; } public void setFreeCabs(int cabs) { freeCabs = cabs; } @Override public String toString() { return String.format("%s : free cabs = %d", name(), freeCabs); }}package by.bsu.enums;public class TaxiRunner { public static void main(String[ ] args) { TaxiStation ts = TaxiStation.valueOf(TaxiStation.class,"Volvo".toUpperCase()); System.out.println(ts + " : ordinal -> " + ts.ordinal()); ts.setFreeCabs(3); TaxiStation[ ] station = TaxiStation.values(); for (TaxiStation element : station) { System.out.println(element); } }}

    :

    VOLVO:freecabs=0:ordinal->2MERCEDES:freecabs=10TOYOTA:freecabs=7VOLVO:freecabs=3

    toString() Enum . toString() -, , , , - .

    . :

    ; ; ; , new.

  • JAVA

    80

    Immutable

    , , :

    /* # 21 # # ImmutableObject.java */

    package by.bsu.immutable;public class ImmutableObject { private String name; private int id; public ImmutableObject (String name, int id) { this.name = name; this.id = id; } public int getName() { return name; } public int getId() { return id; }}

    , -. - Java . String.

    Immutable, , , -, , .

    . - -. , - (). , - () , , , , -- , , .

  • 81

    - , .

    . .

    . : , - .

    , , , - , :

    /* # 22 # # Matrix.java */

    public class Matrix { private int[ ][ ] a; private int n; private int m; public Matrix(int nn, int mm) { n = nn; m = mm; // a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = (int)(Math.random() * 5); } } show(); } public Matrix(int nn, int mm, int k) { n = nn; m = mm; a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = k; } } if(k != 0) { show(); } } public void show() { System.out.println("Ma : " + a.length + " " + a[0].length); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { System.out.print(a[i][j] + " ");

  • JAVA

    82

    } System.out.println(); }}public static void main(String[ ] args) { int n = 2, m = 3, l = 4; Matrix p = new Matrix(n, m); Matrix q = new Matrix(m, l); Matrix r = new Matrix(n, l, 0); for (int i = 0; i < p.a.length; i++) { for (int j = 0; j < q.a[0].length; j++) { for (int k = 0; k < p.a[0].length; k++) { r.a[i][j] += p.a[i][k] * q.a[k][j]; } } } System.out.println(" : "); r.show(); }}

    , -: ; , ; ,

    ; ,

    , . - ;

    show() , ;

    main(), , . . - ;

    , , - , , , - . () -

    , - .

    /* # 23 # # Matrix.java */

    package by.bsu.task.entity;import by.bsu.task.exceptions.MatrixException;public class Matrix {

  • 83

    private int[ ][ ] a; public Matrix(int n, int m) throws MatrixException { // if ((n < 1) || (m < 1)) { throw new MatrixException(); } a = new int[n][m]; } public int getVerticalSize() { return a.length; } public int getHorizontalSize() { return a[0].length; } public int getElement(int i, int j) throws MatrixException { if (checkRange(i, j)) { // i j return a[i][j]; } throw new MatrixException(); } public void setElement(int i, int j, int value) throws MatrixException { if (checkRange(i, j)) { // i j a[i][j] = value; } throw new MatrixException(); } @Override public String toString() { StringBuilder s = new StringBuilder("\nMatrix : " + a.length + "x" + a[0].length + "\n"); for (int [ ] row : a) { for (int value : row) { s.append(value + " "); } s.append("\n"); } return s.toString(); } // private boolean checkRange(int i, int j) { if ( i < 0 || i > a.length - 1 || j < 0 || j > a[0].length - 1) { return false; } else { return true; } }}

    Matrix private- checkRange(inti,intj)

  • JAVA

    84

    . if , -, , -, , , , , ; -, true false, .

    private boolean checkRange(int i, int j) { if ( i >= 0 && i < a.length && j >= 0 && j < a[0].length ) { return true; } else { return false; }}

    - .

    /* # 24 # - # MatrixCreator.java */

    package by.bsu.task.creator;import by.bsu.task.entity.Matrix;import by.bsu.task.exceptions.MatrixException;public class MatrixCreator { public void fillRandomized(Matrix t, int start, int end) { int v = t.getVerticalSize(); int h = t.getHorizontalSize(); for(int i = 0; i < v; i++) { for(int j = 0; j < h; j++) { try { int value = (int)(Math.random() * (end - start) + start)); t.setElement(i, j, value); } catch (MatrixException e) { /* exception , */ } } } } // public void fillFromFile(Matrix t, File f) { /* */ } // public void fillFromStream(Matrix t, InputStream input) { /* */ } // public void fillFromDataBase(Matrix t, Connection conn) { /* */ }}

    - , .

  • 85

    /* # 25 # # Multiplicator.java */

    package by.bsu.task.action;import by.bsu.task.entity.Matrix;import by.bsu.task.exceptions.MatrixException;public class Multiplicator { public Matrix multiply(Matrix p, Matrix q) throws MatrixException { int v = p.getVerticalSize(); int h = q.getHorizontalSize(); int temp = p.getHorizontalSize();

    // if (temp != q.getVerticalSize()) {

    throw new MatrixException(); // Incompatible matrices }

    // Matrix result = new Matrix(v, h); try {

    // for