In this section, we will learn how to write the simple program of Java. We can write a simple hello Java program easily after installing the JDK.
To create a simple Java program, you need to create a class that contains the main method. Let’s understand the requirement first.
The requirement for Java Hello World Example
For executing any Java program, the following software or application must be properly installed.
- Install the JDK if you don’t have installed it, download the JDK and install it.
- Set path of the jdk/bin directory.
- Create the Java program
- Compile and run the Java program
here’s an example of a simple “Hello World” program in Java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compilation Flow:
When you write a Java program, you create a source code file with a .java
extension. To execute this program, you need to follow these steps:
- Compilation: The first step is to compile the source code file into a platform-independent binary format called bytecode. To do this, you use the
javac
compiler, which is included in the Java Development Kit (JDK). The compiler reads the source code file and generates a bytecode file with a.class
extension. - Loading: Once the bytecode file is generated, you can load it into memory using the Java Virtual Machine (JVM). The JVM is a software component that interprets the bytecode and executes it on the underlying hardware. The loading process involves verifying the bytecode to ensure that it is valid and secure.
- Execution: Once the bytecode is loaded into memory, the JVM executes it line by line, interpreting each instruction and performing the corresponding actions. As the program executes, it may interact with the system by reading input from the user, writing output to the console, or accessing files or other resources.
- Termination: When the program finishes executing, the JVM terminates and releases the memory used by the program. At this point, any resources that were opened by the program should be closed, and any finalization code should be executed.
In summary, the Java compilation flow involves compiling the source code into bytecode, loading the bytecode into memory, executing it on the JVM, and terminating the program when it completes.
Parameters used in First Java Program
In the “Hello World” example program in Java, there is only one parameter used, which is the String[] args
parameter of the main
method. This parameter is an array of strings that represent the command-line arguments passed to the program when it is executed.
Here’s a breakdown of the String[] args
parameter:
- class keyword is used to declare a class in Java.
- public keyword is an access modifier that represents visibility. It means it is visible to all.
- static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main() method is executed by the JVM, so it doesn’t require creating an object to invoke the main() method. So, it saves memory.
- void is the return type of the method. It means it doesn’t return any value.
- main represents the starting point of the program.
String[]
: This indicates that the parameter is an array of strings.args
: This is the name of the parameter, which can be any valid Java identifier.[]
: This indicates that the parameter is an array, which means it can contain multiple values.
In the “Hello World” program, we don’t actually use the args
parameter, but it’s included in the main
method because it’s a required parameter. If you wanted to modify the program to use command-line arguments, you could access the values in the args
array using the array indexing syntax, like this:
public class HelloWorld {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("Hello, " + args[0] + "!");
} else {
System.out.println("Hello, World!");
}
}
}
In this modified program, we check whether the args
array has any elements (i.e., whether any command-line arguments were passed), and if it does, we use the first element as the name to greet. If no arguments were passed, we fall back to the default “Hello, World!” greeting.
In how many ways we can write a Java program?
There are many ways to write a Java program, depending on the requirements and the preferences of the developer. However, all Java programs share a few common elements, such as the use of classes and objects, the syntax for defining methods and variables, and the use of control structures like loops and conditionals.
Here are a few common ways to write a Java program:
- Using an Integrated Development Environment (IDE): An IDE is a software application that provides a complete environment for writing, testing, and debugging code. IDEs typically include a code editor with syntax highlighting, code completion, and other helpful features, as well as tools for compiling, running, and debugging code. Examples of popular Java IDEs include Eclipse, IntelliJ IDEA, and NetBeans.
- Using a text editor and command-line tools: If you prefer a more minimalist approach, you can write Java code using a plain text editor like Notepad or Vim, and then compile and run the code using command-line tools like
javac
andjava
. This approach requires a bit more setup and configuration than using an IDE, but it can be more lightweight and flexible. - Using an online Java compiler: There are many websites that provide online Java compilers, which allow you to write and run Java code directly in your web browser. These compilers are often simpler than full-fledged IDEs, but they can be useful for quick prototyping and testing.
Regardless of which approach you choose, you’ll need to follow the rules and syntax of the Java programming language, which are defined in the Java Language Specification. This includes using proper syntax for defining classes, methods, and variables, and following best practices for coding style and organization.