Index

A access modifiers, 60 class, 56 (AWT), fields, 56 9, 14, 420–422 JAR, 58 Access modifiers library, 59 compilation error, 62 methods, 56 member-level accessors, 60, 63 modules, 60 package-private modifier, 61 package-info., 57 public class, 60 top-level, 60 anonymous class, 285 C anyMatch(..) method, 309 Checked exceptions, 143–144 , 577 Classes Arrays, 92 abstraction Assignment operator (=), 209–210 Actor class, 123 AudioType, 299 Human class, 121–122 Java compiler error, 122 Musician class, 123 B parent class/superclass, 122 Binary operators, 217 subclass, 122 Binary representation, 168–169 UML diagram, IntelliJ Binary serialization, 503–507 IDEA, 123–124 Bitwise operators, 227 constructors, 117–120 AND, 228, 230 data encapsulation, 111–114 NOT, 227–228 fields, 108–110 OR, 230–231 instantiation, 107–108 XOR, 231–232 methods, 115–117 Boxing, 190 variables, 110–111 Bubble sort algorithm, 256, 267 Comments, 107 Building blocks Compact String, 187

599 © Iuliana Cosmina 2018 I. Cosmina, Java for Absolute Beginners, https://doi.org/10.1007/978-1-4842-3778-6 Index

Concurrent mark sweep (CMS), 565 sorting class hierarchy, 319 Constructors System.out.print class family, 318 Actor class, 120 System.out.print method, 322–324 Human instance, 117–119 logging with JUL Musician class, 120 FileHandler class, 330 polymorphism, 119 IntelliJ IDEA, 334 return statement, 118 java.util.logging.ConsoleHandler, 329 Control flow statements java.util.logging.Level class, 336 flowchart elements, 244 java.util.logging. if-else SimpleFormatter, 329 code, 248 logging libraries, 325 flowchart, Complex if-else, 247 logging output, 331 flowchart, missing else branch, 246 log messages, 335 IntelliJ IDEA launcher, 249 MergeSort class, 326, 328 parameters, 249 SimpleFormatter, 330 using exception, 277–279 SorterJulDemo class, 326 using try catch, 277–278 SortingJulDemo class, 329, 332–333 StreamFormatter class, 329 StringBuilder, 326 D WARNING, 335, 336 Data encapsulation, 111–114 XMLFormatter, 330 Debugging SLF4J and Logback, 337 assertions ch.qos.logback.core. java.lang.AssertionError, 346–347 ConsoleAppender class, 340 rules, 348 ch.qos.logback.core.FileAppender VM options, 346 class, 341 breakpoints, 317 ch.qos.logback.core.rolling. definition, 317 RollingFileAppender, 342–343 IntelliJ IDEA breakpoints, 348–349 ch.qos.logback.core.rolling. Java tools RollingPolicy, 342 jcmd, 353–355 configuration file, XML/Groovy, 339 jconsole, 355–358 info.debug(..) logs, 339 JMC, 359–362 info.error(..) logs, 338 jps, 351–352 info.warn(..) logs, 339 logging logging implementation, 337, 339 main(..) method, 324 log.info(..) logs, 339 merge sort, 319–322 LogManager, 337 sort(..) method, 325 log.trace(..) logs, 339

600 Index

