Posts

Showing posts from January, 2022

Serialization in Java

Image
Overview Serialization is the process of converting an object into a byte stream. This byte stream can be saved as a file (.ser) or sent over a network. The file is platform independent.  Deserialization is the process of converting byte stream back into an object.   Do's and Dont's If you don't want to serialize a property or method then you should declare it as transient or static. If have Serialized a parent class then you don't have Serialize a child class. It's vice-versa is not true. Reference https://www.geeksforgeeks.org/serialization-in-java/

Synchronization in Java

Image
Overview Synchronization is the process by which we control the accessibility of multiple threads for a particular resource. With synchronization only one thread at a time can execute a particular method or block of code. Without synchronization we can face the problem of data inconsistency and thread interference.  Disadvantages of Synchronization Synchronization can increase the waiting time of application due to synchronization of multiple threads. At times, synchronization can create performance issues. Types of Synchronization Process Synchronization - not present in Java multithreading. Thread Synchronization -  Mutual Exclusive - It can be achieved by 3 ways By synchronized method synchronized void bookSeats ( int seats) { if (seats <= total_seats ) { System. out .println(seats + " seats are booked successfully!!!" ) ; total_seats = total_seats - seats ; System. out .println( "Seats left: " + total_seats ) ; } else {

Threads in Java

Image
Overview When a program is executing it follows a path, that is Thread. Threads are used to perform critical tasks in the background without interrupting the main thread. There are two ways to create a Thread. By extending the class Thread By implementing an interface Runnable Five states of Thread Born Runnable Running Non Runnable Dead sleep() vs wait() sleep() belongs to Thread class whereas wait() belongs to an object. sleep() can be called without a synchronized context but wait() should be called in a synchronized context. sleep() does not release lock on an object but wait() releases lock on an object during synchronization. sleep() is a static method but wait() is not a static method. sleep() is executed on a Thread but wait() method is executed on an object.  interrupt() This method is used to interrupt an executing Thread. If a Thread is in sleep() or wait() state then the interrupt() method will get called and the catch() statement gets executed. If a Thread doesn't go i

Generics in Java

Image
Overview Generics are introduced to deal with type-safe objects. There are three advantages of using Generics Ensure type safety. Eliminate type casting Compile time checking. Example public class GenericsDemo { public static void main (String[] args) { // Class to print an Integer IntegerPrint integerPrint = new IntegerPrint( 2 ) ; integerPrint.print() ; // Class to print a Double DoublePrint doublePrint = new DoublePrint( 23.0 ) ; doublePrint.print() ; // Class to print a String StringPrint stringPrint = new StringPrint( "Chinki" ) ; stringPrint.print() ; // Just to print a value of different type you got to write three different classes. To avoid writing redundant codes you can use Generics. // Generic class to print all types of value. Print<Integer> printInteger = new Print<>( 7 ) ; printInteger.print() ; Print<Double> printDouble = new P

LinkedList and ArrayList difference

Image
Overview LinkedList and ArrayList are similar Collections.  public class ArrayListDemo { public static void main (String[] args) { LinkedList<String> nameLinkedList = new LinkedList<>() ; nameLinkedList.add( "Lucky" ) ; nameLinkedList.add( "Ivy" ) ; nameLinkedList.add( "Oreo" ) ; nameLinkedList.add( "Brownie" ) ; nameLinkedList.add( "Casie" ) ; nameLinkedList.add( "Tom" ) ; System. out .println(nameLinkedList) ; ArrayList<String> nameArrayList = new ArrayList<>() ; nameArrayList.add( "Lucky" ) ; nameArrayList.add( "Ivy" ) ; nameArrayList.add( "Oreo" ) ; nameArrayList.add( "Brownie" ) ; nameArrayList.add( "Casie" ) ; nameArrayList.add( "Tom" ) ; System. out .println(nameArrayList) ; } } Though there are some m

Recursion in Java

Image
Overview When we call a method inside a same method that is recursion. Following is a simple example of recursion. public class RecursionDemo { public static void main (String[] args) { sayHi () ; } // The reason why we marked this method as static because we want to call this method in a static method that is main() private static void sayHi () { System. out .println( "Hi!" ) ; sayHi () ; // This recursion will throw a StackOverflowError. } } To handle a recursion you got to have an exit strategy / condition. public class RecursionDemo { public static void main (String[] args) { // sayHi(); sayHello ( 3 ) ; } // The reason why we marked this method as static because we want to call this method in a static method that is main() private static void sayHi () { System. out .println( "Hi!" ) ; sayHi () ; // This recursion will throw a StackOverflowError. } private stat

Enum in Java

Image
Overview Enum is a short time of Enumeration. It is used to define a set of values that is not going to change in future e.g. days of a week.  Example of simple Enum public enum DaysOfTheWeek { SUNDAY , MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY , SATURDAY } public static void main (String[] args) { DaysOfTheWeek day = DaysOfTheWeek. FRIDAY ; if (day == DaysOfTheWeek. FRIDAY ) { System. out .println( "Yay! Tomorrow is Saturday." ) ; } for (DaysOfTheWeek myDay : DaysOfTheWeek. values ()) { System. out .println(myDay) ; } } Example of Enum with Constructor public enum Burgers { CHICKEN_BURGER ( 3 , 15.10 ) , VEG_BURGER ( 5 , 12.25 ) , EGG_BURGER ( 4 , 13.50 ) , KING_BURGER ( 4 , 20.25 ) ; final int rating ; final double price ; Burgers ( int rating , double price) { this . rating = rating ; this . price = price ; } } public static void main (String[] args) { for (Burgers bur

Constructors in Java

Image
Overview Types of Constructors Default Constructors - These are default constructors who doesn't have any arguments Dog myDog = new Dog() ; public Dog () { // Write the code that will execute automatically when an object of Dog is created } Parameterized Constructor Dog bravo = new Dog( "Bravo" ) ; public Dog (String name) { this . name = name ; } Dog newDog = new Dog( 5 ) ; public Dog ( int age) { this . age = age ; } Dog jackie = new Dog( "Jackie" , 3 ) ; public Dog (String name , int age) { this . name = name ; this . age = age ; } Hello World Do's and Dont's The purpose of new keyword is to create an object. The purpose of a constructor is to initialize an object. In the event of creating an object: first new keyword is executed to create an object and then the constructor method of a class is called to initialize the object.  If you have added a parameterized constructor in your class, then Java won't call the defa

Multi-Threading in Java

Image
Overview Introduction Multitasking: Executing several tasks simultaneously is the concept of Multitasking. There are two types of Multitasking Process Based Multitasking: Executing several tasks simultaneously where each task is a separate independent program (process) is called Process Based Multitasking. e.g. While typing a Java program in the editor we can listen audio songs from the same system. At the same time, we can download a file from net. All these tasks will be executed simultaneously and independent of each other. Hence it is Process Based Multitasking. Process Based Multitasking is best suitable at OS level. Thread Based Multitasking: Executing several tasks simultaneously where each task is a separate independent part of the same program is called Thread Based Multitasking. And each independent part is called a Thread. Thread Based Multitasking is best suitable at programmatic level. Whether it is Process Based or Thread Based, the main objective of Multitasking is to re