This is basically for all those hard-core coders so that they sit properly before the laptop and their neck doesn't pain..!!!...please follow it..!!
Monday, March 5, 2012
Sunday, March 4, 2012
Boxing and Unboxing in Java
Boxing and Unboxing in Java
Integer y = new Integer(56); // make it
int x= y.intValue(); // unwrap it
x++; // use it
y = new Integer(x); // re-wrap it
System.out.println("y =" + y); // print it
y++; //unwrap-it, increment-it, rewrap-it
System.out.println("y = " + y); // print it
y=57
Integer x = y; // assign a second ref var to the wrapper
System.out.println(y==x);
y++; //unwrap, use , rewrap
System.out.println(x+ " " + y); // print values
System.out.println(y==x); //verify that they refer
// to different objects
56 57
false
x2++;
y = new Integer(x2); // rewrap-it
Where Boxing Can Be Used
class UseBoxing {
public static void main(String args[])
{
UseBoxing u = new UseBoxing();
u.go(5);
}
boolean go(Integer i)
{
Boolean ifSo = true; //boxes the int
Short s = 400; //boxes the literal
if(ifSo)
{ // unboxing
System.out.println(++s); //unboxes, increments,
// reboxes.
}
return !ifSo;
}
}
- Boxing and Unboxing make using wrapper classes more convenient.
- In Java 5, new feature came, and it was Auto-boxing and Auto-unboxing.
- In the old, pre-Java 5 days, if you wanted to make a wrapper, unwrap it, use it, and then rewrap it, you might do something like this:
Integer y = new Integer(56); // make it
int x= y.intValue(); // unwrap it
x++; // use it
y = new Integer(x); // re-wrap it
System.out.println("y =" + y); // print it
- Now improved version of Java 5 you can say,
y++; //unwrap-it, increment-it, rewrap-it
System.out.println("y = " + y); // print it
- Both Examples produce the output::
y=57
- Behind the scenes, the compiler does the unboxing and reassignment for you.
- We knew that wrapper objects are immutable, this example appears to contradict that statement. It sure looks like y's value changes fro 56 to 57.
- What actually happened, is that second wrapper object was created and its value was set to 57.
- We could only prove it as:-
Integer x = y; // assign a second ref var to the wrapper
System.out.println(y==x);
y++; //unwrap, use , rewrap
System.out.println(x+ " " + y); // print values
System.out.println(y==x); //verify that they refer
// to different objects
- Which produces the output:
56 57
false
- So under the covers when the compiler got to the line y++; it had to substitute something like this:
x2++;
y = new Integer(x2); // rewrap-it
Where Boxing Can Be Used
- It is very common to use wrappers in conjunction with collections.
- Any time you want your collection to hold objects and primitives, you 'll want to us wrappers to make those primitives collection-compatible.
- The general rule is that boxing and unboxing work wherever you can normally use a primitive or a wrapped object.
class UseBoxing {
public static void main(String args[])
{
UseBoxing u = new UseBoxing();
u.go(5);
}
boolean go(Integer i)
{
Boolean ifSo = true; //boxes the int
Short s = 400; //boxes the literal
if(ifSo)
{ // unboxing
System.out.println(++s); //unboxes, increments,
// reboxes.
}
return !ifSo;
}
}
Monday, January 16, 2012
Vector Class in J2SE
- It extends AbstractList, which is also a class in java.
- It implements List, RandomAccess, Cloneable, Serializable, these are interfaces in java.
- Like an array, it contains components that can be accessed using an integer index. However, the size of vector can grow or shrink as needed to accomodate adding and removing items after the vector has been created.
- Each Vector tries to optimize storage management by maintaining a capacity and a capacityIncrement.
- Vector Size <= Capacity.
Subscribe to:
Posts (Atom)