Skip to content

Commit c450ec3

Browse files
committed
Fixes from review by Ana
1 parent ebf8d10 commit c450ec3

File tree

7 files changed

+38
-30
lines changed

7 files changed

+38
-30
lines changed
83.7 KB
Loading
4.76 MB
Loading
1.18 MB
Loading
479 KB
Loading
-543 KB
Binary file not shown.
5.69 MB
Loading

app/pages/learn/01_tutorial/01_your-first-java-app/02_building-with-intellij-idea.md

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ toc:
2323
- Documenting code {document}
2424
- Searching and navigating {navigation}
2525
- Summary {summary}
26-
description: "Learn how to code, run, test, debug and document a Java application faster in IntelliJ IDEA."
27-
last_update: 2024-04-04
26+
description: "Learn how to code, run, test, debug and document a Java application in IntelliJ IDEA."
27+
last_update: 2024-04-22
2828
author: ["MaritvanDijk"]
2929
---
3030

3131
<a id="overview">&nbsp;</a>
3232
## Overview
33-
An IDE (Integrated Development Environment) allows you to quickly build applications, by integrating a source-code editor with the ability to compile and run your code, as well as integration with tools you’ll need for software development, including build tools, testing and debugging tools, version control, and so on. And finally, an IDE will let you search and navigate your codebase in ways your file system won’t.
33+
An IDE (Integrated Development Environment) allows you to quickly create applications, by combining a source-code editor with the ability to compile and run your code, as well as integration with build, test and debug tools, version control systems, and so on. Finally, an IDE will let you search and navigate your codebase in ways your file system won’t.
3434

