Showing posts with label wrapper. Show all posts
Showing posts with label wrapper. Show all posts

Tuesday, 21 June 2011

Some issues with Autoboxing

Integer j1 = 127;
Integer j2 = 127;
System.out.println( j1==j2); //Works!!!
Integer k1 = 128;
Integer k2 = 128;
System.out.println( k1==k2); //Doesn't Work!!!
Integer w1 = -128;
Integer w2 = -128;
System.out.println( w1==w2); //Works!!!
Integer m1 = -129;
Integer m2 = -129;
System.out.println( m1==m2); //Doesn't Work!!!

This gives us similarity to constant string pool.

Monday, 25 April 2011

Java: count the number of one bits in an int

Integer.bitCount counts the number of one-bits in the two's complement binary representation of the specified int value. Example code:
public class CountOnes {
public static void main(String[] args) {
int[] i = { 1, 4, 7, 15 };
for (int j = 0; j < i.length; j++) {
System.out.printf("Number of 1's in %d: %d\n", i[j], Integer.bitCount(i[j]));
}
}
}

Friday, 22 April 2011

Primary datatypes and their wrapper classes

Corresponding to all the primitive type there is a wrapper class defined. These wrapper classes are OOP version of the primitive types. These classes provide useful methods for manipulating primitive data values and objects.

The language designers decided that the higher processing speed and memory efficiency of simple, non-class structures for such heavily used data types overruled the elegance of a pure object-only language.The designers decided instead that for each primitive type there would be a corresponding wrapper class. An instance of a wrapper contains, or wraps, a primitive value of the corresponding type.

Some primitive types and their wrapper classes:

Data types Wrapper class
int Integer
short Short
long Long
byte Byte
char Character
float Float
double Double

he wrapper constuctors create class objects from the primitive types. For example, for a double floating point number "d" :
     double d = 5.0;
     Double aD = new Double(d);

Here a Double wrapper object is created by passing the double value in the Double constructor argument.
In turn, each wrapper provides a method to return the primitive value
     double r = aD.doubleValue();

Each wrapper has a similar method to access the primitive value: integerValue() for Integer, booleanValue() for Boolean, and so forth.

Monday, 18 October 2010

Autoboxing in java


Autoboxing, introduced in Java 5, is the automatic conversion the Java compiler makes between the primitive (basic) types and their corresponding object wrapper classes (eg, int and Integer, double and Double, etc). The underlying code that is generated is the same, but autoboxing provides a sugar coating that avoids the tedious and hard-to-read casting typically required by Java Collections, which can not be used with primitive types.

In the previous section on wrapper classess for primitive type values, we discussed how to create an instance of a wrapper from a primitive and conversely, how to obtain the primitive value held by the wrapper. This involves a a certain amount of clumsy code.
For example, creating a Float object from a float primitive is straightforward:
  float primitive_float = 3.0f;
  Float wrapper_float = new Float (primitive_float);
Going the other direction, however, requires explicitly calling the floatValue() method on the Float object:
  float primitive_float = wrapper_float.floatValue ();
If you are dealing with a lot of instances of wrappers and conversions, you will thus need to deal with a lot of method invocations.
In J2SE 5.0, however, the code to create the wrapper object allows for this much simpler form:
  Float wrapper_float = primitive_float;
Here, the "wrapping" is done automatically! There is no need to explicitly call the Float constructor. This "wrapping" is called "autoboxing" in the sense that the primitive value is automatically "boxed up" into the wrapper object. Autoboxing is available for all the primitive/wrapper types.
Going the other way, from object type to primitive, is just as simple:
  Integer wrapper_integer = 5; // primitive 5 autoboxed into an Integer
  int primitive_int = wrapper_integer; // automatic unboxing Integer into int


Now we can have this.
int i;
Integer j;
i = 1;
j = 2;
i = j;
j = i;
Earlier we had to do this.
int i;
Integer j;
i = 1;
j = new Integer(2);
i = j.intValue();
j = new Integer(i);
This is quite clumsy.

Also see - Some issues with Autoboxing