Simple Java Application

Simple Java Application


Learning Java is simple. It is preferred to know C-lang earlier. Knowledge of C++ is not essential as same OOPS concepts we learn with Java syntax. This Simple Java Application gives you step-wise writing source code, compilation and execution.

Note: Before coming to know this tutorial, you are expected to know Java features, Java keywords and Java naming conventions.

Following is your first Simple Java Application that prints Hello World

Simple Java ApplicationOutput Screenshot of Demo.java on Simple Java Application

Let us see each step from writing source code to getting output.

A. Writing source code

I take it granted you loaded JDK software and set the path in Environment Variables. Now open command-prompt and open notepad and write the following command. Say example,

C:\snr> notepad Demo.java

Demo.java is the file name. The extension of of a Java file should be .java like for a C-prog it is .c. The above statement opens a notepad and write the above source code as it is.

B. Compilation

After writing source code, let us compile.

C:\snr> javac Demo.java

javac stands for Java compiler. It is an instruction to compiler to compile Java file Demo.java. When compiled successfully, the compiler generates an executable file Demo.class.

C. Execution

After compilation, let us execute.

C:\snr> java Demo

The above command executes Demo.class and gets output as it the above screenshot.

Let us discuss briefly each line of code.

import java.lang.*;

import keyword is equivalent to include of C-lang. java.lang is a package equivalent to header file of C-lang, but with a difference. A header file contains a set of related predefined functions where as package contains related predefined classes and interfaces. With the above statement, all the classes and interfaces of java.lang package are imported for our Java program. Such classes are String and System used in the above program.

public class Demo

The whole code you write should be included within the class declaration; between open and close braces of the class. For this reason, main() method is also enclosed within the class. public is an access specifier that means the Demo.java code can be used by any other class without any restrictions. class is another keyword of Java which gives the name to the code as Demo.

public static void main(String args[])

static is known as access modifier that means the main() method can be called without the help of an object by JVM. JVM stands for Java Virtual Machine, the component of JDK responsible for execution of Java code. void keyword means, the main() method does not return a value. The main() method takes a string array by name args as parameter that can be used later to access command-line arguments.

System.out.println(“Hello World”);
System.out.println(“Best Wishes”);
System.out.println(“I want to prove Java is Simple”);

System.out.println() is the same as printf() of C-lang which prints at command-prompt. In Java, for each line \n is inbuilt. In println(), ln stands for new line character \n.

Following links give simple Java applications to start with learning Java in the same order. It is advised to talk to your Java friends whenever there is a bottleneck in understanding or write to me by filling the text box “Leave a Reply” given at each lesson end.

1. Java Example Compilation Execution
2. Using Local and Instance Variables
3. Data Binding Data Hiding Encapsulation
4. Using Variables from Methods
5. Unassigned Local and Instance Variables
6. static Keyword

Leave a Reply

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