Showing posts with label CORE JAVA. Show all posts
Showing posts with label CORE JAVA. Show all posts

Friday, 15 April 2016

THREAD

               Well on this time we are going to discuss about thread in java. Thread is a light weight sub process, a smallest unit of processing. Thread uses the multi-threading to run multiple threads simultaneously in the program, Multi-threading uses the common memory area for all the process.

                 In multitasking there are two types of thread they are,

                                                                   *   Process-based
                                                                   *   Thread-Based

PROCESS BASED:

                 It is a heavy-weight sub process, it uses the separate memory area for each process and it takes time while switching from one process to another process.

THREAD BASED:

                 It is a light-weight subprocess, it uses the signal memory area for all the process and it takes the less time for switching from one process to another process

                  In java there are five states of thread , but according process, Sun there is four states they are new , Runnable , non-runnable and terminate.

                  New In the beginning thread is in new state if you create an instance of thread class but before the invocation of start() method

Runnable-> The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

Running-> The thread is in running state if the thread scheduler has selected it.

Non-Runnable-> This is the state when the thread is still alive , but is currently not eligible to run,

Terminated-> A thread is in terminated or dead state when its run() method exits.

MAIN THREAD:

                 In java it contain the main thread on an main method.There are many thread methods are used in java they are start(), sleep(), run(), join(), wait(), isAlive, getpriority(), gtcurrentthread(), etc.

                  There are two ways to access a thread in java they are 
                                                  *   By implementing the Runnable
                                                  *  By extending the Thread

Implementing the runnable:

                 While implementing the runnableThreadExa implements 

                                              Runnable{
                                             @override
                                              public void run()
                                           {
                                        //TODD AUTO-GENERATED METHOD STUB
                                                        for(int i=5;i>0;i--)
                                                  {
                                          try{
                                                          Thread.sleep(500);
                                                          System.out.println(i);
                                                   }
                                                      catch(InterruptException e)
                                             {
                                            //TODD AUTO-GENERATED CATCH BLOCK
                                                       e.printStackTrace();
                                                   }
                                                 }
                                                      }
                             public static void main(String args[])throws InterruptedException
                                                   {
                                              RunnableThreadExa runn=new RunnableThreadEXa();
                                                         Thread t=new thread(runn);
                                                         Thread t1=new thread(runn);
                                                         Thread t2=new thread(runn);
                                                         system.out.println(t.getName());
                                                               t.setname(jarvis);
                                                               t.start();
                                                                t.start();
                                                                t.join();
                                                                  }
                                                                   }

Extending the thread:


                                            public class extendThread extends thread
                                             {
                                                      pubic void run()
                                            {
                                            //TODD AUTO-GENERATED METHOD STUB
                                                       for(int i=5;i>0;i++)
                                            {
                                                           try{
                                                          sleep(500);
                                                        }

                                                        catch (InreruptsException e){
                                            //TODD AUTO-GENERATED CATCH BLOCK 
                                                       e.intracellular();
                                                              }
                                                          }
                                                             }
                                                    public static void main(String args[])
                                                                               throws exception
                                                       {
                                                          thread t=new thread();
                                                          thread t1=new thread();
                                                        system.out.println(t.getName);
                                                            t.setName(jarvis);
                                                            t.start();
                                                             t.start();
                                                             t.join();
                                                                   }
                                                                        } 

That's all for today guys we have seen threads and we ill see other topic in next session

Thursday, 14 April 2016

EXCEPTION

              Hi guys today we are going to see the advanced topic on JAVA , we can see what makes exception in java,

              An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run-time error. Exception handling is a mechanism to handle runtime errors. The main advantage of exception is that a normal flow of the application is maintained.

Exception Handling Fundamentals
               Java Exception handling is handled by five keywords They are,
                                                            *  try
                                                            *  catch
                                                            *  throw
                                                            *  throws
                                                            *  finally
               Program statements that you want to monitor for exceptions are contained within a try block. If an exceptions are contained within a try block , it is thrown. Your code can catch this exception using catch and handle it in rational manner. System-generated exception are automatically thrown by the java-time system.To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clauses. Any code that absolutely must be executed after a try block completes is put in a finally block. This is the general form of an exception-handling block.

try
{
//block of code to monitor for errors
}
catch(ExceptionType1 exob)
{
//exception handler for exception type1
}
catch(Exception Type2 exob)
{
// exception handler for exceptionType2
}
//....
finally
{
//block of code to be executed after try block ends
}
              Here,ExceptionType is the type of exception that has occurred

Type of Exception:

               There are mainly two types of exception: checked and unchecked where error is considered as unchecked exception. The sun microsystem says there are three types of exception
                                                           *  Checked Exception
                                                           *  Unchecked Exception
                                                           *  Error

CHECKED EXCEPTION:

                      This classes that extend throwable class except RunTimException and error are known as checked exceptions (e.g) IOException, SQLExcepion etc. Checked exceptions are checked at compile-time.