MergeSort class, 340 Gender enum, 125 element, 343 getComment(), 127 SortingSlf4jDemo class, 340–341 Human class, 128, 129 SortingSlf4jDemo.main(..) private modifier, 125 method, 344 Epsilon no-op collector, 565 StringBuilder, 344 equals() method, 224 , 343 checked, 143–144 SortingSlf4jDemo class, 349–351 compiler error, 144 techniques, 317 definition, 139 Deserialization, 503 EmptyPerformerException, 143 distinct() method, 306 finally block, 144 Documentation, Javadoc hierarchy, 140 @author tag, 398 NullPointerException, 143 classes and variables, 397 PerformerGenerator, 142–143 @deprecated tag, 403 RuntimeException, 141 API, 407 StackOverFlowError, 140–141 expression RTFM, 407 swallowing, 141 Gradle javadoc task, 403–406 throwable, 140 HTML tags, 398 try/catch block, 143–144 IntelliJ IDEA, 406–407 unchecked, 143 IntSorter interface, 398 Exchangeable Image (EXIF) @link tag, 400, 402 data, 514–517 method declarations, 400–401 Explicit type conversion, 212, 214 Optional interface, 399–400 @param tags, 401 @return tags, 401 F special tags, 397 File handlers @throws tag, 402 accept(..) method, 476 canRead() and canWrite(), 475 createNewFile(), 476 E createTempFile(prefix, suffix), 477 Eden space, 562 deleteOnExit(), 477 Elvis operator, 241 description, 471 empty() method, 295 exists(), 477 Enums FileFilter, 476 comment() method, 128 FilenameFilter, 476 field values, 126 getAbsolutePath(), 473

601 Index

File handlers (cont.) finalize() methods, 571, 574–575, 577 getName(), 474 genSinger() methods, 572, 573 getParent(), 474 grep command, 576 IOException, 478 IntelliJ IDEA launcher, 575–576 isFile(), 473 deleting object, 587 isHidden(), 475 eden space, 562 lambda expressions, 476 Epsilon no-op collector, 565, 569 length(), 474 exceptions and causes, 595–596 list() method, 475 G1, 565, 569 listFiles(), 475 garbage first (G1), 564 pathnames, 473, 475 generations, 562 printStats(..) method, 471–472 genSinger() method, 581–582 rename(f), 478 go() method, 578 SecurityException, 477 grep method, 580, 582 String value, 471 head memory, 578, 583 URI, 474 heap structure, 561–562 FileInputStream, 489–492 Java heap and stack memory, 572–573 FileOutputStream, 499–502 java.lang.ref.Cleaner object, 584 FilterCharProcessor, 539 old generation space, 563 Finite streams, 289 Oracle Hotspot JVM flatMap(..) method, 304–305 architecture, 560–561 Flattening, 305 parallel collector, 565, 567 Floating-point types, 170 permanent generation space, 564 Flow.Processor, 549 runtime.freeMemory() method, 578 Flow.Publisher, 549 runtime.maxMemory() FlowPublisherVerification method, 578, 583 , 550 runtime.totalMemory() method, 578 Flow.Subscriber, 549 serial collector, 564, 566 forEach(..) method, 285, 301 SingletonDictionary Functional interfaces, 138 instance, 589, 590 Singleton pattern, 588–589 G strong reference, 591 Garbage collection (GC) TLAB, 570 Cleaner instance, 584, 585 VM option, 566–568, 570 cleaner.register() method, 586–587 weak references, 591, 593–595 CMS, 564–565, 568 -Xms and-Xmx, 584 code young generation space, 562–563

602 Index

Garbage first (G1), 565 IntelliJ IDEA, HelloWorld project generate(..) method, 290 build menu, 76 Generics, 145–147 build project option, 76 Git, 24, 38 change directory, 79 Gradle, 24, 37–38, 85 commands execution, 80 Gradle multimodule-level structure, 87–88 compile, 77, 80 Gradle project, 86, 88–89 configuration, 72 create new project option, 70–71 H directory structure, 82 HelloWorld.java file, 74, 79 Heavyweight components, 14 Java class, 75, 78–79 Hello World! class Java module, 71 class declaration, 90 Java 11 project, 71 configuration, 93–94 JRE, 78 IntelliJ IDEA editor, 89 language level, 73 java.util.List, 94 menu option, 76 main() method, 90–92, 94 modules, 73–74 package declaration, 90 move class, 82 println() method, 95, 96 object types, 76 package option, 81 I project SDK, 73 Identifiers, 106 project settings, 73 Imperative programming, 537 project view, 72 Installation refactor button, 82 Download JDK button, 26 sandbox, 72 Git, 38 src directory, 75 Gradle, 37–38 terminal button, 79 JDK 8 vs. contents comparison, 28 Interactions, Java components, 531 JDK 10 vs. JRE contents, 28 Interface Publisher, 534 Integer primitives Interfaces byte, 169 vs. abstract classes, 134 int, 169 annotations, 129, 137–139 long, 170 API, 135 short, 169 Artist interface, 133, 135 Integrated development environment compiler errors, 136 (IDE), 23 isCreative method, 137 Integration tests, 371 Java broken hierarchy, 136 IntelliJ IDEA, 23, 41, 62, 88–89, 249 Performer class, 135–136

