Wrapper Classes
Wrapper Classes
Wrapper classes
________________________________________________________________
Wrapper classes are the some inbuilt classes in java which is used for perform
conversion between primitive type to reference type and reference type to
primitive type.
if we think about diagram we have two variable one is type of long and one type
of integer means we have statement long int a; it required 8 byte memory and int
b=100 it required 4 byte of memory and we have statement a=b means we
package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args) {
long a;
int b=100;
a=b;//implicit conversion
System.out.println("A is "+a);
long c=200;
int d;
d=(int)c;//explicit conversion
System.out.printf("D is %d\n", d);
}
}
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 2
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
Implicit or explicit conversion get failed when we try to convert primitive type of
value to reference type and reference type of value to primitive type.
String s="1234";
int a=(int)s;
If we think about above code here implicit or explicit conversion get failed.
Because we have s is reference type of string and int is primitive type so we
cannot reference primitive type directly so here we get compile time error
And if we want to solve this problem java provide some special type of classes
which help us convert reference to primitive or primitive to reference called as
wrapper classes
If we think about above diagram we have class hierarchy in this hierarchy we have
Number class which is parent of all numeric classes
Basically Number is abstract class and it contain some abstract method name as
xxxValue() which help convert numeric reference value to primitive type value.
package org.techhub;
int a=100;
Integer b=a;//auto boxing.
b) auto unboxing : auto unboxing means when we convert any reference value
to primitive value called as auto unboxing.
Integer b=200;
int d=b; //auto unboxing.
package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args) {
int a=100;
Integer b=a; //auto boxing
System.out.printf("B=%d\n",b);
Integer d=500;
int e=d;//auto unboxing
System.out.println("E = "+e);
}
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 5
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
}
Auto boxing and auto unboxing get failed when we have different type of
reference and different type of primitive in this case Number class comes in
picture means Number class provide some inbuilt method to which help us to
convert different type of reference or different numeric reference in different
type of primitive and different primitive in different type of reference.
Float a=5.4f;
int b=a;
If we think about above code we there is auto unboxing possible because we have
Float type of reference and int type of primitive data type in this case we can use
Number class methods i.e xxxValue()
int intValue(): this method can convert any numeric reference value to primitive
type integer .
float floatValue(): this method can convert any numeric value in float primitive
type.
double doubleValue(): this method can convert any numeric reference value in
double primitive type
long longValue()
short shortValue()
etc
package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args)
{ Float a=5.4f;
int b=a.intValue();
System.out.println("B is "+b);
Double d=6.5;
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 6
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
long l=d.longValue();
System.out.println("L is "+l);
}
}
valueOf(): this method is used for convert any primitive type value to reference
type value it is a static method present in every wrapper class.
package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args) {
int a=100;//it is primtive type of data
Integer b=Integer.valueOf(a);//convert primitive type a in to reference type b
System.out.printf ("B is %d\n",b);
}
}
Example: we want to convert integer type of primitive data in to String reference
type.
package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args)
{ int a=123456;
String str=String.valueOf(a);
System.out.printf("String is "+str);
}
}
String.valueOf(a) : this method help us convert primitive type of integer in to
string format.
parseXXX(): this method help use convert any string type of data in to primitive
type of data.
Example: we want to convert string to integer
String s=”12345”;
int b = Integer.parseInt(s);// convert string in to integer
package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args)
{ String s="124566";
int b=Integer.parseInt(s);
System.out.printf("After conversion of integer %d\n",b);
float c=Float.parseFloat(s);
System.out.printf("After conversion of float %f\n",c);
}
}
String class
____________________________________________________
Q. what is a String?
String is immutable class of java. Immutable means once we initialize value never
change later called as immutable as well as string final class.
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 8
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
Means if we create object of string class then we cannot modify its value later
once we initialize it.
Note: in the case of java every ““consider as string object.
If we want to create string object we have two ways
a) Using initialization of string :
Syntax: String ref =”value”;
Here “value” is actual string object and ref is string reference variable.
Example: String s=”good”;
b) Using new keyword:
Syntax: String ref = new String (“value”);
Example: String s = new String (“good”);
Q. what is diff between String creation using simple initialization and using a
new keyword?
_____________________________________________________________
When we create string using initialization ““then string object created in string
pool constant section and if we create a string using a new keyword then string
object created in heap section of memory
so when we create string using initialization technique if we have two string with
a same value then JVM create only one object for that string and share its address
with a different reference variable and if we create string using a new keyword
then JVM create new string object every time in memory means we have two
different string object with a same value
If we want to prove above diagram practically then you can print hash code of
both strings shown in following example.
package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args) {
String s="abc";
String s1="abc";
System.out.println("Address of s "+System.identityHashCode(s));
System.out.println("Address of s1 "+System.identityHashCode(s1));
}
}
Output:
Address of s 123961122
Address of s1 123961122
If we think about above diagram we have same hash code with two different
reference variable s and s1 so we can consider we have single object in memory
and its reference share by two different string objects.
If we want to work with a string we have following constructors and some inbuilt
methods provided by string to us.
Constructor of String
_____________________________________________________
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 11
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
package org.techhub;
import java.util.*;
public class ConversionApplication {
static String s""; //String s = new String();
public static void main(String[] args) {
System.out.println(s);
System.out.println(s.length());
}
}
2) String ref = new String(“Value”): this constructor help us to create string object
with a new value in memory.
package org.techhub;
import java.util.*;
public class ConversionApplication {
package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args) {
String s=new String("Good");
System.out.println(s.length());
}
}
char charAt(int index): this method is used for return character from a specified
index of string.
Source:
package org.techhub;
import java.util.*;
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
String s="abcmnopqrabc";
LinkedHashMap<Character,Integer>map = new
LinkedHashMap<Character,Integer>();
for(int i=0; i<s.length();i++) {
Integer count=map.get(s.charAt(i));
if(count==null) {
count =new Integer(0);
}
++count;
map.put(s.charAt(i), count);
}
Set<Map.Entry<Character, Integer>> set=map.entrySet();
for(Map.Entry<Character, Integer> d:set) {
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 15
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
if(d.getValue()>1) {
System.out.printf("%c-----
>%d\n",d.getKey(),d.getValue());
}
}
}
}
Example: WAP input string and separate digit from a string and calculate its sum
“abc1234mnopq567”;
1+2+3+4+5+6+7
Example:
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
Scanner xyz = new Scanner(System.in);
System.out.println("enter string data");
String s=xyz.nextLine();
int sum=0;
for(int i=0; i<s.length();i++) {
char ch=s.charAt(i);
int asc=(int)ch;
if(asc>=48 && asc<=57) {
sum=sum+(asc-48); //49-48=1
}
}
System.out.println("Sum is "+sum);
}
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 16
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
}
String [] split (String): this method is used for split data from a string using a
some specified character.
Example: WAP to count the every world length from a string and avoid duplicate
avoid
Example:
Source code
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
String str="good morning good afternoon india good evening india";
LinkedHashMap<String,Integer> map = new
LinkedHashMap<String,Integer>();
}
}
Note: here we get output small good after conversion because toUpperCase()
function not convert lower case string to upper case because string is immutable
if we use any function of string then every function of string create new object
every time in memory and after that perform operation
If we think about above code we have two objects in memory first String
s=”good” and second is String s1=”GOOD”means here s not modify its own value
so we can say string is immutable
Example: WAP to convert lower case string to upper case without using
toUpperCase() in java
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
String s="good";
String s1="";
int l=s.length();
for(int i=0; i<l;i++) {
char ch=s.charAt(i);
if(ch>=97 && ch<=122) {
char c=(char)((int)ch-32);
s1=s1+c;
}
}
s=s1;
System.out.println(s);
}
}
String toLower(): this method help us to convert upper case later to lower case.
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
String s="GOOD";
s=s.toLowerCase();
System.out.println("After conversion "+s);
}
}
String trim(): this method help to remove white spaces at beginning of string and
ending of string.
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
String s=" GOOD";
s=s.trim();
System.out.println(s);
}
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 21
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
boolean endsWith(String): this method is used for search data from string using a
some specified character at ending this method return true if character ends with
a specified data otherwise return false.
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
String s = "Good Morning";
boolean b = s.endsWith("ing");
System.out.println(b);
}
}
Example: Suppose consider we have array of string which contain 10 string data
and we want to find strings those ends with sh
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
String []str=new String[]
{"Ganesh","Manesh","Sandeep","Raghav","Ram","Ramesh","Sangram","Govind","
Krushna"};
for(int i=0;i<str.length;i++) {
boolean b =str[i].endsWith("sh");
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 22
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
if(b) {
System.out.println(str[i]);
}
}
}
}
String substring(int startindex,int endindex): this method is used for extract data
between two specified index from a string.
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
String str="good morning india";
String s=str.substring(5,12);
System.out.println(s);
}
}
alStopWord.add(s);
}
String steam[]= new String[] {"ing","noon","ry"};
ArrayList<String> streamColl=new ArrayList<String>();
for(String s:steam) {
streamColl.add(s);
}
String []str= new String[]
{"good morning india","good afternoon india","inida is my contry",
"i am indian","i like india"};
for(String s:str) {
String words[]=s.split(" ");
String w="";
for(int i=0; i<words.length;i++) {
if(!(alStopWord.contains(words[i]))) {
w=w+words[i]+" ";
}
newArrList.add(w);
}
}
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 24
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
}
}//good morn india , good after india ,india county , indian india
Assignment
__________________________________________________________
WAP to input string and reverse words from a string without using reverse()
function
Note can use map
int indexOf(String): this method is used for return index of particular data from a
string if data not found return -1.
Note: we can use this method for search data from a string
When we get -1 then we consider not present in string if not get -1 then we can
consider data present in string.
package org.techhub;
}
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 25
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
String concat (String): this method is used for combine two strings and generates
new third string from it.
package org.techhub;
String s1 = "good";
String s3=s1.concat(s2);
System.out.println(s3);
}
}
boolean equals(Object): this method can compare two strings with each other
using its hashcode
package org.techhub;
String s1 = "good";
boolean b = s1.equals(s2);
if(b) {
System.out.println("Equal");
}
else {
System.out.println("Not Equal");
}
}
}
int compareTo(String): this method help us to compare the two strings with each
other by using lexical order or by comparing single single character from both
strings if all character are equal return 0 and if any one character is different then
return ascii code difference of first mismatch character.
Source code
package org.techhub;
package org.techhub;
public class SearchStringApplication {
public static void main(String[] args) {
String s1 = "good";
String s2 = "GOOD";
int result = s1.compareToIgnoreCase(s2);
if(result==0) {
System.out.println("Strings are equal");
}
else {
System.out.println("Strings are not equal");
}
}
}
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 28
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433
Note: StringBuffer and StringBuilder always allocate its memory in heap section of
RAM means we must be use it using new keyword we cannot use it using
initialization technique of string.
void insert(int index,int data): this method can add integer type of data in string
on specified index.
void insert(int index,float data): this method cann add float type of data on
particular index.
it is also overloaded method with all data types in string.