Sunday 29 July 2012

WAP to add two numbers by just assigning values(J2SE).

Adding two numbers
J2SE Programming

Now I am going to write a program which adds two numbers.
For this i just want to know about addition operator and two inputs.
As we know, there is one operator known as plus(+) operator can be used to add two inputs.
Now the problem is how to get input for adding numbers.

 There are three ways to get input as follows:-
  1. One way is just by assigning values.
  2. Second way is by User input during Run-time.
  3. Third way is using Command-Line arguments.
Here we are using First way i.e. by Assigning values.
Other ways will be discussed Later.

                               class Addition
                              {
                                          //Main Function
                                       public static void main(String[] args)
                                      {
                                          Addition a1=new Addition();
                                          int a=a1.add(10,20);
                                          System.out.println(Sum is : +a);
                                      }

                               //Function to add two numbers

                                       public int add(int a,int b)
                                      {

                                         int c=0;
                                         c=a+b;
                                         return c;

                                      }
                              }


Now save it as Addition.java
Compile it using "javac Addition.java" Command in CMD
After compilation Run it using command "java Addition" in CMD

Saturday 28 July 2012

Printing Hello World In Java(J2SE)

J2SE Programming

Here now we go for our first program i.e. printing hello world in java.

class HelloWorld
{
        public static void main(String[] args)
        {
           System.out.println("Hello World");
        }
}

Now compile it as show in fig.
Firstly you have to move into the directory where the file is saved(use cd to move between Directories )


Print hello world in java

Once you have compiled it then Run it as Shown in fig below

Print hello world in java


Wednesday 25 July 2012

Starting Java(J2SE) Programming

Start Java Programming
J2SE Programming

As we know there are three types of programming we can do in Java. One is Console Based, second is Window Based and third one is Web-Based.
We start firstly with Console based.

Before Actual working, I tell some syntax used in java
Class syntax :    class <class_name>
                            {
                              //code to do
                           }
e.g.
      class abc
      {
        //code
      }

Method Declaration :  access_specifier return_type <function_name>(Argument List)
                                    {
                                      //Code to do
                                    }
e.g.
      public void doStuff()
      {
       System.out.println("DoStuff");
      }

Variable Declaration : type <variable_name>;

e.g. int a;

Variable Intialization : name=<variable_value>;

e.g. a=10;

Object Creation : type <reference_name>=new type(Arg List);

abc a1=new abc();

How java(J2SE) compiler works?

J2SE Programming

Here am going to tell you how java compiler works i.e. what is the actual working of java compiler.

If you write a program in java then it is both compiled and interpreted. The aim of the both compilation and interpretion is just to make program made by you more secure and making it portable i.e. platform independent.

Java Compiler Working

Now What actually happen when you compile a java program:-
  1. Firstly it is compiled for any error like Syntax error,etc.
  2. Once it is compiled, code is generated by the compiler known as BYTE Code which is some binary code can't be understandable by the Humans.The file generated by compiler has a extension of .class
  3. Like if you compile a file named First.java, then compiler generates a file named First.class which can be viewed using notepad but can't be understanded.
  4. Once the byte code is generated, it can be run on any platform.Here run means Interpreting the byte code and providing the appropriate output.
  5. There is one another thing in java which makes it more efficient i.e JIT(Just In Time Compiler) which is a part of JVM( Java Virtual Machine) which converts only necessary code to executable code              e.g. if then is an if condition which is already known that else part will be executed then JIT converts only that else part to the executable code but not the if part. JIT Comes in action during Run Time.
  6. After that the appropriate output is shown on the screen.

Monday 23 July 2012

Java(J2SE) Intro

Java Introduction
JAVA INTRO


 J2SE Programming

Java , a programming language which is fully based on oops concept was developed by James Gosling in 1992 at Sun Microsystem.It was initially called as OAK.
Java is accepted as a core component of the organisation in 1995
Syntax used in java are much similar to used in C and C++.

Java is both Compiled and interpreted which is the best feature of it to make it secure.




It is an platform independent programming language in which following work can be carried out:-

  • Console Based applications
  • Window Based Applications
  • Web Based Applications

Features In Java:-


  • Simple
  • Object Oriented language.
  • Robust
  • Secure Language
  • Easy-to-understand
  • Multi-threded
  • Interpreted
  • Distributed
  • Dynamic
  • Portable