tutorialcup
Compile and Run a Java program
How to run a java program - Let us see how to write, compile, and run a simple java program. We can either use a command prompt or any IDE like Eclipse to compile and run any Java program. In this tutorial, we will see both methods.
Prerequisite
We should have the below prerequisites before we start writing our first Java program then we will learn how to run a java program?
- Install Java in your system.
- A text editor or IDE like Eclipse.
Simple Java Program
Below is a simple Java program to print a line of the statement.
public class MyJavaProgram {
public static void main(String args) {
System.out.println("This is my first Java program");
}
}
This is my first Java program
Explanation of the Java Program
Let us see in detail what each line means in the simple Java program given above.
public class MyJavaProgram {
Here, public is the access modifier which means that this class can be accessed anywhere. Next is the keyword class followed by the class name. We should always remember that the class name should be the same as the filename.
public static void main(String args) {
public - states that this method can be accessed from outside the class
static - which means we don't need to create an object to run this method
void - does not return any value
main - it is the method name.
Compile and Run a Java program
How to run a java program - Let us see how to write, compile, and run a simple java program. We can either use a command prompt or any IDE like Eclipse to compile and run any Java program. In this tutorial, we will see both methods.
Prerequisite
We should have the below prerequisites before we start writing our first Java program then we will learn how to run a java program?
- Install Java in your system.
- A text editor or IDE like Eclipse.
Simple Java Program
Below is a simple Java program to print a line of the statement.
public class MyJavaProgram {
public static void main(String args) {
System.out.println("This is my first Java program");
}
}
This is my first Java program
Explanation of the Java Program
Let us see in detail what each line means in the simple Java program given above.
public class MyJavaProgram {
Here, public is the access modifier which means that this class can be accessed anywhere. Next is the keyword class followed by the class name. We should always remember that the class name should be the same as the filename.
public static void main(String args) {
public - states that this method can be accessed from outside the class
static - which means we don't need to create an object to run this method
void - does not return any value
main - it is the method name.