Showing posts with label wrapper class in java. Show all posts
Showing posts with label wrapper class in java. Show all posts

Friday, 8 February 2019

Wrapper Classes in Java - Online Help

Wrapper Class in Java:

Let's discuss wrapper class in java,
   
  The wrapper class in java is nothing but a class which wraps primitive data types. when we create an object for the wrapper class it will store primitive data type in it. In simple, wrapper class provides the mechanism to convert primitives into objects and objects into primitives.

Wrapper classes in java for primitive types:

Each primitive type has a corresponding type-wrapper class( in packages java.lang). These classes are called Boolean, byte, character, double, float, integer, long and short. Each type- wrapper class enables You to manipulate primitive-type values as an object.

 These classes cannot manipulate variables of primitive types, but they can manipulate objects of the type- wrapper classes because every class ultimately derives from the object.

Each of the numeric type-wrapper classes - byte, short, integer, long, float and double extends class number. also, the type-wrapper classes are final classes so you cannot extend them.

The primitive type does not have methods, So the methods related to a primitive type are located in the corresponding type-wrapper class( e.g., method parseInt, which converts a string value in an int value, is located in the class integer).

 If you need to manipulate a  primitive value in your program first refer to the documentation for the type-wrapper classes - the method you need might already be declared.


Autoboxing and auto-unboxing in Java:

        autoboxing and auto-unboxing features added in Java5.  autoboxing is a process by which primitive type is automatically encapsulated (boxed)into its equivalent types wrapper .auto-unboxing is a process by which the value of the object is automatically extracted from a type-wrapper.

In versions of Java Prior to J2SE 5.0, if you want to insert a primitive value into a data structure that could store only objects in the collection or if you want to retrieve an object of a type wrapper class from a collection and manipulate its primitive value, you have to invoke a method on the object to obtain its corresponding primitive type value.

For example, suppose you want to add an int to an array that stores only reference to integer object .prior to J2SE 5.0, you would be required to "wrap" an int value in an integer object before adding the integer to the array and to "unwrap" the int value from the integer object to retrieve the value from the arrays, as in

Integer[] integerArray=new Integer[10];// Create integer array

// Assign integer 10 to integerArray[0]

// get int value of integer

int value=integerArray[0].intvalue();

Notice that the int primitive value 10 is used to initialize an integer object. this Archives the Desire result but requires extra code and is cumbersome. We then need to invoke method int value of the class integer to obtain the int the value in the integer object.

 J2SE 5.0 simplifies between primitive type values and type-wrapper objects, requiring no additional code on the parts of the programmers.J2SE 5.0 introduces two new conversions the boxing conversion and the unboxing conversion.

  A boxing conversion converts a value of a primitive type to an object of the corresponding type-wrapper class. An unboxing conversion converts an object of a type wrapper class to the value of the corresponding primitive type. J2SE 5.0 allows these

  conversions to be performed automatically(call autoboxing and auto-unboxing). For example, various statements can be rewritten as

integer[] integerArray=new Integer[5];//create integer array
integerArray[0]=10;//assign Integer 10 to integerArray[0]
int value=integerArray[];

In this case, autoboxing occurs when assigning int value(10) to integerArray[0], because integerArray Store reference to integer objects, not int primitive values, auto-unboxing occurs when assigning integerArray[0] to int variable value, because variable value stores an int value, no reference to an integer object.autoboxing and  unboxing also occur in control statements-the condition of a control statement can evaluate to a primitive Boolean type or a boolean reference type. These conversions store primitive values in and retrieve them from a data structure that store only
references to objects.

program to demonstrate autoboxing and unboxing:

class test
{
public static void main(String[] args)
{
integer iob=100;         //autoboxing of int
int i=iob;                    //unboxing of integer
System.out.println(i+""+iob);
character cob='a';             //autoboxing of char
char ch=cob;                   //unboxing of character
System.out.println(cob+""+ch);
}
}

The output is

100 100
a a

Benefits of autoboxing/ auto-unboxing :

  •  autoboxing and unboxing let us use primitive types and wrapper class objects interchangeably.
  •  we don’t have to perform explicit Typecasting.
   it helps prevent errors but may lead to unexpected result Sometimes .hence must be used with care.

Recursion in java: factorial

let's write a recursive program to perform a popular mathematical calculation consider the factorial of a positive integer n, written! (pronounced "n factorial") which is the product

  n.(n-1).(n-2)..............

With 1! equal to 1 and 0! define to be 1. for example5! is the product, iteratively(non recursively)using a for a statement as follow:

factorial=1;
for(int counter=number ;counter>=1;counter--)
factorial*=counter;

A recursive declaration of the factorial method is arrived by observing the following relationship:

n! =(n.(n-1)!)

For example,5!is clearly equal to 5.4! as shown by the following equations:

5!=5.4.3.2.1
5!=5.(4.3.2.1)
5!=5.(4!)

Sample Program:

import java.util.*;
class factorialdemo
{
public static void main(String args[])
{
int n;
Scanner s=new Scanner(System.in);
System.out.println("Enter the number");
n=s.nextInt();
int f=1;
for(int i=1;i<=n;i++)
f*=i;
System.out.println(+n+"!="+f);
}
}

The recursive method factorial first test to determine whether a terminating condition is true.if the number is less than or equal to 1(base case), factorial returns 1, no further recursion is necessary and the method returns. if the number is greater than 1 line 12 expresses the problem as the product of a number and a recursive call to factorial evaluating the factorial of number -1, which is a slightly simpler problem than the original calculation, factorial(number).

The output is:

5
5!=120


Recommended posts: