Introduction to Java Programming.
Chapter 1:
Introduction
1.6 Your
First Java Program: Hello World!
The "Hello World" program is a
traditional first program for any new programming language. It is a simple yet
powerful introduction to the language’s basic syntax, compilation, and execution
process. By successfully writing and running this program, you will gain a
foundational understanding of how Java works.
1.6.1
Structure of a Simple Java Program
Let’s dissect the components of a typical
"Hello World" program in Java. Every executable Java application must
have a class that contains a special method called main.
Full HelloWorld.java Program:
public class HelloWorld {
public static void
main(String[] args) {
// Prints "Hello, World!" to
the console
System.out.println("Hello, World!");
}
}
Now, let’s break down each line:
public class ClassName {
… } |: Understanding the Class Definition
class: This keyword is used to declare a class. In
Java, all executable code must reside within a class. A class is a blueprint or
a template for creating objects.
HelloWorld: This is the name of our class. By convention,
class names in Java start with an uppercase letter and follow PascalCase (e.g., MyAwesomeProgram).
It’s crucial that the class name matches the file name (e.g., HelloWorld.java).
public: This is an access modifier. public means that this class is accessible from anywhere.
For the main class of a Java application, it must be public.
{ … }: These curly braces define the body of the
class. All members (variables and methods) of the class go inside these braces.
public static void main(String[] args) { … } |: The main Method
This is arguably the most important line for a
standalone Java application, as it serves as the program’s starting point. When
you execute a Java program, the Java Virtual Machine looks for and executes
this specific method.
public: This access modifier means the main method
can be accessed from outside the HelloWorld class
(specifically, by the JVM).
static: This keyword means that the main method
belongs to the HelloWorld class itself, rather than
to any specific object of the HelloWorld
class. This allows the JVM to call this method directly using the class name (HelloWorld.main())
without needing to create an object of HelloWorld.
void: This keyword indicates that the main method
does not return any value.
main: This is a special method name recognized by
the JVM as the entry point for the program. It must be spelled exactly as main.
(String[] args): This defines a
parameter for the main method. It’s an array of String objects, which can be
used to receive command-line arguments when the program is run. For "Hello
World," we won’t use it, but it’s a standard part of the main method
signature.
System.out.println("…"); |: Outputting Text to the
Console
This line is responsible for displaying the text
"Hello, World!" on the console.
System: This is a
built-in final class (meaning it cannot be subclassed)
provided by Java’s standard library (java.lang
package, which is automatically imported). It provides access to system
resources.
out: This is a static member of the System class.
It is an object of type PrintStream that represents
the standard output stream, typically the console.
println(): This is a method of the PrintStream
object (out). It stands for "print line" and is used to print the
enclosed text (in this case, "Hello, World!") to the console,
followed by a new line character, moving the cursor to the next line. There’s
also print() which does not add a new line.
;: The semicolon at the end of the line is a
statement terminator in Java, indicating the end of a statement.
1.6.2 Writing the Code
Let’s create the HelloWorld.java file
step-by-step:
Open a Text Editor: Use any plain text editor (like Notepad on
Windows, TextEdit on macOS,
VS Code, or Notepad++). Do not use a word processor (like Microsoft
Word), as they add formatting that will interfere with the code.
Type the Code: Carefully type (or copy-paste) the exact code provided above
into your text editor:
public class HelloWorld {
public static void
main(String[] args) {
System.out.println("Hello, World!");
}
}
Pay close
attention to capitalization, punctuation, and curly braces.
Save the File: Save the file with the same name as your public class, followed
by the .java extension. In this case, save it as HelloWorld.java. It’s
generally a good practice to save it in a simple, easy-to-access directory
(e.g., C:\java_programs on Windows or ~/java_programs
on Linux/macOS).
1.6.3
Compiling and Running from the Command Line
Once you have saved your HelloWorld.java file,
you can compile and run it using the command-line tools provided by the JDK.
This is a crucial skill for understanding the underlying Java environment.
Open Command
Prompt / Terminal:
Windows: Search for "cmd" or
"Command Prompt" and open it.
macOS / Linux: Open the "Terminal" application.
Navigate to Your File’s Directory: Use the cd (change directory) command to go
to the folder where you saved HelloWorld.java.
Example:
cd
C:\java_programs
○
Example (macOS/Linux):
cd ~/java_programs
Compile the Java Source Code:
Use the javac command to
compile your .java file into bytecode.
javac HelloWorld.java
What happens: If there are no syntax errors in your code,
this command will generate a new file named HelloWorld.class
in the same directory. This .class file contains the Java bytecode
that the JVM can understand. If javac reports errors,
review your HelloWorld.java file for typos, incorrect capitalization, or
missing semicolons.
Run the Compiled Java Program:
Use the java (Java interpreter/launcher) command to
execute the compiled bytecode.
java HelloWorld
What happens: This command instructs the JVM to load the HelloWorld.class file and start execution from its main
method.
Expected Output: You should see the following text displayed
in your command prompt/terminal:
Hello, World!
1.6.4 Running in an IDE
Integrated Development Environments like
Eclipse and NetBeans automate the compilation and
execution process, making development much faster and more convenient. While
the underlying steps (compilation, JVM execution) are the same, the IDE handles
them behind the scenes.
If you have followed the setup instructions in
Section 1.5.2, you can easily run your "Hello World" program within
your chosen IDE:
In Eclipse or NetBeans:
Create a New Java Project: As detailed in Section 1.5.2, create a new
Java project (e.g., MyHelloWorldProject).
Create a New Class: Within that project, create a new Java class
named HelloWorld and ensure the public static void main(String[] args) method is
automatically generated.
Write the Code: Inside the main method, add the line:
System.out.println("Hello, World from IDE!");
Save the File: The IDE usually saves automatically or prompts you to save.
Run the Program:
■
Eclipse: Right-click anywhere in the HelloWorld.java
editor window, then select Run As > Java Application.
■
NetBeans: Click the green "Run Project" button in the toolbar,
or right-click on the project in the "Projects" window and select
Run.
View Output: The output will appear in the IDE’s built-in console window
(often at the bottom of the screen):
Hello, World from
IDE!
By successfully completing this "Hello
World" exercise, you have taken your first significant step into Java
programming! You now understand the basic structure of a Java program and how
it moves from source code to executable output.