Posts

Showing posts with the label Java

Collections in Java

Image
Overview Array An Array is an indexed collection of fixed number of Homogenous data elements. The main advantage of arrays is we can represent multiple values by using single variable so that readability of the code will be improved.  Limitations of Arrays Arrays are fixed in size i.e. once we create an array there is no chance of increasing or decreasing the size based on our requirement. Due to this, to use arrays concept compulsory we should know the size in advance which may not be possible always. Array can hold only homogenous data type elements. Student[] students = new Student[ 1000 ] ; students[ 0 ] = new Student( "Vim Payne" , 18 , 20 , 540 ) ; /* Prompts a compile time error: Incompatible types found: Teacher required: Student */ students[ 1 ] = new Teacher( "Vim Payne" , 18 , "St. Theressa School" , "9A" ) ; We can solve the above problem by using Object type arrays. Object[] students = new Object[ 1000 ] ; students[ 0 ] = new...

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...

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...

final keyword in Java

Image
Overview final class cannot be inherited. final methods cannot be overridden. final variables cannot be changed in future.

Map and HashMap difference

Image
Difference Map is an interface but HashMap is a class. So you cannot create an object of a Map but you can create an object of a HashMap Map <String , Integer> empIds = new Map<>() ; // This will give a compile time error Map <String , Integer> empIds = new HashMap<>() ; // This is allowed Map contains unique key value pairs but HashMap can have duplicate values. Map interface can be implemented by using its implementing class. Whereas HashMap class implements the Map interface. Map is used to create, Hashmap and TreeMap. While HashMap implements Map interface and extends AbstractMap class. There is no difference between the objects of Map and HashMap. Both of them maintains an insertion order. You are not allowed to use primitive data types (int, float, double, char) to define a Map or a HashMap. You have to use non-primitive data types (Integer, Float, Double, Character, String, Objects) to define a Map or HashMap.  Example of Map public class Maps { ...

JAVA

Image
Overview Java is an object oriented programming language that is used to create software for multiple platforms. For more details please click Reference 1. JVM JVM stands for Java Virtual Machine. It translates Java bytecode into native instructions. Once translated these bytecodes and interact with an operating system. If a system doesn't have JVM then the system won't be able to convert Java bytecodes. Note Java is not considered as pure Object Oriented Programming Language because several OOP features are not satisfied by Java. Like operator overloading, multiple inheritance etc... Moreover we are depending on primitive data types which are non-objects.  Reference https://www.techopedia.com/definition/3927/java ://www.javased.com/?action=source-search https://www.demo2s.com/java/java.html