Posts

Thread and Thread Safety in Java.

  Threads are small programs(piece of code/process) part of large programs/processes. Java allows multiple threads to run concurrently for maximum utilization of CPU.   Thread creation . Class A which Either Extend Thread class or implement Runnable interface, both contains run method. We need to override run method. Object of class A becomes a thread.  Thread life - starts by calling start() method from thread object.                            this start method internally call run() method ,where we define the code to execute. E.g. class MultithreadingDemo ( extends Thread)/( implements Runnable ) {        public void run() { //code to execute for each thread } } public class Multithread // driver class {      public static void main(String[] args)      {              MultithreadingDemo object = new MultithreadingDemo(); // thread created,if we want more thread we make more object              object.start(); //thread started and will call run method internally      }

Checked and Unchecked Exception in Java

  Checked Exception- probable   Exception which should be checked at compile time for any     unwanted situation need to be handled. Those are taken care by compiler.   For e.g. We are doing any file reading . There is a possibility ,file can't be found at a given place.  So, compiler force us to do handle this exception otherwise it will be a compilation error.   We can do either by throws keyword or try-catch block. Unchecked Exception- Exception thrown at run time ,i.e. compiler is unable to check those error at compile time . For e.g.logic error, a number divided by zero throws exception at run time. Refer- https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/

Static Keyword in Java

 Static Keyword is a modifier and can be applied to variable , method, static block and static class. 1. Static Variable - only 1 copy, can be access by classname directly, can be used for common thing .      For e.g. for n students if college name is same.                     Static String collegeName ="IIIT" 2. Static method - can only access static variables and methods. But non static method can access         both static and non static method     Note-  Main method is also a static method.  3. Static block - Can execute before main method. Static block and static variable are                                executed depends on from top to bottom declaration inside class. 4. Nested Static class - In Java ,we can declare class inside another class. known by names Outer        class and inner class. We can't declare a class as static but we can declare an inner class as             static class. Object declaration is different for accessing inner class.     Refer-  ht

Polymorphism

 1. Polymorphism       Static polymorphism can be by change in number of arguments or by change in data type passed      to function.     Runtime polymorphism can be obtained when same method is defined in parent and sub class.    Sub class object can be assigned to parent class object. But when same name method is called by       above assigned parent object, then sub class method is called, not parent class method.               For e.g. parent class Parent contains method print("In Parent class")                                   Subclass S1,S2 contains method print("In sub class 1"),print("In sub Class 2")                                            respectively                                                            Inside Test class -                                 Parent P=new S1(); P.print();// will print In subclass 1                                 Parent P=new S2();P.print();//  will print In subclass 2 Refer-  https://www.geeksforgeeks.org/