Posts

Showing posts from November, 2022

JDBC Introduction

Image
Standard Steps followed for developing JDBC(JDBC4.X) Application ======================================================= 1. Load and register the Driver 2. Establish the Connection b/w java application and database 3. Create a Statement Object 4. Send and execute the Query 5. Process the result from ResultSet 6. Close the Connection Step1: 1. Load and register the Driver A third party db vendor class which implements java.sql.Driver(I) is called as "Driver". This class Object we need to create and register it with JRE to set up JDBC environment to run jdbc applications. Note: public class com.mysql.cj.jdbc.Driver extends com.mysql.cj.jdbc.NonRegisteringDriver implements java.sql.Driver { public com.mysql.cj.jdbc.Driver() throws java.sql.SQLException; static {}; } In MySQL Jar, Driver class is implementing java.sql.Driver, so Driver class Object should be created and it should be registered to set up the JDBC environment inside JRE. 2. Establish the Connection b/w java applica...

Annotations

 Usually comments added in the code will be removed during the compilation process. It will not reach till the runtime. It can also called as meta data(data about data). To overcome this we have one approach called XML which is not user friendly. So that's why we are going for Annotations. Annotations are case sensitive.  => It is present inside the java.lang.annotation.Annotation and by default Annotation is the parent for all the annotations. We can use annotations every where inside code like methods, variables, parameters, constructors, interfaces etc..  => Annotations means information about code.  => Annotations are of 2 types. i) Inbuilt           ii) Custom =>Annotations works based on the mechanism of interface.  

ENUMS

 Enums are present inside the lang package. We no need to import explicitly. It will get imported automatically. It is nothing but group of predefined constants. It is something like our own data type. It can't be changed. By default they are public static and final. After compilation of the code, Enums will also generate the .class files. It's a way of creating our own data types. usually the values inside the Enums will be denoted in uppercase letters to denote that those are constants. Enums can be created inside the class as well as outside of the class. and when Enums are created, It will extent parent ENUM class automatically and ordinal() and Values methods will be inherited as well.  Example: Enum week { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; }  Inside Enums we can have Constructors, methods, fields or instance variables, constants  => when we call week.MONDAY, It will create an object and constructor will be called automatically. and the ...

Difference b/w java.util.Date and java.sql.Date

java.util.Date => It is a utility class to handles Date in our java program. => It represents both Date and Time java.sql.Date => It is designed class to handle Dates w.r.t DB operations => It represents only Date,but not Time. Note: In sql package      Time(C) represents only => Time value      TimeStamp(C) represents both => Date and Time value. Date and Time API: (Joda-Time API) Until Java 1.7 version the classes present in Java.util package to handle Date and Time (like Date, Calendar, TimeZone etc) are not up to the mark with respect to convenience and performance. To overcome this problem in the 1.8 version oracle people introduced Joda-Time API. This API developed by joda.org and available in Java in the form of "java.time" package. # program for to display System Date and time. import Java.time.*; public class DateTime { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println(date); LocalTime t...

Intermediate operation and Terminal operation

 Intermediate operation is a kind of Lazy one which doesn't do the work when no one is dependent/ watching on him. It means if no function is added after the intermediate operation function then if we try to print that one, It will not display anything but when any function is added after the intermediate operation then if we try to print, It will display the data. Eg: filter() and map() in Stream API Terminal operation is something like it doesn't bother who is watching or not watching. It will do the work even if no function is attached after it.  Eg: forEach() in Stream API/.

Stream API

 Stream API is introduced in java 1.8 and the method stream() is a default method in Collection interface.  Once you work on the stream, you can't reuse it. It will throw an IllegalStateException. Advantages of Stream API: 1. Stream is fast compared to normal collections. and It is an internal process.                                                       2. Stream make sure that you can use multiple threads to work on your data                                                                3. It also make sure that you don't change the existing data                    ...

Generics