603 Index

Interfaces (cont.) portable, 8–9 default methods, 135 real applications in, 2 definition, 129 diamond class hierarchy, 130 automatic memory management, 7 marker, 129 Duke, 5 Musician and Actor classes, 129–130 Green Team, 5 normal, 129 Java logo, 7 Performer class, 129–133 logo, 6 Interface Subscriber, 534 multithreaded execution, 7 Intermediate operations, 282, 298 portability, 7 Internationalization security, 7 contents of resource files, 444 version 9, 21 description, 442 Java Archives (JARs), 58 JavaFX, 446–449 Java Architecture for XML Binding locale, 442 (JAXB), 507–510 property names, 445 Java building blocks, 59, 66 Resource Bundle IntelliJ IDEA Java code, 100 editor, 444–445 Java coding conventions, 106 resource files, 443, 449 Java editors, 62 Stage.close(), 449–450 JavaFX International Software Testing applications, 526–529 Qualifications Board (ISTQB), 370 BorderPane, 438 Interning, 184 CellFactory, 439–440 IntPublisher class, 556 colored ComboBox demo, 441 IntStream interface, 292 ComboBox, 438–440 components, 432 CSS style elements, 438 J, K graphics, 434 Jar hell, 60, 69 GUI library, 432 Java java.lang.IllegalAccessException, 433 applications, 21 launch(...) method, 434 code, 2–3 ListCell declaration, 441 conventions, 4 modules, 432 Gradle, 3, 20, 22 nodes, 434 Hello World!, 2 Oracle, 432 history, 1–2 Prism, 433 machine code, 9–10 properties, 434 Node.js, 21 Quantum toolkit, 434

604 Index

start(..) methods, 435 output files, 519 and AWT, 433 width and height of images, 526 TextArea, 438–439 Java Message Service (JMS), 531 Window Demo, 435–437 Java Mission Control (JMC) JAVA_HOME environment variable description, 359 on , 36–37 flight recording menu and dialog on macOS, 35–36 window, 361–362 on Windows system Java Flight Recorder, 359 dialog window, 31 Memory tab, 360–361 menu item, 30 Oracle article, 362 Path variable, 33 SortingSlf4jDemo main class, 359 system variable, 32, 34 start JMX console, 359–360 Java IDE Java Native Interface (JNI), 29 GitHub user, 42–43 Java 2 Platform, Enterprise Edition IntelliJ IDEA (J2EE), 11 configure Git plugin, 41 Java 2 Platform, Micro Edition (J2ME), 11 configure Gradle plugin, 41 Java 2 Platform, Standard Edition (J2SE), 11 configure plugins dialog section, 40 Java Process API Gradle project view, 46 BufferedReader, 363 IDE Feature Trainer plugin, 42 children() method, 368 java-for-absolute-beginners creating, 362–363 project, 43–45 InputStream, 363 JetBrains, 39 JAVA_HOME environment variable, 366 Java keywords, 147–150 Linux shell commands, 367 Java Media API onExit(), 367 BaseMultiResolutionImage class, parent() method, 368 519–523 ProcessBuilder, 367–368 BufferedImage, 517 ProcessDemo class, 366 checkSize(..), 524 ProcessHandle, 364 EXIF data, 514–517 ProcessHandle.Info, 365 getResolutionVariant(), 521, 522, Java Runtime Environment (JRE), 10, 28 524–525 JavaScript Object Notation (JSON), 511–513 image classes hierarchy, 513–514 Java Server Pages (JSP), 455–456, 461, 463 image file, 514 Java Shell tool (JShell) ImageIO class, 519 code completion, 53 image storage formats, 513 defined, 49 java.awt.Graphics2D, 517–519 help, 50 java.awt.Image class, 513–514 java.lang.String, 52

