Wrapper classes(Part-3) in Java

ValueOf() method

In java, wrapper class has three kinds of methods for ValueOf() in which;

  1. Primitive type is taken as argument
  2. String type is taken as argument
  3. String and int type, both taken as argument at once

In these methods, primitive values is boxed or wrapped into objects of wrapper class.

  1. Primitive type is taken as argument

As name suggests, data of primitive type is taken as argument here, and compatible object of wrapper class is returned. This method looks like;
“public static return_type valueOf (primitive_type)”

Note: here, return_type can be any class of wrapper.

2. String type is taken as argument

As name suggests, data of straing type is taken as argument here, and compatible object of wrapper class is returned. It sends a NumberFormatException in case of invalid numerical value of string. As it is not possible to convert string into character, hence, this method is not applicable with character wrapper class. This method looks like;
“public static return_type valueOf(String s) throws NumberFormatException”
Note: here, return_type can be any class of wrapper except character wrapper class.

3. String and int type, both taken as argument at once

As name suggests, this method incorporates two arguments; a valid numerical value of string type which is changed into object of wrapper class, and another argument of int type showing the base or radix of numerical value.

This method also throws/sends NumberFormatException in case of invalid numerical value. Remember that, the method discussed here is only applicable with a few wrapper classes which include Long, Short, Integer and Byte. This method looks like;
“public static return_type valueOf(String s, int radix) throws NumberFormatException”
Note: here, return_type can be any allowed wrapper class.

Number Class

A number class is an abstract class from package Java.lang. It is the super class of all wrapper classes can be converted to their respective to the primitive types.

Auto-Widening

In java, the term widening is referred to passing of a data type of lower size to a data type of higher size. In java it is performed automatically by the compiler. For example, Parsing short data type into data type of int.

Example:

Int i=10;
 long l = i; 
// this will automatic do type conversion
 float f = l; 
// this will automatic do type conversion System.out.println("Int value "+i); System.out.println("Long value "+l); System.out.println("Float value "+f)

Result:

Int value 10
Long value 10
Float value 10.0

Auto-Boxing

Autoboxing converts the primitive types to their corresponding wrapper class by making that primitive type into an object. This is automatically done by the Java compiler.
Autoboxing is applied by the Java compiler when:
In a method the primitive type is passed as the parameter. This method requires an object of the wrapper class.

A variable of the corresponding wrapper class is assigned the primitive type.

Example:

int a=10;
Integer b=a; 
//converting int type into Integer wrapper          this is autoboxing 
Integer c = new Integer(5);
System.out.println(a+" "+b+" "+c);

Result:

10 10 5

Auto-Upcasting

Upcasting usually refers to go from specific to general. It is a type of typecasting in which there is casting of subtype to a supertype. This is automatically done by the Java compiler.
For Example In parent child relation:
Lets suppose Child is the inherited class of parent class if we create object like:

Parent p= new Child();
obj1.show();

From above code, There is upcasting from child class to parent class.

Leave a Reply