UNCHECKED EXCEPTION:

                       This classes that extend RunTimeException are known as unchecked exception e.g ArithmeticException, NullPointerException, ArrayIndexOutofBoundsException etc. Unchecked exception are not checked at compiled time rather they are checked at runtime

ERROR:

                   Error is irrecoverable e.g OutOfMemoryError, VirtualMachineError, AsserionError etc. using try and catch block to handle runtime error, enclose the code that you want to monitor inside a try block . Immediately after the try block, include a catch clause that specifies the exception type that you wish to catch. The followinf program includes try block and a catch clause that processes the arithmeticException henerated by the division by zero error. Once an exception is thrownm program control transfers out of the try block into the catch block.

Example:
class Exc1
{
public static void main(String args[])
{
system.out.println(using try and catch block);
int d,a;
try //monitor a block of code.
{
d=0;
a=42/d;
system.out.println(This will not be printed);
}
catch(ArithmeticException e)
//catch divide-by-zero
error
{
system.out.println(division by zero);
}
system.out.println(after catch statements);
}
}

MULTIPLE CATCH BLOCK:

              In some cases, more than one exception could be raised by a single piece of code. To handle this type of situation, we can use two or more catch clauses, each catching a different type of exception. When an exception is thrown , each catch statement is inspected in order, and the first one whose type matches that of the exception is executed. After one catch statement executes the others are bypassed, and execution continues after the try/catch block.

THROW KEYWORD:

              The keyword is used to explictily throw an exception.We can throw either checked or unchecked exception.The general form is.
throw Throwableinstance;

                Throwableinstance must be an object of type Throwable or a subclass of throwable. There are two ways you can obtain a throwable object: using a parameter in a catch clause, or creating one with the new operator. The flow of execption stops immeditely after the throw statements; any subsequent statements are not executed.

Example:
 class throwdemo
{
staticvoid demproc()
{
try
{
throw new NullpointerExcepion(demo);
}
catch(NullpinterException e)
{
system.out.println(caught inside demoproc.);
throw e; //rethrow the exception
}
}
public static void main (Static args[])
{
system.out.println(Throw example);
try
{
demoproc();
}
catch(Nullpointereception e)
{
system.out.println(Recaught +e);
}
}
}

New is used to construct an instance of NullPointerException . Many of java's built in run times exceptions

That's all for today guys this is exception and this is in java... see you later in next class guys

Wednesday, 13 April 2016

KEYWORDS IN JAVA

                                       KEYWORDS


            In java there are more than 42 keywords and still finding, Here I'll get you through the main three concepts on keywords,
                                                               *   This
                                                               *   Super
                                                               *   Static

THIS KEYWORD:

           This is the keyword used to refer the current class objects in java, It invoke the current class constructor , current class methods and the current class variable. This is used to specify the variable when the compiler got confused , 
             
            For example if there are 3 users called A, B, C, The user A and B simultaneously give the data to C and C gets confused that which data from the users. In this situation we use THIS keyword to specify the person.

            There are three type of THIS keyword is used they are 

          "   this()- It is used to invoke the current class constructor,
              this.a()- It is used to invoke the current class method,
              this.a - It is used to invoke the current class variable.  "

           Example:

                                                Public class ThisEx
                                                   {
                                                      int a=5;
                                                   ThisEx()
                                                   {
                                                    }
                                                     ThisEx(int a)
                                                    {
                                                     }
                                                      void a()
                                                   {
                                                          }
                                                   void b()
                                                      {
                                                       this.a();
                                                       system.out.println(this.a);
                                                          }
                                                        public static void main (String args[])
                                                    {
                                                       ThisEx obj=new ThisEx(5);
                                                            obj.b();
                                                          }
                                                              }

             By using THIS keyword it have certain rules. While invoking this() that invocation should be inside the constructor .While invoke the this.a() that invocation should be inside the method. While invoke the this.a that invocation should be inside the statement. 

SUPER KEYWORD:

            Super keyword is used to refer the super class object. It is used to invoke the super class constructor, super class methods, super class variables.

            Super is used to get the specific content from the super class, There are three types of super method are used they are,

                  "  Super()-- It is used to invoke the super class CONSTRUCTOR.
                    Super.a()-It is used to invoke the super class METHOD.
                    Super.a-- It is used to invoke the super class VARIABLE   "


          Example:

                                                          Class super1
                                                        {
                                                          int a=5;
                                                          super1()
                                                       {
                                                          system.out.println(super class constructor);
                                                          }
                                                         void.a()
                                                         {
                                                         system.out.println(super class method);
                                                         }
                                                              }
                                                         public class SuperEx extends Super1 
                                                          {

                                                             SuperEx()
                                                         {
                                                               super();
                                                            }
                                                               void b()
                                                         {
                                                               super.a();
                                                              system.out.println(super.a);
                                                            }
                                                               public static void main(String args[])
                                                          {
                                                                superEx obj=new SuperEx();
                                                                 obj.b();
                                                              }
                                                                 }

            By using the super keyword it have certain rules. While invoke the super() that invocation  should be inside the constructor.While invoke the super.a() that invocation should be inside the method. While invoke the this.a that invocation should be inside the statement.

