Getting Started with IntelliJ IDEA - datacouch-io/spark-java GitHub Wiki
Introduction to IntelliJ IDEA
IntelliJ IDEA is a popular Integrated Development Environment (IDE) for Java developers. It provides a feature-rich and user-friendly environment for writing, debugging, and managing Java applications. In this training, we'll explore the essential features of IntelliJ IDEA and learn how to use it effectively for Java development.
Key Features of IntelliJ IDEA
-
Code assistance and intelligent code completion
-
Powerful code navigation and search tools
-
Built-in version control integration (Git, SVN, etc.)
-
Extensive support for testing frameworks (JUnit, TestNG, etc.)
-
Seamless integration with popular build tools (Maven, Gradle)
-
Comprehensive debugging capabilities
-
Refactoring tools for code optimization
-
Support for various Java frameworks and libraries
Let’s get Started
Installing and Launching IntelliJ IDEA
Before we dive into IntelliJ IDEA, make sure you have it installed on your system. You can download IntelliJ IDEA from the official website.
After installation, launch IntelliJ IDEA, and you'll be greeted with the welcome screen. Here are a few essential elements to get started:
-
Create New Project: Start a new Java project or open an existing one
-
Open Project: Open an existing project from your file system
-
Configure: Customize your IDE settings and plugins
-
Get from Version Control: Clone a project from a version control system (e.g., Git)
Creating First Project
Let's create a new Java project:
- Click on "New Project" on the welcome screen
- Choose "Java" from the list of project then click on next
- Click on next
-
Project Name: Give your project a meaningful name (for instance; CalculatorProject )
-
Project Location: Choose where to save your project files
- Click "Create" to create the project
Writing and Running Java Code
Now that we have our project set up, let's create a simple Java class and run it:
Creating a Java Class
-
In the Project Explorer, right-click on the "src" folder
-
Select "New > Java Class”
-
Enter a class name (e.g., "Calculator")
-
Click "OK" to create the class
Writing Java Code
In the Calculator class, write the following Java code:
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static int subtract(int a, int b) {
return a - b;
}
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
int sum = add(num1, num2);
int difference = subtract(num1, num2);
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
}}
Running the Java Code
-
To run your code, right-click within the main method in the Calculator class and select "Run 'Calculator.main()'”
-
You should see the output in the "Run" tool window at the bottom of the IDE. The output will display the sum and difference of num1 and num2
Debugging Your Code
IntelliJ IDEA provides robust debugging tools to help you identify and fix issues in your code. Now, let's debug the Calculator class:
-
Place breakpoints in your code by clicking in the gutter next to the line numbers. You can place breakpoints on lines like int sum = add(num1, num2); and int difference = subtract(num1, num2);
-
Click the "Debug" button or right-click within the main method and select "Debug 'Calculator.main()'"
-
The debugger will stop at the breakpoints you set. You can use the debugger controls to navigate through your code and inspect variable values
-
For example, you can hover over variables like num1, num2, sum, and difference to see their values
-
You can also add watches and evaluate expressions in the "Debug" tool window
-
To continue debugging, use the controls like "Step Over", "Step Into", and "Resume Program"
-
Fix any issues in your code and continue debugging until your code behaves as expected.
This example demonstrates how to write, run, and debug Java code in IntelliJ IDEA using the Calculator class. You can apply these debugging techniques to your own projects to identify and resolve issues effectively.
Working with Packages and Classes
In this step, we'll create a simple Java package, create classes within that package, and demonstrate how to work with packages and classes in IntelliJ IDEA.
Creating a Java Package
-
In the Project Explorer (left sidebar), right-click on the "src" folder
-
Select "New > Package"
-
Enter a package name, for example, "com.myapp.util" (You can use any name you prefer)
-
Click "OK" to create the package
Creating Java Classes
Now, let's create two Java classes within the package:
Class: MathUtil.java
-
Right-click on the newly created package (e.g., "com.myapp.util")
-
Select "New > Java Class"
-
Enter the class name as "MathUtil" and click "OK"
-
In the MathUtil class, write the following code:
package com.myapp.util; public class MathUtil { public static int add(int a, int b) { return a + b; } public static int subtract(int a, int b) { return a - b; } }
Class: MainApp.java
-
Right-click on the same package (e.g., "com.myapp.util").
-
Select "New > Java Class"
-
Enter the class name as "MainApp" and click "OK"
-
In the MainApp class, write the following code:
package com.myapp.util; public class MainApp { public static void main(String[] args) { int num1 = 20; int num2 = 10; int sum = MathUtil.add(num1, num2); int difference = MathUtil.subtract(num1, num2); System.out.println("Sum: " + sum); System.out.println("Difference: " + difference); } }
Using Classes in Different Packages
Notice that we have created classes in the "com.myapp.util" package. To use these classes from another package, you need to import them. IntelliJ IDEA makes this process seamless:
-
In the MainApp class, IntelliJ IDEA will automatically suggest adding the required import statements. If not, you can manually add them at the top of the MainApp.java file:
import com.myapp.util.MathUtil;
-
Now, you can use the MathUtil class in your MainApp class.
Running the Application
To run the application:
-
Right-click within the main method in the MainApp class.
-
Select "Run 'MainApp.main()'"
-
You should see the output in the "Run" tool window at the bottom of the IDE. It will display the sum and difference of num1 and num2.
This example demonstrates how to work with packages and classes in IntelliJ IDEA, including creating packages, classes, and importing classes from different packages.
Conclusion
IntelliJ IDEA is a powerful and versatile IDE that provides excellent support for Java development. In this training, we covered the basics of creating projects, writing and debugging code, organizing classes, building and running projects, and using Git for version control. As you explore more features, you'll find that IntelliJ IDEA enhances your productivity and helps you write high-quality Java applications. Happy coding!