605 Index

Java Shell tool (JShell) (cont.) FilterCharProcessor, 539, 542 Java statements, 55 filterCharProcessor.subscribe(..), 544 JDK, 50 Flow.Publisher + operator, 51 interface, 537 Oracle, 55 imperative programming, 537 REPL, 49 implementations, 537 scratch variable, 51 infinite IntStream, 548 String method, 52 mapping function, 544–545 string variable, 54 processor/subscriber, flow, 541–542 variables, 51 publisher subscribe(..), 539 vars command, 54, 55 start() method, 539 verbose mode, 50 SubmissionPublisher , 537–539 comments, 107 subscribe(..) calls, 547 exceptions, 139–141, 143–145 subscribe() method, 539 generics, 145–147 subscription.request(..), 539 grammar transformerProcessor, 546 block delimiters, 105 JetBrains, 39 case sensitive, 103 jlink, 67 Java keywords, 104 jps, 351–352 line terminators, 105 JShell, 409 variables, 104 JSP Standard Tag identifiers, 106 Library (JSTL), 465–466 import section, 101–103 JUnit Java code, 100 @AfterAll, 374 lambda expressions, 99 @AfterEach, 375 languages, 99 annotations, 374 object types (see Object types) @BeforeAll, 374 package declaration, 101 @BeforeEach, 374 variables, 106 @DisplayName, 375 java.util.Optional instances, 295 FakeDBConnection java.util.stream.Stream.Builder, 289 Account instance, 378 (JVM), 29, 49 AccountRepoImpl, 378–380 jcmd, 353–355 DbConnection implementation, jconsole, 355–358 380, 381 JDK reactive streams API deleteByHolder method, 382–384 AbstractProcessor, 541, 543, 546 DerbyDBConnection, 378 calling cancel(), 539 Map, 382

606 Index

mocks implementation, 268, 270–271 classes, 393 vs. while flowcharts, 269 createAccount(..) method, 393, 394 for findOne(..) method, 395 Arrays utility class, 263 Gradle test reports, 395–396 code, 257, 260–261 @InjectMocks and @Mock, condition, 259–260 394–395 enhanced syntax, 262 libraries, 395 flowchart, 258 objects and variables, 395 square brackets, 258 PowerMock, 393 Loops, breaking pseudo test class, 375–376 break statement, 271–273 execution, 377 continue statement, 271, 273–274 junit-platform.properties, 376 return statement, 271, 275–276 menu option, IntelliJ IDEA, 377 testOne() method, 378 stubs M AccountServiceImpl, 384 Maven repository, 85 assertThrows, 389–392 Member-level accessors, 63 createAccount(...) Methods, 115–117 method, 385–386 Module descriptor, 64 option field, 388 Module hell, 60 repo stub, 387–388 Modules returned values and compile, manually, 84 exceptions, 386 defined, 64 test coverage, 387 directives, 67 testNonNumericAmountVersion IDE generate, 83 One() method, 389, 390 Java 9 project, 65, 66 write test, 388–389 Java 10, 67 @Test, 375 java--list-modules, 66 JDK, 64 keywords, 67–68 L limit access, 69 Lambda expression, 95 module-info.class descriptor, 84 Last In, First Out (LIFO), 154 module-info.java, 64, 68 Lazy loading, 178 public types, 68 Looping statements, 257 requires keyword, 68 do-while SimpleReader class, 65 code block execution, 268 Modulus operator, 222

