First Java Program

First Java Program

NOTE: You need to have java installed on your system.
Step 1:

Open a text editor, like Notepad on windows and TextEdit on Mac. Write a Java Program as similar as mentioned below, in the text editor.

public class FirstJavaProgram{
public static void main(String[] args){
System.out.println(“This is my first java program.”)
}//End of main
}//End of FirstJavaProgram Class

Output:

This is my first java program.

Note: You can also use IDE like Eclipse to run the java program instead of text editor but we will cover that part later in the coming tutorials.
Step 2:

Save the file as “FirstJavaProgram.java”. In our program, “FirstJavaProgram is the class name and we should always save the program with “className.java”, that’s why our file name should be “FirstJavaProgram.java”.

Step 3:

Now we will compile the program. For this, open “cmd” on Windows, and if you are using Mac OS then open “Terminal”.
To compile the program, type the following command and hit enter.
javac FirstJavaProgram.java

Note: You may get this error when you try to compile the program: “javac’ is not recognized as an internal or external command, operable program or batch file“. This error occurs when the java path is not set in your system

If you get this error then you first need to set the path before compilation as discussed in previous tutorial.

Step 4:

After compilation the “.java” file, it gets translated into the “.class” file(byte code). Now we can run the program. To run the program, type the following command and hit enter:
java FirstJavaProgram

Complete look to the First Java Program

Now that we have understood that how to run a java program, let have a closer look at the program we have written above.

1. public class FirstJavaProgram{

This is the first line of our java program. Every java application must have at least one class definition that consists of ‘class’ keyword followed by ‘class name’. A keyword ‘class’ should never be chaged, we should use it as it is. However the class name can be anything.

I have made the class public by using public access modifier, I will cover access modifier in a separate post, all you need to know now that a java file can have any number of classes but it can have only one public class and the file name should be same as public class name.

2. public static void main(String[] args){

This is our next line in the program, lets understand it:

public:

This makes the main method public that means that we can call the method from outside the class.

static:

We do not need to create object for static methods to run. They can run itself.

void:

It does not return anything.

main:

It is the method name. This is the entry point method from which the JVM can run your program.

(String[] args):

Used for command line arguments that are passed as strings. We will cover that in a separate post.

3. System.out.println(“This is my first program in java”);

This method prints the contents inside the double quotes into the console and inserts a newline after.

We will discuss all the terms in the separate post.

Related Post

Control Flow Statements | Swift tutorial Control Flow : Swift tutorial Swift Provides diffrent type of controll flow statements like: Loops Statements : Loops are used for perform the t...
Loops in java Loops in JAVA Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. Loop is used in programming ...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Strings String can be represented as a single-dimensional character type array. Strings in C are group of characters, digits, and symbols enclosed in quotatio...
Variables in Java Variables in Java A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” wh...
C Language – Introduction Why C? There are three important aspects of any language so as C has : The way it stores data, The way it operates on this data and How th...

Leave a Reply

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.