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

Asset Color Catalog | Swift Tutorial Asset Color Catalog is the feature of XCode 9. BY the use of this feature we can maintain the colors easily and avoid the mistakes. We can create the ...
Introduction To C A Brief History Of C   C was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used the Unix operating system. C is the r...
Navigation Tab Bar in IOS | Swift tutorial Navigation Tab Bar in IOS | Swift tutorial Different Customizing techniques for Navigation bar with storyboard and code 1) Custom NavigationTabBar b...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Create Firebase single Page Website Create firebase single page Website tutorial In this tutorial we are study how to create single page website by the use of firebase hosting its very ...
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.