Java


                                  Java Classification

                             Java has its parts named Core Java,Enterprising Java,Micro Java. Core Java is useful for developing Desktop applications. Enterprising Java is useful for developing Web Applications. Micro Java is useful developing Embedded Applications.
                              A knowledge of Core Java is essential for working in Java field.


    The primitive data types in Java are int,float,char.Java's basic programming structure is,
In the program below int is the data type, 'a' is a variable.

                                     Basic Java Programs

1)class FirstExample
{
  public static void main(String args[])          
  {
    int a=10;
    System.out.println(a);
  }
}
  -------------------------------- program ends---------------------------------------
Output:10
Where public static void main(String args[]) is nothing but main function where the compilation starts.
Note:Everything should be enclosed inside the class even the main function unlike C++.The class which encloses the main function is called main class.
System.out.println(a)-prints a which contains the value 10.
 ---------------------------------program starts---------------------------------------------------------
2)class SecondExample
   {
     public static void main(String args[]) 
     {
       int a=10;
       int b=20;
       System.out.println(a+b);
     }
   }
--------------------------------program  ends---------------------------------------------------
Output:30
In the above example we have performed the addition operation.
For subtraction,System.out.println(a-b);
For multiplication,System.out.println(a*b);
For division,System.out.println(a/b);

                                    Programs Using Control Structures

Java consists of control structures such as if,if else,switch.
-----------------------------------------program starts--------------------------------------
 class IfCS
{
 public static void main(String args[])
 {
  int x=1;
  if(x==1)
  {
   System.out.println("True");
  }
 }
}
--------------------------------------program ends-------------------------
Output:  True
------------------------------------------program starts----------------------
class IfelseCS
{
 public static void main(String args[])
 {
  int i=2;
  if(i==1)
  {
   System.out.println("True");
  }
  else
  {
   System.out.println("False");
  }
 }
}
-------------------------------------------program ends---------------------------------
Output:False
-------------------------------------------program starts---------------------------------
 class Nestedif
{
 public static void main(String args[])
 {
  int i=1,j=2;
  if(i= =1)
  {
   if(j= =2)
   {
    System.out.println("True");
   }
  }
 }
}
---------------------------------------------------------program ends----------------------------
Output:True
--------------------------------------------------------program starts----------------------------
class Nestedifelse
{
 public static void main(String args[])
 {
  int i=1,j=2;
  if(i= =1)
  {
   if(j= =1)
   {
    System.out.println("True");
   }
   else
   {
    System.out.println("False");
   }
  }
 }
}
--------------------------------------program ends-----------------------------------------------
 Output:False
--------------------------------------program starts-----------------------------------------------
class SwitchCS
{
 public static void main(String args[])
 {
  int i=3;
  switch(i)
  {
   case 1:System.out.println("True");
          break;
   case 2:System.out.println("False");
          break;
   case 3:System.out.println("Not Predictable");
          break;
  }
 }
}
-------------------------------------------------program ends-----------------------------------------
Output:Not Predictable
Switch cases are nothing but multiple branch statements.It is used whenever multiple conditions are to be checked.

                                    Programs Using Loops

In Java there are three types of loops(for,while,do while).
--------------------------------------program starts-----------------------------------------
class ForLoop
{
  public static void main(String args[])
  {
    for(int i=0;i<=5;i++)
   {
     System.out.println(i);
    }
   }
  }
-----------------------------------------program ends------------------------------------------
Output: 0
            1
            2
            3
            4
            5
----------------------------------------program starts------------------------------------------
class WhileLoop
{
  public static void main(String args[])
  {
    int i=0;
    while(i<=5)
    {
      System.out.println(i);
       i++;
     }
   }
 }
----------------------------------------program ends--------------------------------------------------
Output: 0
            1
            2
            3
            4
            5
----------------------------------------program starts-------------------------------------------------
class DoWhileLoop
{
 public static void main(String args[])
 {
  int i=0;
  do
  {
   System.out.println(i);
   i++;
  }while(i<=5);
 }
}
------------------------------------------program ends----------------------------------------------------
Output:0
           1
           2
           3
           4
           5

                                    Programs Using Arrays

3)class ArrayExample1
   {
      public static void main(String args[])
      {
         int x[]=new int[3];
         x[0]=2;
         x[1]=4;
         x[2]=6;
         for(int i=0;i<3;i++)
            System.out.println(x[i]);
      }
   }

--------------------------------------------program ends----------------------------------------------
In the above example we have used array.Arrays contains elements of same type.
Output:2
           4
           6

------------------------------------------------program starts-------------------------------------------

4)class ArrayExample2
  { 
    public static void main(String args[])
    {
      int x[]={10,20,30};
      for(int i=0;i<3;i++)
      {
         System.out.println(x[i]);
       }
     }
   }
-------------------------------------------------------program ends----------------------------------------
Output:10
            20
            30
The Array can also be declared by this method.

                                         Programs using Functions

                  In Java  we cannot write a function inside a function.The calling of function inside a function is allowed whereas the definition of a function inside a function is not allowed.
--------------------------------------------------program starts--------------------------------------------
           
4)class FunctionExample1
   {
     static int a=10,b=20;
     public static void add()
     {
        System.out.println(a+b);
     }
     public static void main(String args[])
     {
       add();
     }
   }
--------------------------------------------------------program ends--------------------------------------
Output:30
In the above method we have used a function with no parameters.
----------------------------------------------program starts------------------------------------------------

 5)class FunctionExample2
    {
        static int a,b;
        public static void add(int x,int y)
        {
           a=x;
           b=y;
           System.out.println(a+b);
        }
         public static void main(String args[])
        {
           add(5,2);
         }
       }

