Sunday, December 21, 2014

Integer wrapper return true from range -128 to 127

We are already familiar with boxing and auto boxing of wrapper to premitives and vice-verse.

See the below example.

    Integer integer1=127;
    Integer integer2=127;

    System.out.println(integer1==integer2);//The output will be true.
   
But as we increase the value from 127 to 128 you will see the output is different.
   
    Integer integer1=128;
    Integer integer2=128;

    System.out.println(integer1==integer2);//The output will be False.

As per JLC the boxed value returns true for int or short in the range of -128 to 127(included). It also returns true for a byte or a char in the range of \u0000 to \u007f.



Thursday, December 18, 2014

Finding all elements whose Id starts with a specific word using jQuery

Some time we come across with a problem where we have a lots of elements in HTML or JSP page which is generated dynamically and their ID starts with some special word.
Like as follows


<input id="item_1fruit" name="fruit" type="text"  value=" Mango" />
<input id="item_2veg" name="vegetable" type="text" value="Potato" />
<input id="item_3eggs" name="eggs" type="text" value="Eggs" />
<input id="item_4nuts" name="nuts" type="text" value="Almonds"/>

So above we can get all elements in jQuery as follow

$("[div^=item_]")   // do whatever you want
 In below code we are iterating all the elements and setting some values.

$("[div^=item_]").each(function(){

     var text_value= $(this).val();
      alert("Text box value "+text_value);
        });


Autoboxing and Unboxing in Java



Autoboxing means conversion of java premitives to their corresponding wrapper object class, it happens automatically by the java compiler.
For example int to Integer, double to Double.

Unboxing is quite opposite to the autoboxing means java wrapper objects are automatically converts to their premiteves by compiler.
For example Integer to int, Boolean to boolean,Double to double.

See the below written code snap
Autoboxing
a.       int x=100;
                     Integer int_val=x;
b.     double amount=10.50;
  Double amount_val=amount;
Unboxing
a.       Integer int_val=new Integer(100);
 int score=int_val;
b.    Double amount=new Double(100.50);
                    double total_amount=amount;

Using this java feature we can  also assign any value to the super class Object.
For example you can write code as follows using Autoboxing.

        Object x=1;
       Object name="Singh";
       System.out.println(x);
       System.out.println(name);

The above code will run successfully.
 This feature was introduced in Java 5 realease in September 2004.

Sunday, December 14, 2014

Accessing Private Methods and Variables in Java using Reflection API


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//Accessing Private methods using Reflection

public class Test {
 public static void main(String ag[])
 {
  A obj=new A();

  try {
   Class c=obj.getClass();
   Method method=c.getDeclaredMethod("msg", null);
   //System.out.println(method);
   try {

    method.setAccessible(true);
    method.invoke(obj,null);//Calling Private method of a class.

   } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } catch (SecurityException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }


 }
}
////////////second class************Having Private methods
class A
{
 private void msg()
 {
  System.out.println("In class A of private Mehod");
 }
}


//Hi Guys we can access the private fields of a java class using reflection in the same way.
Thanks
Pankaj Kumar Singh

Spring Boot Config Server and Config Client.

 In Spring cloud config we can externalise our configuration files to some repository like GIT HUT, Amazon S3 etc. Benefit of externalising ...