3535
One of the [most widely used integrated development environments (IDEs)](https://www.jetbrains.com/lp/devecosystem-2023/java/#java_ide) for Java is IntelliJ IDEA. Its user-friendly interface, rich feature set, and vast ecosystem make it an ideal environment for beginners to learn and grow as developers. In this tutorial you’ll learn how to use some of its features to simplify your development process and accelerate your learning curve with Java programming.
3636

@@ -78,26 +78,31 @@ IntelliJ IDEA will create a project for you, with a basic `pom.xml` and a defaul
7878

7979
We can see the project structure in the [Project tool window](https://www.jetbrains.com/help/idea/project-tool-window.html) on the left. The `.idea` folder contains your project configuration. The `src` folder contains your code. When you expand that folder, you'll see that IntelliJ IDEA has created a `main` folder for your Java code and a `test` folder for your tests.
8080

81-
To simplify the view on your project, for example to focus on the package structure, click the title bar to select the desired view from the list.
81+
Let’s add some code by creating a new class.
82+
* In the **Project** tool window on the left, select the directory `src/main/java`.
83+
* To add a new Java class, right-click the **Project** tool window to open the context menu and select **New | Java class**.
84+
* Name this class `HelloWorld`.
8285

83-
[![Project view](/assets/images/intellij-idea/project-view.png)](/assets/images/intellij-idea/project-view.png)
86+
[![New Java class](/assets/images/intellij-idea/new-java-class.png)](/assets/images/intellij-idea/new-java-class.png)
8487

85-
Let’s add some code. We’ll start by creating a new class. In the **Project** tool window on the left, select the directory `src/main/java`. Add a new Java file using the shortcut **⌘N** (on macOS) or **Alt+Insert** (on Windows/Linux). Alternatively, right-click the **Project** tool window to open the context menu and select **New | Java class**. Name this class `HelloWorld`.
88+
---
89+
**IDE TIP**
8690

87-
[![New Java class](/assets/images/intellij-idea/new-java-class.png)](/assets/images/intellij-idea/new-java-class.png)
91+
You can add a new Java file using the shortcut **⌘N** (on macOS) or **Alt+Insert** (on Windows/Linux).
92+
93+
---
8894

8995
<a id="edit">&nbsp;</a>
9096
## Writing and editing code
91-
Inside the class `HelloWorld`, we will write the main method; which is where execution of our Java program will start.
92-
The classic way to write a main method in Java is:
97+
The entrypoint to execute your `HelloWorld` Java program is the main method. Add the main method by typing:
9398
```java
9499
public static void main(String[] args) {
95100

96101
}
97102
```
98-
Add a main method to your HelloWorld class. Type the code, rather than pasting it; this will help you get more familiar with the syntax.
103+
I recommend that you type the code, rather than pasting, as that will help you get more familiar with the syntax.
99104

100-
Once you've added the main method, let's also add code to print "Hello World!":
105+
To keep things simple, let's make our program print `Hello World!` to the console and move the cursor to a new line, by adding `System.out.println("Hello World!");` to the method body:
101106

102107
```java
103108
public static void main(String[] args) {
@@ -113,7 +118,7 @@ IntelliJ IDEA will show you if you’ve typed or selected something that doesn
113118

114119
[![Suggestions](/assets/images/intellij-idea/alt-enter.png)](/assets/images/intellij-idea/alt-enter.png)
115120

116-
To speed up development, we can also [generate code](https://www.jetbrains.com/help/idea/generating-code.html). IntelliJ IDEA can generate constructors, getters and setters, toString(), equals() and hashCode() methods, and more.
121+
To speed up development, we can also [generate code](https://www.jetbrains.com/help/idea/generating-code.html). IntelliJ IDEA can generate constructors, getters and setters, `toString()`, `equals()` and `hashCode()` methods, and more.
117122

118123
[![Generate code](/assets/images/intellij-idea/generate-code.png)](/assets/images/intellij-idea/generate-code.png)
119124

@@ -123,7 +128,7 @@ IntelliJ IDEA will manage the formatting of your code as you write it. If needed
123128
## Running your application
124129
A major benefit of using an IDE is that you can directly run your code without having to first manually compile it on the command line.
125130

126-
You can run our application directly from the editor, by clicking the green run button in the gutter near the class declaration, or using the shortcut **⌃⇧R** (on macOS) or **Ctrl+Shift+F10** (on Windows/Linux).
131+
You can run the `HelloWorld` application directly from the editor, by clicking the green run button in the gutter near the class declaration, or using the shortcut **⌃⇧R** (on macOS) or **Ctrl+Shift+F10** (on Windows/Linux).
127132

128133
Alternatively, we can run our application using the green Run button in the top right corner, or using the shortcut **⌃R** (on macOS) or **Ctrl+F10** (on Windows/Linux) to run the latest file.
129134

@@ -135,19 +140,19 @@ To edit your run configurations, select the configuration in the run/debug confi
135140

136141
[![Edit Configurations](/assets/images/intellij-idea/edit-configurations.png)](/assets/images/intellij-idea/edit-configurations.png)
137142

138-
Edit the **Run/Debug Configurations** in the popup.
143+
The popup **Run/Debug Configurations** appears and there you can modify JVM options, add program arguments and many more.
139144

140145
[![Run / Debug Configuration](/assets/images/intellij-idea/run-config.png)](/assets/images/intellij-idea/run-config.png)
141146

142147
<a id="test">&nbsp;</a>
143148
## Testing
144149
Testing your code helps you to verify that the code does what you expect it to do. You can run your application and test it yourself, or add automated tests that can verify your code for you. Thinking about what to test and how, can help you to break a problem up into smaller pieces. This will help you get a better solution faster!
145150

146-
For example, let's say we have the following method that calculates the average of a list of values, and we want to make sure the average is calculated correctly:
151+
For example, let's say we have a `Calculator` class containing the following method that calculates the average of a list of values, and we want to make sure the average is calculated correctly:
147152
```java
148-
public class Average {
153+
public class Calculator {
149154

150-
public static double calculateAverage(int[] numbers) {
155+
public static double average(int[] numbers) {
151156
int sum = 0;
152157
for (int number : numbers) {
153158
sum += number;
@@ -166,22 +171,22 @@ IntelliJ IDEA supports multiple testing libraries, including [JUnit 5](https://j
166171

167172
Note that the JUnit 5 dependency `junit-jupiter` is added to the `pom.xml` in the `<dependencies>` section.
168173

169-
[![JUnit5 dependencies](/assets/images/intellij-idea/junit5-dependencies.png)](/assets/images/intellij-idea/JUnit5-dependencies.png)
174+
[![JUnit5 dependencies](/assets/images/intellij-idea/junit5-dependencies.png)](/assets/images/intellij-idea/junit5-dependencies.png)
170175

171176
Go back to the test file to add tests. We can let IntelliJ IDEA help us generate our test for us. In the test class, we can use **Generate** (**⌘N** on macOS or **Alt+Insert** on Windows/Linux) and select **Test Method** to add a test. Give the test a name that explains the intended behavior, and add the relevant test code.
172177

173-
For example, let's make sure that the method `calculateAverage()` correctly calculates the average for an array of positive numbers, and returns `0` for an empty array. You might want to add additional tests, for example, including negative numbers.
178+
For example, let's make sure that the method `average()` correctly calculates the average for an array of positive numbers, and returns `0` for an empty array. You might want to add additional tests for different scenarios, such as an array of negative numbers, a mix of positive and negative numbers, etc.
174179
```java
175180
import org.junit.jupiter.api.Test;
176181
import static org.junit.jupiter.api.Assertions.assertEquals;
177182

178-
public class AverageTest {
183+
public class CalculatorTest {
179184

180185
@Test
181186
void shouldCalculateAverageOfPositiveNumbers() {
182187
int[] numbers = {1, 2, 3, 4, 5};
183188

184-
double actualAverage = Average.calculateAverage(numbers);
189+
double actualAverage = Calculator.average(numbers);
185190

186191
double expectedAverage = 3.0;
187192
assertEquals(expectedAverage, actualAverage);
@@ -191,7 +196,7 @@ public class AverageTest {
191196
void shouldReturnZeroAverageForEmptyArray() {
192197
int[] numbers = {};
193198

194-
double actualAverage = Average.calculateAverage(numbers);
199+
double actualAverage = Calculator.average(numbers);
195200

196201
double expectedAverage = 0;
197202
assertEquals(expectedAverage, actualAverage);
@@ -210,15 +215,16 @@ To add a breakpoint, click the gutter at the line of code where you want executi
210215

211216
Execution will stop at the breakpoint, so we can investigate the state of our application. We can see current values of variables and objects. We can evaluate an expression, to see its current value and look at more details. We can even change the expressions to evaluate different results. We can continue execution by either stepping into a method to see what happens inside a called method (using the shortcut **F7**, or the corresponding button in the *Debug* tool window) or stepping over a line to go to the next line even if a method is called (using the shortcut **F8**, or the corresponding button in the *Debug* tool window), depending on what we’re interested in. Finally, we can resume the program to finish the execution of the test.
212217

213-
Let's debug the failing test from the previous section. In the code, place a breakpoint on line 4. Run the failing test through the debugger. Step over the code until you get to line 8, and observe the values of the variables. When we get to line 8, select `sum / numbers.length`, right-click to open the context menu and select **Evaluate Expression**. Press **Enter** to evaluate the selected expression. We see that `sum / numbers.length` results in a `java.lang.ArithmeticException: / by zero`. The empty array has a length of `0` and Java does not allow dividing by zero. When we try to cast this Exception to a `(double)` we get `NaN`; an exception is not a number.
218+
Let's debug the failing test from the previous section. In the code, place a breakpoint on line 4. Run the failing test through the debugger. Step over the code until you get to line 8, and observe the values of the variables. When we get to line 8, select `sum / numbers.length`, right-click to open the context menu and select **Evaluate Expression**. Press **Enter** to evaluate the selected expression. We see that `sum / numbers.length` results in a `java.lang.ArithmeticException: / by zero`. The empty array has a length of `0` and Java does not allow dividing by zero.
219+
When we evaluate `(double) sum / numbers.length` we get the result `NaN`. We expected `0`, so our test fails.
214220

215221
[![Debugging](/assets/images/intellij-idea/debug.gif)](/assets/images/intellij-idea/debug.gif)
216222

217-
Let's fix our code by immediately returning `0` as the average of an empty array:
223+
As we didn't consider this scenario in our initial implementation, we can fix this by changing our method to return `0` when an empty array is given as input:
218224
```java
219-
public class Average {
225+
public class Calculator {
220226

221-
public static double calculateAverage(int[] numbers) {
227+
public static double average(int[] numbers) {
222228
if (numbers.length == 0) {
223229
return 0;
224230
}
@@ -238,11 +244,13 @@ For more information on debugging, see [Debugging in Java](https://dev.java/lear
238244
<a id="refactor">&nbsp;</a>
239245
## Refactoring code
240246

241-
While working with the code, we may want to make small improvements without changing the functionality. We can use [refactoring](https://www.jetbrains.com/help/idea/refactoring-source-code.html) to reshape the code. We can rename classes, variables and methods using **Refactor | Rename** (**⇧F6** on macOS, or **Shift+F6** on Windows/Linux).
247+
While working with our code, we may want to make small improvements without changing the functionality. We can use [refactoring](https://www.jetbrains.com/help/idea/refactoring-source-code.html) to reshape the code.
242248

243-
We can inline variables (**⌘⌥N** on macOS, or **Ctrl+Alt+N** on Windows/Linux), or extract variables (**⌘⌥V** on macOS, or **Ctrl+Alt+V** on Windows/Linux) as needed.
249+
* We can rename classes, variables and methods using **Refactor | Rename** (**⇧F6** on macOS, or **Shift+F6** on Windows/Linux).
250+
* We can inline variables (**⌘⌥N** on macOS, or **Ctrl+Alt+N** on Windows/Linux), or extract variables (**⌘⌥V** on macOS, or **Ctrl+Alt+V** on Windows/Linux) as needed.
251+
* We can break long methods into smaller parts by extracting a method (**⌘⌥M** on macOS, or **Ctrl+Alt+M** on Windows/Linux) and giving it a meaningful name.
244252

245-
We can break long methods into smaller parts by extracting a method (**⌘⌥M** on macOS, or **Ctrl+Alt+M** on Windows/Linux) and giving it a meaningful name. We can refactor the code to a more familiar style, or to use new idioms and language features.
253+
All these options help us refactor our code to a more familiar style, or to use new idioms and language features.
246254

247255
Pull up the refactoring menu to see what is possible, using the shortcut **⌃T** (on macOS) or **Ctrl+Alt+Shift+T** (on Windows/Linux).
248256

@@ -252,7 +260,7 @@ Pull up the refactoring menu to see what is possible, using the shortcut **⌃T*
252260
## Documenting code
253261
We can add documentation to our code. IntelliJ IDEA provides completion for documentation comments, which is enabled by default. Type `/**` before a declaration and press **Enter**. IntelliJ IDEA auto-completes the documentation comment for you.
254262

255-
We can make the Javadoc comments easier to read by selecting Reader Mode. We can toggle to reader mode in the editor using **^⌥Q** (on macOS) or **Ctrl+Alt+Q** (on Windows/Linux). Right-click the icon in the gutter to select **Render All Doc Comments** if you want all comments to show in reader mode.
263+
IntelliJ IDEA provides a way for you to easily understand and read JavaDoc comments by selecting _Reader Mode_. **Toggle Reader Mode** in the editor using **^⌥Q** (on macOS) or **Ctrl+Alt+Q** (on Windows/Linux). Right-click the icon in the gutter to select **Render All Doc Comments** if you want all comments to show in reader mode.
256264

257265
<a id="navigation">&nbsp;</a>
258266
## Searching and navigating

0 commit comments

Comments
 (0)