607 Index N parallelStream() method, 286 Path handlers NetBeans, 23 compareTo(..) method, 479 NG reactive publisher, 551 createFile(...), 482 Non-blocking back-pressure, 534 delete(..), 482 NullPointerException, 283 getFileName(), 480 Numerical operators, 214 getFileSystem(), 480 getRoot(), 480 O IOException, 482 Object types java.nio.file.Path, 478 classes (see Classes) Paths.get(fileURI), 478–479 enums, 125–128 properties, 481 interfaces (see Interfaces) resolve(..), 480 Operators sample code, 481 assignment, 209–210 toAbsolutePath(), 480 category, 207–208 peek(..) method, 308, 310 explicit conversion, 211–213 Performer hierarchy, 211 numerical Point-to-point (p2p) messaging binary, 217, 219–223 model, 531 Elvis operator, 241 Polymorphism, 119 logical, 233–237 Primitive data types negation, 216 binary representation, 168–169 relational, 223, 225–226 boolean type, 165–166 shift, 238, 240–241 byte, 169 sign, 215–216 char type, 166–167 unary, 214–215 = (equals) operator, 159 Oracle, 55 float and double, 170–171 JavaFX 2.0, 16 int, 169 Java SE 7, 15–16 integer primitives, 167 Java SE 8, 16–17 long, 170 Java SE 9, 17–18 numeric types, 167–168 Java SE 10, 19 numeric values, 171, 173 Java SE 11, 20 real primitives, 170–171, 173 short, 169 stack, 160–161 P, Q swap() method, 160 Package, 56, 81 Project Jigsaw, 64 Parallel collector, 565 Project reactor

608 Index

advantage, 552 hierarchy, 488 empty() method, 556 java.io.Closeable interface, 486 Flux and Mono, 556 lambda expressions, 487 flux publisher implementation, 553 nullReader(), 489 interval(..) method, 557 StringBuilder, 488 JDK-based implementation, 557 Scanner class, 482 operators, 552 utility methods, 484 org.reactivestreams. Reading user data Subscriber, 553, 554 Scanner reactive publisher, 555 advantage, 412 reactor.core.CoreSubscriber, 554 console.format(..), 419 subscriber, write, 553–554 console methods, 418 Public class, 61 java.io.Console, 417 long values, 416 next..() methods list, 411–412 R ReadingUsingConsoleDemo, 420 Reactive Manifesto, 532 read value, 412–415 Reactive producer/consumer system, 533 sample code, 418–419 Reactive programming System.in, 411 flow interfaces, 535 templates, 419 producer/consumer system, 533 usage, 415–416 reactive streams API, 534–535 System.in, 410–411 standard API, 534 Real primitives streams API implementations, 535–536 boxing and unboxing, 173 Reactive streams API double, 171 implementations, 535–536 float, 170 Reactive streams interfaces, 534–535 numeric values, 171, 173 Reactive Streams Technology Reference data types Compatibility Kit, 548, 550–551 arrays Read-Eval-Print Loop (REPL), 49 initialization, 178–179 Reading files, 482 int type, 179 Files.readAllBytes(..), 484 lazy loading, 178 InputStream, 489–492 null keyword, 177–178 Reader class square brackets, 179 BufferedReader, 485–486, 488 class and interface hierarchy, 174 Files.newBufferedReader, 489 class constructor, 173 Files.newBufferedReader(Path) collections, 196 method, 487 date time API, 191

609 Index

Reference data types (cont.) from arrays, 287, 289 escaping characters, 187 collection interfaces and heap, 161–163 classes, 284 java.lang.Thread class, 201–202 empty streams, 289 lambda expressions, 205 finite streams, 289 run() method, 202 IntStream instance, 292–293 CounterRunnable code, 205 LongStream instances, 293 runnable code, 204–205 stream of primitives, 292 stack and heap memory, 174–176 stream of strings, 294 start() method, 203 debugging, code, 310 string, 183 anyMatch(..), 309–310 swap() method, 163–165 findAny(), 309–310 Thread.currentThread() method, 205 peek(..), 310 thread management, 206 dropWhile, 292 wrapper classes, 189 functions, 282 Regression tests, 371 interfaces, 293 Run-time polymorphism, 119 intermediate operation add(..), 289 allMatch(..), 309–310 S anyMatch(..) method, 309 Serial collector, 564 collect(..) method, 303 Serialization, 502 count(), 306 binary, 503–507 distinct() method, 306 JSON, 511–513 filter(..) method, 302 XML, 507–511 findAny(), 309–310 sorted() operation, 313 findFirst(), 306 Stack and heap memory flatMap(..) method, 304–305 add() method, 157 limit(..) method, 307 definition, 153–154 map(..) method, 303 java.lang.String class, 154 noneMatch, 310 JVM parameters, 154 Optimal instance, 309 main() method, 158 parallelStream(), 286–287 object declaration, 156 peek(..), 310 String Pool, 155 sorted() method, 306 variable declaration, 155–156 toArray() method, 302 Stream API iterate, 285 Consumer, 285–286 java.util.function.Supplier, 290 creation java.util.stream.BaseStream, 282

