Simple Java Programs With Output and The Ultimate Guide

Code :

java code

Write a lot of Java commands to make an application work – that is you have to create some “code”. The beginner using a modern approach to creating a user interface via the drag-and-drop designer can avoid writing much code for a long time , but there comes a time when you have to do more.

Creating code that does something is the traditional core of the programming skill.

What A Program is ?

A program is a list of instructions. The computer simply reads the list of instructions in the order that you present them and obeys each one in turn.


Variables, assignment and expressions

A variable is roughly speaking a chunk of storage that you have given a name to. What you can store there depends on what you specify when you create the variable.

For example, in Java language you can store an integer value in an int variable.

So, for example

int a;
creates a variable called a that you can store an integer in.

A variable can only store one value at a time so if you store another value in it then the anything already stored is lost.

For example:


stores 10 in a and any record of the  previously stored is lost.

You can use a variable on both sides of an assignment.

For example :   a=b;

this retrieves the value stored in b and stores it in a.

You can also write things like :


An arithmetic expression that causes a lot of problems when you first see it is:


The most basic unit of a Java program is a single statement and every Java statement ends with a semicolon.

So for example :

and so on.



First program Of Saying Hello :

It’s traditional to introduce a computer language by presenting a program hello,world message.


class HelloWorld
   public static void main(String[] args)
      System.out.println("hello, world");

The application’s class is named HelloWorld. Its main() method executes System.out.println("hello, world"); to send the contents of “hello, world” string to the standard output stream,which is typically the command line.

Store 1 in a file named Then, at the command line, execute the following command to compile this source file:


javac requires the .java file extension; otherwise, it generates an error message.

HelloWorld.class contains the executable equivalent of To run this class file via the java application launcher tool, execute the following command:

java HelloWorld

Java doesn’t permit you to include the .class file extension; if you do so it will generate an error message.

Assuming you’ve written your program correctly, you should observe the following output:

hello, world Personalizing hello :

We can improve on  1 by personalizing the application. For example, you might want to output hello, Java instead of hello, world.  2 shows this enhancement of the original program.

class HelloWorld
public static void main(String[] args)
System.out.println(“hello, ” + args[0]);

2 shortens "hello, world" to "hello, " and appends + args[0] to join the args array’s first string to the message. The result is then output.

hello, " + args[0] is an expression that appends the string in the first element of the args array to hello.

Compile  2 (javac and run the application with a single command-line argument, as follows:

java HelloWorld Java

You should observe the following output:

hello, Java Screenshot from 2016-05-02 16:56:11 Addition of Two Number : class Addtwonum{     public static void main(String args[])     {     int x,y,z;     x=10;     y=20;     z=x+y;     System.out.println("Addition of X and Y="+z); } } Output :- addnum