Writing Your First Java Program
Here's a simple "Hello, World!" program in Java:
Let's break it down:
1.
public class Main
: This line declares a
class named "Main" In Java, every program starts with at least one
class definition, and the class name must match the filename (e.g., if the file
is called "Main.java," the class name must be "Main").
2.
public static void
main(String[] args)
: This line is the entry point of the Java program. The main
method is where the
program starts executing. It's written in the form of a function or method and
must have this exact signature for the Java Virtual Machine (JVM) to recognize
it as the starting point.
3.
System.out.println("Hello,
World!")
: This line prints the "Hello, World!" message to the
console. System
is a pre-defined class
in Java that provides access to standard system resources. The out
object is an instance
of PrintStream
, which is used to
display output to the console. The println
method prints the
message and adds a newline at the end.
To run the above program in IntelliJ IDEA IDE follow these
steps:
1. Create a New Java Project: Open IntelliJ IDEA and
click on "Create New Project" or go to File > New > Project
. Choose
"Java" from the left-hand menu and select the appropriate JDK version
you've installed on your system. Click "Next."
2.
Configure Project Settings: Give your project a name
and choose a location to save it. You can keep the default settings for other
options. Click "Finish" to create the project.
3.
Create a New Java Class: In the Project
Explorer on the left-hand side, right-click on the "src" folder,
select New > Java Class
. Name the class "Main"
(matching the code we wrote above) and click "OK."
4.
Write the "Hello,
World!" Code: In the editor window, you should see the empty "Main"
class. Copy and paste the above code into it:
5. Save the File: After pasting the
code, save the file (you can use Ctrl + S
on Windows/Linux or Cmd + S
on macOS).
6.
Run the Program: To run the program,
right-click anywhere inside the main
method (between the
curly braces) and select Run 'Main()'
. Alternatively, you
can click on the green triangle play button above or next to the main
method.
7. View the Output: The output will be displayed in the "Run" window at the bottom of the IntelliJ IDEA interface. You will see the following output:
Comments
Post a Comment