Posts

JSON

Jackson import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); Car car = new Car(); car.brand = "BMW"; car.doors = 4; String json = objectMapper.writeValueAsString(car); System.out.println(json)       String json = "{\n" +                 "    \"name\": \"Garima\",\n" +                 "    \"surname\": \"Joshi\",\n" +                 "    \"phone\": 9832734651}";  User garima = new ObjectMapper().readValue(json, User.class); jackson-xc-1.9.11.jar jackson-core-asl-1.9.11.jar jackson-mapper-asl-1.9.11.jar String carJson =        "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }"; ObjectMapper objectMapper = new ObjectMapper();  JsonNode node = objectMapper.readVa...

Java 8

   lambda expression is a function:   (parameters) -> expression or     (parameters) -> { statements; } 1. (int x, int y) -> x + y                          // takes two integers and returns their sum 2. (x, y) -> x - y                                  // takes two numbers and returns their difference 3. () -> 42                                         // takes no values and returns 42 4. (String s) -> System.out.println(s)             // takes a string, prints its value to the console, and returns nothing 5. x -> 2 * x                                       // takes a number...

Equals vs ==

1. == Is used to compare reference and equals is used to compare the contents. 2. == We can used with primitive type, when we used == with primitive types(data types) it will work as comparing two values. But equals we can not used for primitives type as it is method declared in object class. In short all the classes in java having own equals method. 3. If we call equals on reference variable who have null value, then we will get nullpointerexception. Example String s; S.equals("Abc") It will throw nullpointerexception. 4. Equals is a method which is in object class. Object class is extended by all the classes in java. So in short equals method is contained by all the classes in java.

Reflection method call

public class A {   private A(){        System.out.println("This is A");    } }   ..........................................................................................................................import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;                                                                                                   public class Main { public static void main(String[] args) { try { Class a = A.class; Constructor c = a.getDeclaredConstructor((Class[])null); c.setAccessible(true); A obj = c.newInstance((Object[])null); Method privateMethod = a.getDeclaredMethod("obj", (Class[])null); privateMethod.setAc...

Up-Casting(Super Type Casting) vs Down Casting(SubType Casting)

Image
Up-casting is casting to a supertype, while downcasting is casting to a subtype. Supercasting is always allowed, but subcasting involves a type check and can throw a ClassCastException. In your case, a cast from from Dog to an Animal is a upcast, because a Dog is-a Animal. In general, you can upcast whenever there is an is-a relationship between two classes. Downcasting would be something like this: Animal animal = new Dog(); Dog castedDog = (Dog) animal;

HashMap Collision

public class Simple{ public static void main(String args[]){ Map map=new HashMap (); map.put(1, 11); map.put(4, 11); System.out.println(map.hashCode()); Map map1=new HashMap (); map1.put(1, 11); map1.put(4, 11); System.out.println(map1.hashCode()); if (map.equals(map1)) {     System.out.println("equal "); } }} 25 25 equal 

rules for overriding

Image
Rule #1: Only inherited methods can be overridden. Rule #2: Final and static methods cannot be overridden. Rule #3: The overriding method must have same argument list Rule #4: The overriding method must have same return type (or subtype). Rule #5: The overriding method must not have more restrictive access modifier. Rule #6: The overriding method must not throw new or broader checked exceptions. Rule #7: Use the super keyword to invoke the overridden method from a subclass Rule #8: Constructors cannot be overridden. Rule #10: A static method in a subclass may hide another static one in a superclass, and that’s called hiding. Rule #11: The synchronized modifier has no effect on the rules of overriding. Rule #12: The strictfp modifier has no effect on the rules of overriding.