Simple Program of Java

In this tutorial, You'll learn about the simple program of java programming. This tutorial covers java basic syntax, we'll show you run java program from the terminal.   

Simple Program of Java

Here I shared the simple program of java and then explain the internal details of hello java program.

import java.lang.*;
class Simple{
public static void main(String args[]){
   System.out.println("HelloWorld"); 
   }
}
Output:

   C:\Users\codeamy>javac Simple.java
   C:\Users\codeamy>java Simple
   HelloWorld

In this, we will see the internal details of simple hello world java program. You have to write one java program if you are new to java then take help from our previous tutorial of the java hello world program. 

Java Hello World Progam

You can use any text editor to write your program like windows - Notepad, Sublime Text, Atom, Brackets, etc., and for Linux - Gedit, Vim, etc. 

What will happen at Compile Time with Java Program

When you try to compile the .java file with the java compiler then it converts into a file that is not independent of operating system and this file is called bytecode. 
You can find the Bytecode filename with .class extension. Always Bytecode filename matches with your main class name.

The below image explains for compile time 

Java compiler


What happens at Runtime

When your code is successfully compiled. You can run this bytecode file in any OS it will show the same output. Every device has its own JVM preinstalled it helps to run the bytecode and interpret the file.  
Java code at runtime


Run Java Program in Terminal

To run the java program successfully. You have to follow 
  1. Open Command Prompt(cmd.exe).
  2. Go to the location where you save your java source code file.
  3. Type javac command to compile your java file. 
  4. To execute your type java to execute .class file. 
Run Java Program in Terminal

Note: Save your text file with .java extension and Your name of java file must be same as the class name.

A class name first letter always starts with Uppercase. These are some rules to write java code. 

Java is case sensitive language, i.e. Codeamy and codeamy both are different

What will happen if you can save your java source code file with any other name, not as class name. 

No, issue if you save your filename differently with the class name. Then to compile you use filename.java but for executing use the main classname.

  1. To compile java code:       javac Simple.java 
  2. To execute compile code:  java Helloworld 
different java file name than class name

No comments:

If you have any doubts, Please let me know

Powered by Blogger.