Our Recommendation for You Search your Query, You can find easily. for example search by book name or course name or any other which is related to your education

Followers

18: Wrapper Classes /Java Programming in Urdu

 Wrapper Classes

All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are sub-classes of the abstract class Number.


The object of the wrapper class contains or wraps its respective primitive data type. Converting primitive data types into object is called boxing, and this is taken care by the compiler. Therefore, while using a wrapper class you just need to pass the value of the primitive data type to the constructor of the Wrapper class.
And the Wrapper object will be converted back to a primitive data type, and this process is called un-boxing. The Number class is part of the java.lang package.
let see example for wrapper class
public class Test {

   public static void main(String args[]) {
      Integer x = 5; // boxes int to an Integer object
      x =  x + 10;   // unboxes the Integer to a int
      System.out.println(x); 
   }
}


Let’s see java program which explains few wrapper classes methods.

package WrapperIntro;
public class WrapperDemo {
public static void main (String args[]){
Integer intObj1 = new Integer (25);
Integer intObj2 = new Integer ("25");
Integer intObj3= new Integer (35); //compareTo demo
System.out.println("Comparing using compareTo Obj1 and Obj2: " + intObj1.compareTo(intObj2));
System.out.println("Comparing using compareTo Obj1 and Obj3: " + intObj1.compareTo(intObj3)); //Equals demo
System.out.println("Comparing using equals Obj1 and Obj3: " + intObj1.equals(intObj3));
System.out.println("Comparing using equals Obj1 and Obj2: " + intObj1.equals(intObj2)); Float f1 = new Float("2.25f"); Float f2 = new Float("20.43f"); Float f3 = new Float(2.25f);
Float f = intObj1.floatValue() + f1;
System.out.println("Comparing using compare f1 and f2: " +Float.compare(f1,f2)); System.out.println("Comparing using compare f1 and f3: " +Float.compare(f1,f3)); //Addition of Integer with Float
}
System.out.println("Addition of intObj1 and f1: "+ intObj1 +"+" +f1+"=" +f );
}

The most common methods of the Integer wrapper class are summarized in below table. Similar methods for the other wrapper classes are found in the Java API documentation.

Method
Purpose
parseInt(s)
returns a signed decimal integer value equivalent to string s
toString(i)
returns a new String object representing the integer i
byteValue()
returns the value of this Integer as a byte
doubleValue()
returns the value of this Integer as a double
floatValue()
returns the value of this Integer as a float
intValue()
returns the value of this Integer as an int
shortValue()
returns the value of this Integer as a short
longValue()
returns the value of this Integer as a long
int compareTo(int i)
Compares the numerical value of the invoking object with that of i. Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.
static int compare(int num1, int num2)
Compares the values of num1 and num2. Returns 0 if the values are equal. Returns a negative value if num1 is less than num2. Returns a positive value if num1 is greater than num2.
boolean equals(Object intObj)
Returns true if the invoking Integer object is equivalent to intObj. Otherwise, it returns false.