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

Java If-else Statement Java If-else Statement The Java if statement is used to test the condition. It checks boolean condition: true or false. For example, if a number i...
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...
How to install flutter | Flutter tutorial Flutter is the google’s mobile app SDK for develop the high-quality native app for iOS and android. We can develop the app by use of DART and javascri...
Drag and Drop Collection View cell in IOS 11 &... Drag and Drop Collection View cell in IOS 11 & swift 4 In this tutorial we are explain how to drag and drop collection view cell in two collectio...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...

Leave a Reply

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