This project walks you through writing your very first Java program. Java is a versatile programming language used to create a wide range of applications, from mobile apps to games. If you're new to programming, don't worry! This guide provides a step-by-step approach to help you write, run, and modify your first program.
By the end of this project, you will have:
- Learned how to create and structure a Java project.
- Written a simple program that prints messages to the console.
- Experimented with modifying and running the program.
- Launch IntelliJ IDEA on your computer.
- On the welcome screen, click New Project.
- Enter the following details:
- Name:
Java_Project
- Location: (Use the default or specify your folder.)
- Build System:
IntelliJ
- JDK:
21
- Name:
- Uncheck Add a sample code, then click Create.
- In the Project Structure panel on the left, locate your
Java_Project
folder. - Right-click the
src
folder, then select New > Java Class. - Name your class
MyFirstProgram
and press Enter.
- Open the newly created
MyFirstProgram.java
file. You will see:public class MyFirstProgram { }
- Add the
main
method inside the class:public static void main(String[] args) { }
- Add a print statement within the
main
method to display a message:public static void main(String[] args) { System.out.println("I'm a Programmer"); }
- Your final program should look like this:
public class MyFirstProgram { public static void main(String[] args) { System.out.println("I'm a Programmer"); } }
- Click the Run button (green triangle icon) at the top-right corner of IntelliJ to compile and execute your program.
- The Console at the bottom of the IDE will display the message:
I'm a Programmer
Try modifying the program by:
- Printing a different message:
System.out.println("Let’s change the code now...");
- Adding another line to print a second message:
System.out.println("It’s what I do!");
The output will look like this:
Let’s change the code now...
It’s what I do!
-
public class MyFirstProgram
: Declares a new class named MyFirstProgram. -
public static void main(String[] args)
: Defines the main method, which is the entry point of every Java program. -
System.out.println("...")
: Prints the specified message to the console.
Congratulations on writing and running your first Java program! Programming is all about experimenting and learning from your mistakes. Keep practicing and exploring new concepts to improve your skills.
- IntelliJ IDEA