STATIC KEYWORD:

            Static keyword is used to run the static content first. While declaring the static method and static variable in the program, without the help of the object it is possible to call the methods and variable inside the main method.
             By defining the static method it uses only the static variable

      Example:
 
                                                      public class Example
                                                 {
                                                      static int a=5;
                                                      static void a()
                                                 {
                                                       system.out.println(a);
                                                   }
                                                       public static void main(String args[])
                                                 {
                                                       a();
                                                   }
                                                 }
Well here we seen the full indeapth concept of the KEYWORDS in Java, with some practical programs, try it in your environment and learn this easily  . Do comments for better up-gradation and further post

                                            COMMENTS PLS................... 

INTERFACE

            Hi guys today we are going to see some of the indeapth concept of Java, That is Interface , This are used to overcome the multiple inheritance in java. Interface can be access by using the implementation keyword in java.
           
           By default inside interface all methods are abstract and all variables are final. Interface does not have objects . The method which declare inside the interface must be defined by the implemented class.

           Syntax:

                                                  interface interface_name
                                                       {
                                                           }

          This is the basic syntax for interface

          Example:

                                                     interface inter1
                                                 {
                                                     void a();
                                                     }
                                                    interface inter2
                                              {
                                                 void b();
                                                   }
                                            public class inerfaceClassA implements iner1, iner2
                                                  {
                                                          @override 

                                                             public void b();
                                                         {
                                                  //TODO Auto-generated method stub
                                                   system.out.println(interface 1 value);
                                                               }
                                                    @override

                                                         public void a();
                                                           {
                                                           //TODO Auto-generated method stub
                                                            system.out.println(interface 2 value);
                                                               }
                                                               public static void main(String args[])
                                                            {
                                                             interfaceClassA obj=new interfaceClassA();
                                                                           obj.a();
                                                                           obj.b();
                                                                        }
                                                                      }

            In java one interface can be extended to another interface.

                                                                      interface inter1
                                                                 {
                                                                      void a();
                                                                     }
                                                                     interface inter2 extends inter1
                                                                   {
                                                                       void b();
                                                                      }
             Then both the methods must define in the implemented class.

            This is the basic definition of what interface actually does, Now from this we move into the next concept , its about keywords

           Please do comments for my post and let me know about how you like this java tutorial.. and its also be helpful for me to get updated for further post....

                                                               COMMENTS PLS.............
 

Tuesday, 12 April 2016

ENCAPSULATION & ABSTRACTS

               Encapsulation is nothing about wrapping or binding up of data and method into a single unit.This reduces the complexity of the program with the help of method.It is one of the object oriented concept.

               It contain the method and the method acts as the capsule that perform the operation whenever needed. The following will describe about the encapsulation.
      
           Example:
                                                   public class Tony{
                                                    void a()
                                                   {
                                                       system.out.println(hai);
                                                     }
                                                     int n(int e)
                                                 {
                                                     system.out.println(e);
                                                      return 0;
                                                  }
                                             public static void main (String args[])
                                               {
                                                 Tony obj=new Tony();
                                                         obj.a();
                                                         obj.n(5);
                                                }
                                             }

                 The above example contain the methods, The methods contain the statement and data here we are binding statements and data into a single unit and sends to the main method so this is called the ENCAPSULATION.

ABSTRACTION:

                    Abstraction is the process of hiding the implementation details and showing only the functionality details for example while programming you are showing only the output to the user not your but the abstract keyword.

                    Abstract is like an index of the page, if the index is refereed then it is easy to find the respective page. while the abstract class contain the name car, then the following extended class contain the name car, then the following extended class contain respective car name like Audi , Benz, ford etc.. 

                    All the class contain the same common methods like overseeing , break and steering so these method are placed in the abstract so while seeing abstract itself it is easy to find the extended method contain the following methods. Abstract method contain the abstract keyword. The abstract method cannot define in the abstract class.
                  
                    Syntax:
                                 abstract void a();
                               Abstract class
                     Abstract class contain the abstract keyword

                     Example :
                                      abstract class cat
                                      {
                                             abstract void drive();
                                         }
                                               class audi extends car
                                       {                     
                                        @override
                                      void drive()
                                          {
                                            //TO-DO auto-generated method stub
                                              system.out.println(driving Audi);
                                            }
                                         }
                                               class Benz extends car
                                         {
                                            @override
                                       void drive()
                                          {
                                              //Todd auto-generated method stub
                                              system.out.println(Benz);
                                            }
                                                 }
                                         public class Abstractclass
                                         {
                                            public static void main (String args[])
                                          {
                                              car c1=new Audi();
                                              cat c2=new Benz();
                                                    c1.drive();
                                                    c2.drive();
                                            }
                                          }

                      In java abstract we cant create the object , It is possible to refer the abstract class to another class. By default abstract contain the abstract keyword . Abstract method must be extended to some class and by default inside abstract class all variable are static and all methods are abstract.

                      For today we have seen the total concepts on OOPS. see you in next class guys.