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

Dart Data type | Dart Tutorial Dart Data type Variables can store data of different types, and different data types can do different things. Dart supports the following data typ...
Resignation Letter and Relieving Letter Resignation Letter Sample 1 Sender’s Name, Sender’s Address, Mobile Number, MailDate:ToThe Human Resource Department Company Name Company Addre...
UIPopOverPresentationController In Swift UIPopOverPresentationController UIPopOverViewController in swift is writen in swift 3.1 and Use the xcode Version 8.3.2. For use this import the Pop...
Decision Control Statement C has three major decision making instructions— the if statement, the if-else statement, and the switch statement. The if Statement It is used to ex...
Introduction to Java Programming Language JAVA TUTORIALS Introduction to Java Programming Language What is JAVA Java is programming language that produces software for multiple platforms, w...
Create UITableView (Table View) By the use of stor... How to use UITableView in Above IOS 8 and Swift (UITableView) In this tutorial we are study how to use the TableView in Swift. One of the most common...

Leave a Reply

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