Wrapper Classes In Java
We know that when we pass any "Command Line Argument" through the command prompt and which is directly sending to the main method ( main ( ) ), It is available in the form "Array of object of String class" (String args[]). The string type of data we cannot perform any numerical operations.
So hence it is designed to convert string type data into numerical / fundamental type data by using wrapper classes.
So hence it is designed to convert string type data into numerical / fundamental type data by using wrapper classes.
Define Wrapper class & Explain its purpose
For each & every fundamental data types their exist one pre-defined class then it is also known as "Wrapper classes". The purpose of Wrapper class is that to convert String data type into numerical / fundamental data type values.
The following table gives fundamental data type with corresponding wrapper class name & conversion method which will convert string type of data into fundamental type data.
In generally each & every Wrapper class contain the following method whose purpose into convert string data type into numerical / fundamental data type.
Wrapper class name
🔺
Public static xxx parse xxx ( String )
Here note that xxxx represents any fundamental data type( Expect char data type ).
Points to Remembers to use Methods In a Class.
1. Beside the what type of method (instance or static method) We are using and presenting which class.
2. Beside what type of parameters a method is taking and pass it values return type.
3. Beside what type of method is returning and hold the result in a variable of return type.
EX:- Math ↴
int sum( int,int )
The below example will shown how to useful in the wrapper classes in java.
Math m = new Math( );
int x = m. sum ( 10,20 );
The below example will shown how to useful in the wrapper classes in java.
Math m = new Math( );
int x = m. sum ( 10,20 );
System.out.println(x) = //30.
EX 1:- String s = "100";
S = s + 2
S = s + 2
System.out.println ( S ); //1002 (invalid)
Here the value of s is declared as "100" , In java if any values put into a "100" then it called as String. So String type of values as convert into fundamental type by using Wrapper class.
int a = Integer.parseInt(s) // wrapper class name
a = a+2; // valid
System.out.println(a); // 102
EX : 2 String S1 = " 10.5f";
S1 = S1+2;
System.out.println( s1 ); // 10.5f2 ( invalid )
float x = Float. parseFloat ( s1 ); // wrapper class name
x = x+2; // valid
System.out.println(x); //12.5f
EX : 3
String s [ ] = { "10", "20"}; 10/20
String s [ ] = { "10", "20"}; 10/20
System.out.println( s[0] + s[1] ); //1020 (invalid)
int x = Integer.parse Int ( s[0] ); // wrapper class name
int y = Integer.parse Int ( s[1] ); // wrapper class name
int z = x+y;
⧭ Here we will discuss more examples on wrapper class.
⧭ Here we will discuss more examples on wrapper class.
System.out.println ( "sum="+z); //sum=30 (valid)
System.out.println("sum="+x+y); //sum1020
System.out.println("sum="+x+y); //sum1020
System.out.println ("sum="+x+y); //sum=30
System.out.println(="mul"=+x+y); //mul=200
System.out.println ("sub="+x-y); //error
System.out.println("sub="+(y-x)); //sub=10
System.out.println("div="+x/y); //Div=D
System.out.println ("MOD="+y/x); //mod=0
No comments:
Post a Comment
Thanks for visiting my site..