Pages

Saturday, February 17, 2018

Writing first Java program

Hello Techie, This post is about steps to write, compile and execute our first java program.

  • Pre-requisite:
    • Basic knowledge of computer system.
    • To compile and execute java program, we need java environment setup on a system.
    • Once the Java environment setup is complete we can proceed with creating our first java program.


  • Tools required:

    • Any text editor like notepad, notepad++, EditPlus etc. In this post, we will use notepad++ to write a java program.


  • Steps:
    • Open notepad++ and write following code snippet :
      • Let's understand the above code line by line
        • Line 1: Declaration of java class name. Keyword class is to declare the class name.
        • Line 2: Declaration of main() method. This method is the entry point for program execution.
        • Line 3: This will print the string (in this case "This is my first java program !!!") in the console.
        • Line 4: In Java programming language, curly brackets are "{ }" used to define start and end (scope) of the class, method, constructor etc.
    • Save the file with the name "FirstJavaProgram.java" in any folder by clicking File >> Save As... (or use shortcut Ctrl+s). Important to know that, the name of the java file and the class name should be same and file to be saved with an extension ".java".
Write first program in java
Write Code Snippet

Save File

Write File Name

Saved File
    • Open the command prompt and go to the folder where you have to save/stored java file in the previous step.
    • Type following command 
      • javac FirstJavaProgram.java
        
        • javac command is used to compile the java program. The output from a Java compiler is java class file containing platform-neutral java bytecode.
        • In nontechnical terms, java compile convert the human-readable file into machine-readable file.
        java FirstJavaProgram
        
        • java command is used to execute the java program. Inside JVM, Interpreter read and interpret the java bytecode and execute the java program line by line.
        • Once this command executed we will get program output on the console.


Program Output

Class file


Post comment if any issue occurred while creating and executing the java program. 

No comments:

Post a Comment