Convert String to Primitive:

Inside every wrapper class there is one method called parseXXX( ) except Character wrapper class. It will converts the String numric value to corresponding primitive equalent value. Example if you want to convert the following String “123” value to int type then we should use code like bellowInt intVal = Integer.parseInt(“123”);
in parseXXX() method “XXX” means it specifies corresponding primitive type like int,float,double etc.. example Float.parseFloat(“12.34”); Double.parseDouble(“30.56”) etc….

Here input String value for parseXXX() should be numeric else we will get java.lang.NumberFormatException same as above.

Output:

Explanation:

If we pass any derived object to the sop ,then it invokes toString( ) method automatically from that corresponding class and the result of the toString( ) will be printed into the console.

From the above program line no. 11&13 are equivalent to the line no 12&14 correspondingly .After compilation line no. 11&13 also convert equivalent to line no 12&14 but here from line no 11&12 we will get the o/p as state of the wrapper class object but from line no. 13&14 we will get o/p like ClassName@hexa decimal representation of memory location

Why because in all wrapper classes, toString( ) method get ovverided so it works based on the state of the object but in class A we are not overrided toString( ) so it works according to object class toString( ) method

In line no. 19&20 compilation fails because in line 19 we can’t assign class A object to String because while assignment toString( ) method won’t invoke.

Line 20 is also same as line no 19 here we are assigning integer object to string type i.e.,String s4=i. we should assign same and not other type.

Output:

Hello World!
Object class.
10
Long class.
True.

Explanation:

In the above program , System.out.println(guys.test(i)); is a calling statement for the test() method with Integer wrapper type inside “class Abc”.So first jre will look for test method with Integer type only.

But there is no test() with Integer type inside “class Abc”, Then JRE will look for super type of Integer i.e (Number class or Object class) .InSide Abc class there is Test() method with Object class ,So it will come for execution.

Then it executes test() with object type inside “class Abc”.

System.out.println(guys.test(hello)); is a calling statement for the test() with” Long type”,as the “class Abc” consists of test() with Long type just execute it. So it doesn’t looks for the super type of Long type.

About Java Padho

Einstein said, "It is the supreme art of the teacher to awaken joy in creative expression and knowledge." Great teachers are scarce, as they hold nothing back and give everything away. Javapadho is the brainchild of such an extraordinary teacher, i.e. Mr. Praveen Kumar Chandaliya, the founder of SDJ Infosoft.