610 Index

limit, 289–290, 307 getValueIsAdjusting(), 429 NullPointerException, 283 java.awt.BorderLayout, 425–426 Optimal, 309 java.awt.event.ActionListener, 427 Optional, 283, 295–298, 303–306, 309 javax.swing.JFrame, 423 parallel data processing, 282 JComponent, 425 range, 293 JFrame, 424–425 rangeClosed, 293 JFrame.EXIT_ON_CLOSE, 424 Stream, 281–294, 298–300 JList class, 428 Stream.builder(), 289 JScrollPane, 429 Stream.generate(), 289 JTextArea, 428 takeWhile, 291–292 ListSelectionListener terminal functions implementation, 430, 431 forEach and forEachOrdered, Operation System, 429 300, 302 UIManager class, 429 sum and reduce, 307–308 Windows, 423, 431 terminal operations, 283 Switch Statement min() and max(), 307 code, 250–251 transform collections into streams, 283 flowchart, 252, 255–256 String.format(..) method, 231–232 NullPointerExceptions, 254–255 String Pool, 154, 183–184 splitAsStream, 294 Sun Microsystem’s Java Versions T features, 11–12 Terminal operations, 283, 298 J2EE, 11 Test-driven development (TDD), 371 J2ME, 11 Testing J2SE, 11 application J2SE 5.0, 12–13 account management, 373 Java FX 1.0 SDK, 14 AccountRepo, 373 Java official logo, 12 AccountService, 373 JRE, 10 JUnit (see JUnit) Mac OS X, 13 Oracle RDBMS, 373 Oracle features, 14 development phase, 371 Swing Gradle module structure, 371–372 AWT model, 420–422 integration tests, 371 border layout zones, 426 ISTQB, 370 components, 14 lifecycle of software application, 370 FlowLayout, 427 regression tests, 371 getInstalledLookAndFeels(), 429 src directory, 372

611 Index

Testing (cont.) SampleServlet, 461–462 TDD, 371 servers, 450 unit tests, 371 servlets and JSP, 455–456 Thread local allocation buffer structure, 460–461 (TLAB), 570 URL, context path value, 454–455 toBinaryString method, 227 urlPattern property, 457 @WebServlet, 458–459 Writing files U, V Files.write(..), 493–494 Unboxing, 190 Files.write(Path, byte[]), 492 Unchecked exceptions, 143 Files.writeString(..), 493 Uniform Resource Identifier (URI), 474 OutputStream, 499–502 Unit tests, 371 Writer class BufferedWriter, 495, 498 W Files.newBufferedWriter(..), 497 flush() method, 495 Web application hierarchy, 498 Apache Tomcat server, 451 nullWriter(), 499 DateServlet, 467–468 OutputStreamWriter, 499 embedded Tomcat server, 453–454 PrintWriter, 499 HttpServletRequest, 457–458 String instances, 495, 496 index.jsp page, 463–466 StringWriter, 499 Internet, 451 JavaScript, 468 javax.servlet.http.HttpServlet, 456–457 X, Y JSP scriptlets, directive tags, 463 XML serialization, 507–511 JSTL, 465–466 network debugger view, Firefox, 452 request method, 452–453 Z resource/dynamic directory, 461 zipWith(..) method, 557

612