----------------------------------------------program ends-------------------------------------------------
Output: 7
In the above example we have used a function with parameters.
-------------------------------------------------program starts---------------------------------------------
6)class FunctionExample3
  {
    static int a,b,c;
    public static void add(int a,int b)
   {
     c=a+b;
    display();
   }
   public static void display()
   {
    System.out.println(c);
   }
   public static void main(String args[])
   {
    add(5,2);
   }
  }

---------------------------------------------program ends------------------------------------------

Output: 7

Note:In the above program the function display is called inside the function add but it is defined outside the function add.
 
The reason why I have used static variable and method is because static methods can only refer to static variables and it cannot refer to non-static variables and hence the variables a,b and c are static.

                                     Program Using Classes and Objects

A Class is a binding of data(variables)and functions together.Objects are instances(copy) of a class.

Example:

class AutoMobile
  int rpm,torque,topspeed;
  public void move();
  public void stop();
  public static void main(String args[])
  {
    AutoMobile bike=new AutoMobile(); 
    AutoMobile car=new AutoMobile();
    AutoMobile bus=new AutoMobile();
   }
}
Here AutoMobile is the class and the objects are bike,car,bus which share the same properties.The objects 
bike,car,bus have properties rpm,torque and topspeed but their values differ for each object over each property. An Object can access the class properties only through the use of dot(.)operator.
-----------------------------program starts----------------------------------------------------

class Class1
{
 int x,y;
 public void add()
 {
  x=10;
  y=20;
  System.out.println(x+y);
 }
}
class Main1
{
 public static void main(String args[])
 {
  Class1 c1=new Class1();
  c1.add();
 }
}
-------------------------------------program ends-----------------------------------------------
Output:30
Here Class1 c1=new Class1() makes c1 as an object of  Class1.
-------------------------------------program starts-----------------------------------------------

class Class2
{
 int x,y;
 public void add(int a,int b)
 {
  x=a;
  y=b;
  System.out.println(x+y);
 }
}
class Main2
{
 public static void main(String args[])
 {
  Class2 c2=new Class2();
  c2.add(10,20);
 }
}
----------------------------------program ends--------------------------------------------------
Output:30

 Constructor:

Constructor is used to initialize values.It has no return type.Constructor is not invoked(not called) unlike functions.Constructor has the name as the class name.
----------------------------------program starts--------------------------------------------------
 class Class3
{
 int x,y;
 Class3()
 {
  System.out.println("Default Constructor");
 }
 Class3(int a,int b)
 {
  System.out.println("Parametrized Constructor"+" "+a+" "+b);
 }
}
class Main3
{
 public static void main(String args[])
 {
  Class3 c31=new Class3();
  Class3 c32=new Class3(10,20);
 }
}
----------------------------------program ends-----------------------------------------------------

Passing Objects as Parameter

Like passing integer values as parameters we can also pass objects as parameters.
-----------------------------------------program starts----------------------------------
class Class4
{
  int x,y;
  public void assign(int a,int b)
 {
   x=a;
   y=b;
 }
 public void assign(Class4 c4)
 {
   x=c4.x;
   y=c4.y;
 }   
public void display() 
 {
 System.out.println(x+" "+y);
 }
}
class Main4
{
 public static void main(String args[])
 {
  Class4 c41=new Class4();
  Class4 c43=new Class4();
  c41.assign(10,20);
  c43.assign(c41);
  c41.display();
  c43.display();
 }
}--------------------------------------program ends-------------------------------------------------
Output:
10 20
10 20
In the above program we have used functions to pass objects as parameters.We can also use constructors to pass objects as parameters.
----------------------------------------------program starts-------------------------------------------
class Class5
{
 int x,y;
 Class5()
 {
  x=0;
  y=0;
 }
 Class5(int a,int b)
 {
  x=a;
  y=b;
 }
 Class5(Class5 c5)
 {
  x=c5.x;
  y=c5.y;
 }
 void display()
 {
  System.out.println(x+" "+y);
 }
}
class Main5
{
 public static void main(String args[])
 {
  Class5 c51=new Class5(10,20);
  Class5 c53=new Class5(c51);
  c51.display();
  c53.display();
 }
}
---------------------------------program ends---------------------------------------------
Output:10 20
           10 20

Return type

 We can also return values by specifying its type.If the function specifies void type then it means that the function does not return a value.

------------------------------program starts----------------------------------------------------
class Class6
{
 int x=10;
 float y=20;
 double z=30;
 public void get1()
 {
  System.out.println("Does not return any value.Just able to print");
 }
 public int get2()
 {
  return x;
 }
 public float get3()
 {
  return y;
 }
 public double get4()
 {
  return z;
 }
}
class Main6
{
 public static void main(String args[])
 {
  int a;
  float b;
  double c;
  Class6 c6=new Class6();
  c6.get1();
  a=c6.get2();
  b=c6.get3();
  c=c6.get4();
  System.out.println(a);
  System.out.println(b);
  System.out.println(c);
 }
}
------------------------------program ends-----------------------------------------------------
Output:

Does not return any value.Just able to print
10
20.0
30.0
The reason why we dealt about returning values is that it is the basic concept which makes us to understand the concept of how to return an object

Returning Objects

------------------------------program starts---------------------------------------------------------

class Class7
{
 int x;
 public void get1(int a)
 {
  x=a;
 }
 public Class7 get2(Class7 c7)
 {
  return c7;
 }
 public void display()
 {
  System.out.println(x);
 }
}
class Main7
{
 public static void main(String args[])
 {
  Class7 c71=new Class7();
  c71.get1(10);
  Class7 c73=c71.get2(c71);
  c71.display();
  c73.display();
 }
}
----------------------------program ends--------------------------------------------------------
Output:10
           10







Comments