38,99 €
A concise handbook for the most common tools used in modern Java development
Good Java developers learn Java syntax, how to create loops and switches, and can work out a lambda filter stream. But great Java developers need to understand the vast ecosystem of tools and frameworks for collaboration, testing, building, and more.
In Real-World Java®: Helping You Navigate the Java Ecosystem, a team of veteran Java developers delivers a concise and authoritative discussion of the most common frameworks, tools, and techniques used in enterprise Java development shops. The authors provide just enough background and examples to allow you to dive right into the expert guidance on annotations, logging frameworks, observability, performance tuning, testing tools, and collaboration, that real-life commercial Java development typically relies on.
You’ll expand your Java development toolkit with frameworks and utilities like Spring, Git, Prometheus, and Project Lombok. You’ll also discover links to tested, downloadable code examples that demonstrate the skills discussed in the book.
Real-World Java® is the perfect resource for everyone already somewhat comfortable with the language but who wants to familiarize themselves with the tools and frameworks used in contemporary Java software development.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 705
Veröffentlichungsjahr: 2025
COVER
TABLE OF CONTENTS
TITLE PAGE
INTRODUCTION
WHAT DOES THIS BOOK COVER?
WHO SHOULD READ THIS BOOK
READER SUPPORT FOR THIS BOOK
1 How We Got Here: History of Java in a Nutshell
INTRODUCTION
UNDERSTANDING THE STEWARDSHIP OF JAVA
DIFFERENTIATING KEY JAVA VERSIONS
WORKING WITH DEPRECATION AND RETIREMENT
IDENTIFYING RENAMES
UNDERSTANDING THE PRINCIPLES OF CHANGE
FURTHER REFERENCES
SUMMARY
2 Getting to Know your IDE: The Secret to Success
UNDERSTANDING IDE HISTORY
STARTING A NEW PROJECT
CREATING A RUN CONFIGURATION
GETTING TO KNOW YOUR KEYBOARD SHORTCUTS
DEBUGGING YOUR CODE
REFACTORING YOUR CODE
EXPLOITING THE EDITOR
EXTENDING THE IDE
PEEKING AT ECLIPSE
PEEKING AT VS CODE
COMPARING IDEs
FURTHER REFERENCES
SUMMARY
3 Collaborating Across the Enterprise with Git, Jira, and Confluence
COLLABORATING WITH GIT
USING JIRA FOR ENTERPRISE PROCESS COLLABORATION
WORKING WITH CONFLUENCE, THE ENTERPRISE KNOWLEDGE MANAGEMENT SYSTEM
FURTHER REFERENCES
SUMMARY
4 Automating Your CI/CD Builds with Maven, Gradle, and Jenkins
BUILDING WITH MAVEN
BUILDING WITH GRADLE
FINDING A DEPENDENCY
INTEGRATING WITH JENKINS
SCANNING WITH SONAR
EXPLAINING CI/CD PRACTICES
FURTHER REFERENCES
SUMMARY
5 Capturing Application State with Logging Frameworks
NEEDING TO MOVE BEYOND PRINT
USING JAVA UTIL LOGGING
USING LOG4J
USING SLF4J
USING LOGBACK
LEARNING MORE ABOUT LOGGING
COMPARING LOGGING FRAMEWORKS
FURTHER REFERENCES
SUMMARY
6 Getting to Know the Spring Framework
CONFIGURING SPRING
CUSTOMIZING SPRING APPLICATIONS WITH PROPERTIES
TURBOCHARGING DEVELOPMENT WITH SPRING BOOT
WORKING WITH SPRING MVC
HANDLING ERRORS IN SPRING
INSPECTING YOUR APPLICATION WITH ACTUATOR
SECURING YOUR APPLICATION WITH SPRING SECURITY
EXPLORING THE SPRING PROJECTS
FURTHER REFERENCES
SUMMARY
7 Testing Your Code with Automated Testing Tools
UNDERSTANDING TESTING BASICS
LEARNING JUNIT
WORKING WITH COMMON TESTING LIBRARIES
MOCKING OBJECTS
MEASURING TEST COVERAGE
OPTIMIZING YOUR TESTING WITH INTELLIJ
LEARNING OTHER TESTING CONCEPTS
FURTHER REFERENCES
SUMMARY
8 Annotation Driven Code with Project Lombok
PREPARING YOUR ENVIRONMENT FOR LOMBOK
IMPLEMENTING LOMBOK
CONVERTING TO THE LOMBOK WAY
DELOMBOKING YOUR CODEBASE
FURTHER REFERENCES
SUMMARY
9 Parallelizing Your Application Using Java Concurrency
UNDERSTANDING CONCURRENCY BASICS
INTRODUCING CONCURRENT COLLECTIONS
USING BUILT-IN CONCURRENCY SUPPORT IN JAVA STREAMS
USING CONCURRENCY COMPONENTS TO REDUCE COMPLEXITY
LEARNING CONCURRENCY SUPPORT IN THE SPRING FRAMEWORK
FURTHER REFERENCES
SUMMARY
10 Pattern Matching with Regular Expressions
INTRODUCING REGULAR EXPRESSIONS
LEARNING BASIC REGULAR EXPRESSION SYNTAX
USING REGULAR EXPRESSIONS WITH JAVA APIS
EXPLORING ADVANCED REGULAR EXPRESSION SYNTAX
IDENTIFYING COMMON MISTAKES
CODING WITH REGULAR EXPRESSIONS
USING WITH FRAMEWORKS AND TOOLS
FURTHER REFERENCES
SUMMARY
11 Coding the Aspect-Oriented Way
UNDERSTANDING THE NEED FOR ASPECTS
CREATING OUR FIRST EXAMPLE
EXPLORING THE POINTCUT EXPRESSION LANGUAGE
FURTHER REFERENCES
SUMMARY
12 Monitoring Your Applications: Observability in the Java Ecosystem
INTRODUCING OBSERVABILITY
GETTING STARTED WITH PROMETHEUS
ADDING ALERT MANAGER
DASHBOARDING WITH GRAFANA
LOGGING AND TRACING
FURTHER REFERENCES
SUMMARY
13 Performance Tuning Your Services
LEARNING THE CONCEPTS AND TERMINOLOGY
BENCHMARKING
TUNING JVM SETTINGS
TESTING WITH JMETER
USING JDK TOOLS
OPTIMIZING YOUR APPLICATION
FURTHER REFERENCES
SUMMARY
14 Getting to Know More of the Ecosystem
WRITING JAVADOC
COMPARING JVM LANGUAGES
EXPLORING JAKARTA EE
COMPARING DATABASE TYPES
LEARNING ABOUT INTEGRATIONS
DEPLOYING JAVA
BUILDING REST APIs
PICKING A VIRTUAL MACHINE
EXPLORING LIBRARIES
SECURING YOUR APPLICATIONS
STAYING INFORMED ABOUT CHANGES
FURTHER REFERENCES
SUMMARY
APPENDIX: Reading and Writing XML, JSON, and YAML
WORKING WITH XML
WORKING WITH JSON
WORKING WITH YAML
FURTHER REFERENCES
SUMMARY
INDEX
COPYRIGHT
DEDICATION
ABOUT THE AUTHORS
ABOUT THE TECHNICAL EDITOR
ABOUT THE TECHNICAL PROOFREADER
ACKNOWLEDGMENTS
END USER LICENSE AGREEMENT
Chapter 1
TABLE 1.1: Java Releases
Chapter 2
TABLE 2.1: IDE Terminology
TABLE 2.2: Summary of IDE Keyboard Shortcuts
Chapter 3
TABLE 3.1: Revert and Reset
TABLE 3.2: Comparison Operators
TABLE 3.3: Logical Operators
TABLE 3.4: Special Operators
TABLE 3.5: Selected Confluence Macros
Chapter 4
TABLE 4.1: Maven Default Life-Cycle Phases
TABLE 4.2: Version Number Example
Chapter 5
TABLE 5.1: Java Util Logging Levels
TABLE 5.2: Method Calls for Logging
TABLE 5.3: Specifying Log4j as a Dependency
TABLE 5.4: Log4j Logging Levels
TABLE 5.5: Specifying Log4j as the Jackson Dependency Needed for JSON
TABLE 5.6: Specifying Log4j as the Jackson Dependency Needed for YAML
TABLE 5.7: Specifying SLF4J as a Dependency
TABLE 5.8: Specifying SLF4J as a Dependency
TABLE 5.9: SLF4J Logging Levels
TABLE 5.10: Specifying Java Util Logging as SLF4J Provider as a Dependency
TABLE 5.11: Specifying Log4j as SLF4J Provider as a Dependency
TABLE 5.12: Specifying Logback as a Dependency
TABLE 5.13: Logback Logging Levels
TABLE 5.14: Comparing Logging Frameworks
Chapter 6
TABLE 6.1: Example Spring Projects
Chapter 7
TABLE 7.1: Annotations for Test Flow
TABLE 7.2: Specifying AssertJ as a Dependency
TABLE 7.3: Specifying JUnit Pioneer as a Dependency
TABLE 7.4: Specifying Mockito as a Dependency
TABLE 7.5: Specifying EasyMock as a Dependency
TABLE 7.6: Specifying Spring Boot Starter Test as a Dependency
TABLE 7.7: JaCoCo IDE Integration
TABLE 7.8: Navigate to the Test
Chapter 8
TABLE 8.1: Specifying Lombok as a Dependency
TABLE 8.2: Logging Annotations Supported by Project Lombok
Chapter 9
TABLE 9.1: Concurrency Interfaces
Chapter 10
TABLE 10.1: Common Quantifiers
TABLE 10.2: Boundaries
TABLE 10.3: Predefined Character Classes
TABLE 10.4: Flags
TABLE 10.5: Positive/Negative Look-Ahead/Behind Patterns
TABLE 10.6: All Quantifiers
TABLE 10.7: Examples of Regular Expression Mistakes
Chapter 13
TABLE 13.1: JMH Benchmark Modes
TABLE 13.2: JMH Output Time Units
TABLE 13.3: Three Common Memory Options
TABLE 13.4: Examples of Sonar Performance Rules
Chapter 14
TABLE 14.1: Example Jakarta EE Projects
TABLE 14.2: SQL Keywords
TABLE 14.3: Sample Cloud Technologies
TABLE 14.4: Example Java Libraries
Appendix
TABLE A.1: Specifying Jackson as a Dependency
TABLE A.2: Specifying Jackson as a Dependency
TABLE A.3: Specifying Gson as a Dependency
TABLE A.4: Specifying Jackson YAML as a Dependency
TABLE A.5: Specifying SnakeYAML as a Dependency
Chapter 1
FIGURE 1.1: Java releases
FIGURE 1.2: LTS release schedule, with future predictions
Chapter 2
FIGURE 2.1: Creating a new project
FIGURE 2.2: Creating a new application from scratch
FIGURE 2.3: Defining Git Remote
FIGURE 2.4: Running and debugging
FIGURE 2.5: Editing the configuration
FIGURE 2.6: Adding an application configuration
FIGURE 2.7: Adding a
main()
method
FIGURE 2.8: Using menus to discover keyboard shortcuts
FIGURE 2.9: Navigating to a class
FIGURE 2.10: Navigating to a symbol
FIGURE 2.11: Outline view
FIGURE 2.12: Paste Special context menu
FIGURE 2.13: Debug window overview
FIGURE 2.14: Debugger Variables
FIGURE 2.15: Tuning debugging settings
FIGURE 2.16: More debugging optimization settings
FIGURE 2.17: Command-line arguments
FIGURE 2.18: IntelliJ IDE's refactor menu
FIGURE 2.19: Extracting common functionality to a method
FIGURE 2.20: Extracting differences into variables
FIGURE 2.21: Selecting the common functionality
FIGURE 2.22: Extracting the method
FIGURE 2.23: The IDE helps locate opportunities to apply the new method.
FIGURE 2.24: The first refactored draft
FIGURE 2.25: The final refactored version. Compare this to Figure 2.19.
FIGURE 2.26: The variable declaration in line 8 adds no clarity; inline it....
FIGURE 2.27: The variable is inlined.
FIGURE 2.28: Reformatting options
FIGURE 2.29: Selecting column mode
FIGURE 2.30: All cursors at the end of the line
FIGURE 2.31: All cursors moving left
FIGURE 2.32: All lines deleting the middle segment
FIGURE 2.33: Replacement text on all lines
FIGURE 2.34: Installing a plugin in IntelliJ
FIGURE 2.35: Creating a project in Eclipse
FIGURE 2.36: Running an application in Eclipse
FIGURE 2.37: Creating a project in VS Code
FIGURE 2.38: Running an application in VS Code
FIGURE 2.39: Creating a launch configuration in VS Code
Chapter 3
FIGURE 3.1: Forking a GitHub project
FIGURE 3.2: Completing the fork
FIGURE 3.3: Adding SSH keys to your GitHub repo
FIGURE 3.4: Adding new SSH keys
FIGURE 3.5: Git merge without and with fast-forward
FIGURE 3.6: Merge versus rebase
FIGURE 3.7: Tracking a new file
FIGURE 3.8: Color coding for a newly tracked file
FIGURE 3.9: Tracking an external file
FIGURE 3.10: Commit context window
FIGURE 3.11: Diff-viewer window
FIGURE 3.12: IntelliJ Git menu
FIGURE 3.13: IntelliJ Uncommitted Changes submenu
FIGURE 3.14: IntelliJ Current File submenu
FIGURE 3.15: Git context menu
FIGURE 3.16: IntelliJ Git log window
FIGURE 3.17: Gitflow workflow
FIGURE 3.18: Jira issue hierarchy
FIGURE 3.19: Creating a new project
FIGURE 3.20: Selecting a project template
FIGURE 3.21: Creating a new issue
FIGURE 3.22: Filling out the new issue
FIGURE 3.23: Assigning an epic to an issue
FIGURE 3.24: Design of a workflow
FIGURE 3.25: Backlog tab
FIGURE 3.26: Creating a sprint
FIGURE 3.27: Assigning jiras to a sprint
FIGURE 3.28: Adding users
FIGURE 3.29: Adding new columns
FIGURE 3.30: Column maintenance
FIGURE 3.31: Viewing My Issues
FIGURE 3.32: Bulk editing
FIGURE 3.33: Bulk editing, Choose bulk action
FIGURE 3.34: Bulk editing, Operation details
FIGURE 3.35: Inserting an image
FIGURE 3.36: Inserting a table
Chapter 4
FIGURE 4.1: Directory structure in Maven
FIGURE 4.2: Maven compiler documentation home page
FIGURE 4.3: Running Maven from IntelliJ
FIGURE 4.4: Viewing the effective POM in IntelliJ
FIGURE 4.5: IntelliJ dependency graph
FIGURE 4.6: Running Maven from Eclipse
FIGURE 4.7: Viewing the effective POM in Eclipse
FIGURE 4.8: Running Maven from VS Code
FIGURE 4.9: Running Maven from VS Code
FIGURE 4.10: Directory structure in Maven
FIGURE 4.11: Running Gradle from IntelliJ
FIGURE 4.12: Running Gradle from Eclipse
FIGURE 4.13: Running Gradle from VS Code
FIGURE 4.14: Finding dependencies
FIGURE 4.15: Jenkins freestyle sections
FIGURE 4.16: Freestyle timer job configuration
FIGURE 4.17: Jenkins build history
FIGURE 4.18: Maven tool configuration
FIGURE 4.19: Maven freestyle job configuration
FIGURE 4.20: Gradle freestyle job configuration
FIGURE 4.21: Maven pipeline script
FIGURE 4.22: Maven pipeline from SCM
Chapter 5
FIGURE 5.1: Logging hierarchy
FIGURE 5.2: Maven structure for SLF4J
FIGURE 5.3: Maven structure for Log4J
Chapter 6
FIGURE 6.1: Mortgage formula
FIGURE 6.2: Setting the profile in IntelliJ with an option
FIGURE 6.3: Setting the profile in IntelliJ with a launch configuration
FIGURE 6.4: Configuring your project in Spring Initializr
FIGURE 6.5: Configuring your Spring Boot project in IntelliJ
FIGURE 6.6: Selecting dependencies in IntelliJ
FIGURE 6.7: Specifying a server port
FIGURE 6.8: Output from Spring Boot launch
FIGURE 6.9:
curl
command to get response and headers
FIGURE 6.10: Response from ControllerAdvice
FIGURE 6.11: Spring Security filter interactions
FIGURE 6.12: Using Initialzr to locate new dependencies
FIGURE 6.13: Adding new dependencies using IntelliJ
Chapter 7
FIGURE 7.1: Test organization in Maven
FIGURE 7.2: JUnit output in Maven
FIGURE 7.3: JUnit output in Gradle
FIGURE 7.4: Parameterized test output
FIGURE 7.5: Running code coverage in IntelliJ
FIGURE 7.6: Running code coverage in Eclipse
FIGURE 7.7: Code coverage IntelliJ report
FIGURE 7.8: Code coverage Eclipse report
FIGURE 7.9: Comparing expected and actual in IntelliJ
FIGURE 7.10: JUnit IntelliJ options
Chapter 8
FIGURE 8.1: Select the Enable Annotation Processing checkbox.
FIGURE 8.2: Installing Lombok in Eclipse
Chapter 9
FIGURE 9.1: Hit counter happy path
FIGURE 9.2: Hit counter with race condition
FIGURE 9.3: Each thread sets one-half of the variable producing an unexpecte...
FIGURE 9.4: Thread states
FIGURE 9.5:
Thread.sleep()
before Java 21
FIGURE 9.6:
Thread.sleep()
starting Java 21
Chapter 10
FIGURE 10.1: Replacing using a regular expression
FIGURE 10.2: Regex101
FIGURE 10.3: Identifying boundaries
FIGURE 10.4: Debugging a regular expression
Chapter 11
FIGURE 11.1: Sequence diagram
FIGURE 11.2: Pointcut expression
Chapter 12
FIGURE 12.1: Prometheus configuration tab
FIGURE 12.2: Prometheus targets' tab
FIGURE 12.3: Querying for a single metric
FIGURE 12.4: Specifying a label
FIGURE 12.5: Displaying a graph of CPU idle time
FIGURE 12.6: Graphing CPU utilization per core
FIGURE 12.7: CPU utilization, average over all cores
FIGURE 12.8: CPU range vector
FIGURE 12.9: Graph of a gauge
FIGURE 12.10: Graph of a histogram
FIGURE 12.11: Prometheus alerts tab
FIGURE 12.12: Integrating with Prometheus
FIGURE 12.13: Configuring a Grafana dashboard
FIGURE 12.14: Adding a row title
FIGURE 12.15: Adding a graph panel
FIGURE 12.16: Adding a second graph panel
FIGURE 12.17: Kibana landing page
Chapter 13
FIGURE 13.1: Sample application
FIGURE 13.2: JMeter thread group
FIGURE 13.3: JMeter HTTP GET request
FIGURE 13.4: JMeter HTTP POST request
FIGURE 13.5: Email summary page
FIGURE 13.6: JFR views
FIGURE 13.7: Java Mission Control
FIGURE 13.8: JConsole
FIGURE 13.9: VisualVM
Chapter 14
FIGURE 14.1: Relational database
FIGURE 14.2: Swagger REST APIs
FIGURE 14.3: Executing an API
FIGURE 14.4: Log4J CVE
Cover
Title Page
Introduction
Table of Contents
Begin Reading
Appendix: Reading and Writing XML, JSON, and YAML
Index
Copyright
Dedication
About the Authors
About the Technical Editor
About the Technical Proofreader
Acknowledgments
End User License Agreement
v
xxix
xxx
1
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
311
312
313
314
315
316
317
318
319
320
321
322
323
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
vi
vii
ix
xi
xiii
xv
431
Victor Grazi
Jeanne Boyarsky
CONGRATULATIONS ON MASTERING THE BASICS OF JAVA! You've journeyed through loops, switches, and exception handling, you've made sense of lambdas and streams, and you're starting to get comfortable with the core Java APIs. You've built a solid foundation, and that's no small accomplishment—You are ready to start your elite career as a Java engineer!
But Java is more than a language; it's a vast ecosystem filled with tools, platforms, libraries, and APIs that empower you to extend Java's capabilities into every corner of enterprise development.
This book is your guide to navigating that ecosystem. While numerous tomes delve into specialized areas of Java, few provide an accessible roadmap across the broader landscape. Here, you'll find clear explanations and practical coding examples of the technologies you can expect to encounter most often in enterprise Java environments. The knowledge you gain here will prepare you to delve deeper into additional resources and broaden your skills in this dynamic ecosystem.
This book begins with a brief coverage through Java's history, offering insights that will deepen your understanding of the language's evolution and its current direction. From there, we dive into essential tools: you'll get acquainted with IDEs, version control, and build tools, that form the backbone of any Java developer's toolkit.
Next, we explore a range of foundational libraries and frameworks—logging, Spring, testing tools, and Project Lombok—that enhance your productivity and code quality. The book then navigates more advanced topics like concurrency and regular expressions, essential for writing efficient, powerful code, and continues with cross-cutting concerns such as aspect-oriented programming and performance optimization.
Finally, we wrap up with a chapter dedicated to additional tools and technologies that every Java developer should have in their arsenal.
Each chapter includes practical code examples and tips to help you work effectively with these technologies. Plus, a “Further References” section at the end of each chapter provides resources to dive deeper into each topic.
This book is intended for anyone who has learned the Java language but is still gaining familiarity with the broader Java ecosystem. It's especially suited to students, IT professionals transitioning to Java, and developers looking to deepen their expertise. For those returning to Java after working on legacy projects, this book is also an excellent way to get up to speed on the latest tools and practices.
To help you get the most out of this book, here are some additional resources and support links:
Throughout the book, you'll find references to code examples and configurations that illustrate key concepts. These files are available for download to save you time and allow you to experiment directly with the material. You can access these companion files at: https://github.com/realworldjava.
We appreciate your input, errata reports, and questions about this book! Post in the discussion area of our GitHub organization at https://github.com/orgs/realworldjava/discussions. All acknowledged errors will be posted on the README page of the repository for the relevant chapter.
Understanding IDE History
Starting a New Project
Creating a Run Configuration
Getting to Know Your Keyboard Shortcuts
Debugging Your Code
Refactoring Your Code
Exploiting the Editor
Extending the IDE
Peeking at Eclipse
Peeking at VS Code
Comparing IDEs
Whether you're a carpenter or a programmer, the first critical decision you will make is your selection of tools. Choosing your integrated development environment (IDE) and getting to know it well will be one of the most important things you will do in your programming career.
In the early days of Java, you could read a copy of the Java API, a 200-page book that contained everything you needed to know to do great things. Today you would need a dozen 500-page books to get the same. Or you could save some time and rely on your IDE for professional support and guidance.
In this chapter, we'll look at the “big three” IDEs—IntelliJ, Eclipse, and Visual Studio Code—and cover the following topics:
Getting to know your keyboard shortcuts:
Your IDE is a productivity tool, and you can often identify coding professionals by their use of keyboard shortcuts. There are many, but in this section, we will discuss some of the most important ones and how to learn them, and we'll show you how to discover more.
Professional code refactoring:
If you do as many code reviews as we have, you can easily spot nonprofessionals by how they repeat code snippets throughout their codebase. You will see how your IDE can help you locate and correct “copy-and-paste” code.
Other productivity pearls:
You should not forget that the primary purpose of your IDE is
editing
! We will review some powerful, lesser-known editing features.
This chapter is certainly not attempting to be a comprehensive guide to every feature. You can get that from the user manuals. Rather, we will emphasize techniques for leveraging your IDE features to greatly improve your productivity.
The source code for this chapter is available on the book page at www.wiley.com. Click the Downloads link. The code can also be found at https://github.com/realworldjava/Ch02-IDEs. See the README.md file in that repository for details.
There's an old adage that advises never to discuss religion or politics in mixed company. We should add to that discussing your favorite IDE. But in elite circles, the IDE of choice seems to be IntelliJ IDE. “Why?” you may ask.
To answer this, let's cover a bit of history. When Java first arrived on the scene, there were some popular IDEs called Symantec Visual Café (the first Java IDE), Microsoft Visual J++, Borland JBuilder, and a variety of other niche offerings. These IDEs offered features like code completion and a smattering of interesting tidbits designed to help developers cut through the ever-growing weeds.
Visual J++ would have won the IDE battle, were it not for their bigger battle with Java founder Sun Microsystems, ultimately resulting in their own bytecode language C# and the sudden withdrawal of Visual J++ from the planet. But the IDE wars exploded when the free, industry-backed Eclipse project and Sun Micro's own NetBeans (also free) surfaced in turn, followed shortly by the super-slick IntelliJ Idea, a feature-rich paid offering from a company named JetBrains.
Fast-forward 25 years and you will find IntelliJ and Eclipse are still innovating, and Microsoft Visual Studio now offers Java support and has been growing steadily especially among young developers or those who started with other languages like JavaScript or Python. A number of years ago IntelliJ introduced a free “community edition” (CE) to compete with the other free offerings. CE sports most of the features that people use every day, the most notable exception being its lack of special Spring integration; it still works with Spring, of course, but if you want the rich IntelliJ-style Spring experience, they ask you to pay.
In our humble opinion, IntelliJ has one salient feature: it is very focused on developer experience, so the IDE can do almost anything you might imagine. There are many capabilities, as you can see by looking at the menus. There is a built-in code locator that finds duplicate code across disparate files, even in a large codebase, and refactorings are always intuitive, precise, and reliable! That said, Eclipse is still widely used, and Visual Studio has been growing both in capability and traction.
In this chapter, we will discuss the big three IDEs, starting with an emphasis on IntelliJ and concluding with the important differences between it, Eclipse, and Visual Studio. In the rest of this book, we will mostly be using IntelliJ. Nonetheless, if you are a loyal Eclipse or VS Code user, much of this discussion will translate easily, differing mostly in keyboard shortcuts and menu offerings. IntelliJ provides a keyboard shortcut for all its important functionality, and you can add and modify these as you like. You can also select an Eclipse theme that borrows many of those shortcuts, in case you happen to have extensive Eclipse experience. The other IDEs also provide basic key mappings out of the box and allow you to create your own custom mappings. But if you enjoy the power of keyboard shortcuts, you will find IntelliJ provides the largest selection of these out of the box.
TIP When you launch IntelliJ, a window will come up offering feature tips. Read them!
There are versions of the top three IDEs available for Windows, Mac, and Linux. The more powerful your computer, the faster and more responsive your IDE will be. See the “Further References” section to download a new IDE if you'd like to try a different one than you are used to.
Let's start by looking at IntelliJ. Remember to read it even if you plan to use Eclipse or VS Code as those sections assume you have read the entire chapter. It is by far our favorite IDE, and who knows, you might even decide to try IntelliJ once you see what it can do!
When you want to start a new project, you can choose from several options, as shown in Figure 2.1.
FIGURE 2.1: Creating a new project
To create a project from scratch, choose Project from the menu. A large list of project types is displayed for you to choose from, as shown in Figure 2.2. Make your selection based on your project type. When getting started, you will want to select New Project to create a minimal project with just a main class.
FIGURE 2.2: Creating a new application from scratch
There are many other options including whether you want to use a build tool, which is covered in depth in Chapter 4, “Automating Your CI/CD Builds with Maven, Gradle, and Jenkins.”
NOTE Once you learn about Spring Boot in Chapter 6, “Getting to Know the Spring Framework,” you will often choose the Spring Initializer type instead.
You can also import the source from an existing directory into IntelliJ by selecting Project From Existing Sources and pointing to the directory containing the existing project. IntelliJ does a great job locating your build tools, tests, and so on, and staging it all for you to execute.
TIP Creating a project from existing sources is particularly useful if you are switching over from another IDE like Eclipse or if you are using a build tool and already have the directories on your machine.
When you are working on a project with a team, you will most often be importing an existing project from version control. To do so, you will clone a remote repository. (For more on cloning a repo, see Chapter 3, “Collaborating Across the Enterprise with Git, Jira, and Confluence.”)
In most cases you will be cloning an existing project, but every project has a start, and if that is the case, you will need to add your new project to version control after creating it. To do so, perform the following steps. (We are using GitHub, but the process is similar for other repository types. For more on Git, see Chapter 3.)
In your browser, create a repository in GitHub, and grab the URL.
In IntelliJ, select menu item VCS ➪ Enable Version Control Integration from the drop-down menu.
Select a version control system (in our case Git).
Add some files to your project and perhaps a
README.md
file.
Select the menu item Git ➪ Commit.
Now do a push to the remote (select the menu Git ➪ Push, or press Ctrl+Shift+K/Cmd+Shift+K).
In the Push Commits window, click the Define Remote button.
You can now navigate to the URL you provided where you will find your code, as shown in Figure 2.3.
FIGURE 2.3: Defining Git Remote
It is common to have multiple services (which are essentially distinct applications) that serve a single application. In such cases you might want to have those services reside in one master project. To accomplish that, you can choose menu item File ➪ New Module or File ➪ New Module From Existing Sources, which provides options that are similar to the menu item File ➪ New Project options.
There are many ways to run the main() method of a class. While all require a run configuration (which contains the main class, runtime arguments, Java version to use, etc.), IntelliJ will create one for you with default values if you simply run the application. To run the application, you can do one of the following:
Click the green arrow to the left of the class name, as shown in
Figure 2.4
.
Click the green arrow to the left of the
main()
method.
Right-click the class editor.
FIGURE 2.4: Running and debugging
All of these give you the option to choose Run from the context menu, which will create a default run configuration. (Debug is also an option. See the “Debugging Your Code” section for more on debugging.) All of these also give you the Modify The Run Configuration option, possibly in a submenu that lets you specify the details of your run configuration.
Alternatively, you can click Current File on top and choose Edit Configurations from the drop-down that appears (Figure 2.5). Despite the name, you have the option to add a new runtime configuration in addition to editing existing ones.
FIGURE 2.5: Editing the configuration
Select an appropriate application type (for example, Application for a vanilla Java application). See Figure 2.6.
FIGURE 2.6: Adding an application configuration
Then complete the configuration form by entering a main class, Java version, and so on, as in Figure 2.7.
If you are in a test class (annotated with @Test; see Chapter 7, “Testing Your Code with Automated Testing Tools”), then you can click the green arrow next to any test method and select the Run option.
FIGURE 2.7: Adding a main() method
Whichever IDE makes you comfortable, our advice is to learn the shortcuts and use them. How do you do that? Every time you look for a function in the menu, look at the keyboard shortcut for that function (listed to the right of the menu item, as shown in Figure 2.8), then close the menu, and use the shortcut! Start with your most frequently used functions until your fingers remember them without thinking.
FIGURE 2.8: Using menus to discover keyboard shortcuts
For example, to get a list of files that you recently worked on, press Ctrl+E/Cmd+E. It will take a few times to remember it, so follow this advice:
Look it up in the menu.
Close the menu.
Use the shortcut.
TIP Many shortcuts differ between Windows and Mac. When that happens, we type the Windows shortcut followed by a slash followed by the Mac version. If you are using Linux, please follow the Windows shortcuts as they are usually the same.
In the following sections, you'll see many useful shortcuts. There will be others throughout the chapter as we cover specific topics. After a short time, the keyboard shortcuts will be etched in your memory, and you will find your productivity increasing greatly.
You want to be able to glide through your codebase without too much mouse clicking. The IDE makes that easy.
To open a class file in IntelliJ, use the shortcut Ctrl+N/Cmd+O. You do not need to type the entire filename, and you can even use camel case to enter prefixes for portions of the name. For example, Figure 2.9 shows how you can look for the beginning of OrderProcessor by typing OrPro.
FIGURE 2.9: Navigating to a class
Similarly, you can open a file that is not a class file using the shortcut Ctrl+Shift+N/Cmd+Shift+O. And you can find a method name or class/instance variable name using Ctrl+Alt+Shift+N/Cmd+Option+O. Figure 2.10 uses the camel case technique to search for the sendEmailConfirmation method by typing sendEmaCon.
FIGURE 2.10: Navigating to a symbol
You can navigate to the last position that you visited, and then the previous and so on, using Ctrl+Alt+Left Arrow/Cmd+[. Then you can navigate in the forward direction using Ctrl+Alt+Right Arrow/Cmd+].
You can navigate to the last edit position using Ctrl+Shift+Backspace/Cmd+Shift+Backspace. While that moves backwards through recent edits, we have not seen a shortcut to move forward afterward. As a work-around, you can just move the cursor anywhere, and then the next Ctrl+Shift+Backspace/Cmd+Shift+Backspace will start again from the most recent edit position.
If you want to find usages of a certain method, you can Ctrl/Cmd-click the method name to get a quick pop-up with this information. Alternatively, you can use Alt+F7/Option+F7 to see the list in a window at the bottom of your IDE.
You can create your own custom navigation by using bookmarks. In IntelliJ, Ctrl+Shift+1 (or any number from 1 to 9) will set a numbered bookmark at the cursor location. Then you can return to that bookmark using Ctrl+1 (or whichever number you used to create the bookmark). These are some of the few shortcuts that are the same on Windows and Mac!
To get a quick outline of the API of the current file, use Ctrl+F12/Cmd+F12, as shown in Figure 2.11. You can click within the outline to go right to that part of the file.
FIGURE 2.11: Outline view
Copy and pasting? You know how to do that already. However, Ctrl+C/Cmd+C works differently from what you are used to with other tools. IntelliJ's philosophy is to not waste time trying to select an entire line; just position your cursor in the line you want to copy, hit Ctrl+C/Cmd+C, and the entire line is copied to the clipboard. Of course, if you want to copy part of a line or multiple lines, you can still select those as usual and hit Ctrl+C/Cmd+C.
Cutting the entire line works similarly using Ctrl+X/Cmd+X. Again, without selecting anything it cuts the entire cursor line to the clipboard.
To retrieve the clipboard, Ctrl+V/Cmd+V works in IntelliJ just like it does in other tools. However, the more useful Ctrl+Shift+V/Cmd+Shift+V displays your clipboard history and lets you select which cut of the clipboard you want to paste, as shown in Figure 2.12.
Next try the unique Ctrl+W/Option+Up Arrow and Ctrl+Shift+W/Option+Down Arrow. Each press of Ctrl+W/Option+Up Arrow starts at the cursor and selects increasing scope, first a word, then a phrase, then a block, a method, a class, and so on. Ctrl+Shift+W/Option+Down Arrow reverses the order, bringing you back one click at a time to your original selection. If you apply Ctrl+W/ Option+Up Arrow to an arithmetic expression, each Ctrl+W/ Option+Up Arrow selects in the order it will calculate.
FIGURE 2.12: Paste Special context menu
There's a trick to selecting an entire method in two clicks, and that is to position your cursor to any space to the left of the method name and hit Ctrl+W/ Option+Up Arrow twice. Another click will select Javadoc and annotations. That is very useful for reordering the methods in your code, because with the entire method selected, you can then use Ctrl+Shift+Up Arrow/Cmd+Shift+Up Arrow and Ctrl+Shift+Down Arrow/Cmd+Shift+Down Arrow to move the method up or down in the file, past the next method above or below it, and will also move any Javadoc along with it. (Good code is self-documenting; great code has great Javadoc!)
First come three shortcuts when working with lines.
Ctrl+D/Cmd+D duplicates the current line.
Ctrl+Y/Cmd+Backspace deletes the current line.
Ctrl+Alt+Up Arrow/Option+Shift+Up Arrow and Down Arrow move the current line up or down, respectively.
Next, when you want to find errors and warnings in your code, use F2 to find the ones after the cursor, and use Shift+F2 to navigate backwards away from the cursor.
The Alt+Enter/Option+Enter shortcut opens a context-sensitive menu. For example, you can use it to fix a compiler error or generate code for your class.