Deff : The main objective of Generics is to provide Type-Safety and to resolve Type-Casting problems. Case 1: Type-Safety Arrays are always type safe that is we can give the guarantee for the type of elements present inside array. For example if our programming requirement is to hold String type of objects it is recommended to use String array. In the case of string array we can add only string type of objects by mistake if we are trying to add any other type we will get compile time error. eg: String name[] =new String[500]; name[0] = "Navin Reddy"; name[1] = "Haider"; name[2] = new Integer(100); //CE: incompatbile types found: java.lang.Integer required: java.lang.String That is we can always provide guarantee for the type of elements present inside array and hence arrays are safe to use with respect to type that is arrays are type safe. But collections are not type safe that is we can't provide any guarantee for the type of elements present inside collection....

Comparator

 It is an interface, Comparator is used over Comparable when the source code is not available and we can have an option of proving the own sorting logic. The class which is implementing the Comparator interface should override the  compare(Object o1, Object o2) method. Late we need to create the object of the class which is implementing the comparator interface. and that object should be passed to the Collections.sort(x1, obj) method. we can also use the lambda implementation as we are override only one method and it is a functional interface. import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class ComparatorImpl implements Comparator<Student>{ @Override public int compare(Student s1, Student s2) { // TODO Auto-generated method stub return s1.name.compareTo(s2.name); } } public class ComparatorExample { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Student> al=new Arr...

Comparable

 It is an interface, we need to implement this interface to the class which we are working with. and we need to import the java.util.Comparator package in order to work with comparable interface. and also we need to override the compareTo method. Comparable will be used when the source code file is available, In our Example, It is Student class. import java.util.*; import java.util.Collections; import java.util.Comparator; // class Student implements Comparable<Student> // { // int age; // String name; // String tech; // Integer i; // public Student(int age, String name, String tech) { // this.age = age; // this.name = name; // this.tech = tech; // } // @Override // public String toString() { // return "Student [age=" + age + ", name=" + name + ", tech=" + tech + "]"; // } // @Override // public int compareTo(Student that) // { // return this.name....

Map

Image
Map is not a part of collection to add the elements use put(object,object); to get the Key use getKey(); Inside the Map interface, Entry interface will be present, Interface Map {     Interface Entry {                     } }   In LinkedHashMap Order of insertion is preserved whereas in HashMap Order of insertion is not preserved. LinkedHashMap is a Child of HashMap.  If it is a HashMap, JVM internally calls equals(obj) to identify whether keys are duplicated are not.       Garbage collector won't clean the null objects because HashMap will always dominate the garbage collector. Garbage collector will clean the objects if it is present in WeakHashMap. So gc collector dominates over weak hashmap.

Fail Safe and Fail Fast

Fail Fast: When we try to attempt concurrent modification or structural modification, It will not allow that to happen rather it will leave to exception.  Eg: In case of for loop when we are adding the data while we are accessing then it will result in never ending loop.  for (int i=0; i<al.size();i++) { System.out.println(al.get(i)); al.add(99)// } To overcome this we will use Iterator. If we use Iterator instead of for loop It will result in Fail Fast by giving exception. Iterator itr=al.iterator(); while(itr.hasNext()){ system.out.println(itr.hasNext()); } Fail Safe: It is a mechanism which will stop concurrent modification or structural modification without giving any exception. U need to use CopyOnWriteArrayList class to achieve this So, we will import the concurrent packages when we want to achieve Fail Safe 

Collections

Image
When java is introduced, it was open source, and anyone can contribute. From java 1.2 , collection framework is available. before that it is not there.  =>Under Collection hierarchy there are 7 classes  Array List: It is implementing List interface. It is suitable at the insertion at the rear end.   Linked List: It is implementing List as well as deque interface. It is suitable at the insertion at any end. Array deque: It is implementing deque interface which extends queue interface. It is suitable at the insertion at the rear and backend  Priority deque: Automatically high priority element is available at the beginning of the collection. It is suitable at the insertion at the backend. Tree set: It will give you in the sorted order and it is also suitable for the searching operation if and only if the tree is balanced binary search tree. and in case of skewed tree searching operation is not recommended. Hash set: order of insertion is not maintained and searc...