Posts

Showing posts from June, 2021

Object casting in java

Image
 Object casting in java: A b = (C)d A - Class or Instance Name b - Reference Variable Name C - Class or Instance Name d - Reference Variable Name 3 Golden rules: Rule 1 : (checked  by compiler) There should be parent/child relation between C and d, object reference d should be child or parent of Class C else compiler will throw inconvertable compilation error. Eg: String o=new String("hellboymaddi");       StringBuffer b=(StringBuffer)o; In the above example, reference variable o is of String type and there is no parent & child relationship with String Buffer,so it will raise compilation error and exact message is: inconvertible types found: java.lang.String required: java.lang.StringBuffer Rule 2: (Checked by compiler) We are assigning class C to Class A,so Class c Should be equal to Or child Class of A, else compiler will throw incompatible compilation error. Eg: Object o=new String("hellboymaddi");       String b=(StringBuffer)o; In th...

Generics

 Generics important points missed in durga sir material: 1.Generics is used at compile time only  Jvm didn't know about generics concept. Generics concept is used by compiler to provide type safety After compiling the code, generics syntax is removed and jvm doesn't know about it At compile time following actions were performed compiler compiles code normally by considering generic syntax Removes Generic syntax compile once again resultant code. Eg:This can be cleary observed by durga sir examples: For mmore info refer Type Eraser  concept Example 1:   1.ArrayList l=new ArrayList<String>();  2.ArrayList l=new ArrayList<Number>();       In the above example, compiler checks for object reference, we didn't provide any generic type heren new ArrayList<String> is invoked at run time but by this time, generics syntax will be removed. Example 2: Class Test{ public void m1(List<String> l){ } public void m1(List<Integer> l){ ...