Tutorials

Java Basics


Java First Program


We will write our first java program now. 

 

Please read the code snippet given below:


public class HelloJava {

      public static void main(String[] args) {

            System.out.print("My first program in java");

      }

}

OUTPUT:

My first program in java

 

Compile & Run on Eclipse:

Follow the below given steps,

  • Open the eclipse editor.
  • click on File→ New→  Java project
  • Give a project name. For Example JavaPractice
  • Right click on the project that you just created. Click on new→Class
  • Write the above code snippet on Eclipse editor.
  • Click on Run. (highlighted in yellow in below screenshot)
  • You will see the output: My first program in java

java first program by tutorials in hand

 

 

Compile & Run on Notepad:

Follow the below given steps,

1.      First open a Notepad and write the above program (HelloJava) on it. 

2.      Save the program with the name HelloJava.java (Save the source file name with same name as java program. Learn why?)

3.      Open the command prompt window and move to the directory where you saved the program and type,

To compile→ javac HelloJava.java

To run→ java HelloJava

4.      On completing the above steps you will be able to see the output as shown above.

 

It’s time to find out significance of few terms used in our above program.
 

public static void main(String[] args)

  • class :  Every java program starts with a class. Class keyword is used to declare a class in java. We will learn more in OOP section of this tutorial.
  • public : public is access modifier. Class defined as public indicates that it can be accessed from any other class. In java we have other modifiers as well like private, protected, default. We will learn more in Access modifier section of this tutorial.
  • static : When the program is executed then JVM first search for the main method. If method is declared static, then JVM can access this method without creating instance of the class.
  • void : void is the return type. It means that main method will return nothing.
  • main() : main is the name of the method. main() serves as the entry point for any java program. Your class may or may not have main() method. Whenever you execute a program, JVM search for this class having main() method with type signature ‘public static void main(String[] args)’. 
  • String[ ] args : It helps java program to accept command line arguments in String format.
  • System.out.print( ): For now, just remember that this is used to print statement as we did in our program. We will dive deeper into it later.
 


Please Share this page
Views : 220
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!