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.

programming

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:

a=10;

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 :

a=10*4+b;

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

a=a+1;

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

So for example :

 a=1+10;
 b=3*4;
 c=5-4;   
and so on.

asgnmnt

expression

First program Of Saying Hello :

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

 1. HelloWorld.java


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 HelloWorld.java. Then, at the command line, execute the following command to compile this source file:

javac HelloWorld.java

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

HelloWorld.class contains the executable equivalent of HelloWorld.java. 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.

2.  HelloWorld.java
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 HelloWorld.java) 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