First Java Program The first program that most Java developers learn to write is typically a “Hello, World!” program. Here’s what you need to know to write your first Java program:
- Java requires you to create a class for your program. A class is a blueprint for creating objects. To create a class in Java, you use the ‘class‘ keyword, followed by the name of your class, and then a pair of curly braces that enclose the body of the class.
- Every Java program needs a ‘main‘ method. This is the entry point for your program, where execution begins. The ‘main‘ method should be declared as ‘public static void main(String[] args)‘.
- To print output to the console, you can use the ‘System.out.println()‘ method. This method prints a line of text to the console and adds a newline character at the end.
Here’s an example of a “Hello, World!” program in Java:
public class Helloworld {
public static void main(String[ ] args) {
System.out.println(“Hello, World!”);
}
}
To run this program, you can follow these steps:
- Open a text editor and copy the code above into it.
- Save the file with the name HelloWorld.java.
- Open a command prompt or terminal and navigate to the directory where you saved the train.
- Compile the program by typing Java HelloWorld.java and pressing Enter.
- Run the program by typing java Hello World and pressing Enter.
Popular Java Compiler
- Oracle JDK Compiler – The Oracle JDK compiler is the official compiler for Java and is widely used for development and deployment of Java applications. It is available for free download from the Oracle website.
- OpenJDK Compiler – The OpenJDK compiler is an open-source implementation of the Java Development Kit (JDK). It is maintained by the OpenJDK community and is available for free download.
- Eclipse Compiler – The Eclipse compiler is a Java compiler that is included with the Eclipse Integrated Development Environment (IDE). It is used for development of Java applications and is available for free download.
- Jikes Compiler – The Jikes compiler is an open-source Java compiler that was developed by IBM. It is known for its fast compilation times and is available for free download.
- GCJ Compiler – The GCJ compiler is a Java compiler that is part of the GNU Compiler Collection (GCC). It is used to compile Java code into native machine code and is available for free download.
- Apache Maven Compiler Plugin – The Apache Maven Compiler Plugin is a plugin for the Apache Maven build automation tool. It is used to compile Java code during the build process and is available for free download.
Also Read: First C Program