0% found this document useful (0 votes)
28 views31 pages

Wrapper Classes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views31 pages

Wrapper Classes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

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.

There are two types of conversion in java


________________________________________________________
1) Primitive type conversion: primitive type of conversion means if we perform
conversion between simple types called as primitive type of conversion.

There are two types of conversion in primitive type of data


a) Implicit conversion: implicit conversion means those conversion
perform automatically by compiler called as implicit conversion
When we put larger type of data at left hand side and smaller type of data at right
hand side then compiler is able to perform conversion automatically called as
implicit conversion.

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

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 1


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

initialize 4 byte of block in 8 byte so compiler easily accommodate 4 byte space in


8 byte automatically called as implicit conversion.

b) Explicit conversion: explicit conversion means those conversions


perform manually by programmer called as explicit conversion.
means when we try to store large value at right hand side and smaller value at left
hand side then compiler is unable to perform conversion automatically then
programmer need to perform conversion manually called as explicit conversion.

if we want to solve this problem we need to perform conversion manually we


need to convert long b to type of integer like a =(int)b;//explicit conversion

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

Wrapper class’s hierarchy

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.

public abstract class java.lang.Number implements java.io.Serializable {


public java.lang.Number();
public abstract int intValue();
public abstract long longValue();
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 3
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

public abstract float floatValue();


public abstract double doubleValue();
public byte byteValue();
public short shortValue();
}
public abstract int intValue(): this method is used for convert any numeric
reference value to primitive type value.
public abstract long longValue(): this method is used for convert any numeric
reference value to long type value
public abstract float floatValue(): this method is used for convert any numeric
reference value to primitive type float value.
public abstract double doubleValue(): this method is used for convert any
numeric reference value to primitive type double value.
public byte byteValue(): this is used for convert any reference value to numeric
byte primitive type.
public short shortValue(): this is used for convert any reference value to numeric
short primitive type.

package org.techhub;

public class ConversionApplication {


static Integer a;
static int b;
public static void main(String[] args) {
System.out.println("A is "+a);
System.out.println("B is "+b);
}
}
if we think about above code we have output is
A is null
B is 0
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 4
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

because a is reference of Integer class and b is variable of primitive type of value.

There are two types of conversion wrapper classes


______________________________________________
a) auto boxing: auto boxing means when we convert any primitive value to
Reference value called as auto boxing.

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.

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 7


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

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

Example: we want to convert string to float type.


String s=”4567”;
float b=Float.parseFloat(s);
Note: this method may be generate NumberFormatException at run time

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, StringBuffer and StringBuilder


_______________________________________________________________

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

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 9


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

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.

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 10


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

Example with a different object in memory using a new keyword?


package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args) {
String s=new String("abc");
String s1=new String("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 942731712
if we think about above output we get two different hashcode of s and s1 even
values of s and s1 are same so we can consider we have two different objects in
memory with a same value.

Q. what is string pool constant?


The Java string constant pool is an area in heap memory where Java stores literal
string values. The heap is an area of memory used for run-time operations. When
a new variable is created and given a value, Java checks to see if that exact value
exists in the pool or not if same value present in pool then not allow to create
new instance of that value in pool just access address of previous value present in
pool and assign to new reference variable

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

String(): create empty stirng .


package org.techhub;
import java.util.*;
public class ConversionApplication {
public static void main(String[] args) {
String s = new String();
System.out.println(s);
System.out.println(s.length());
}
}
If we think about above constructor we have String object with empty value and
So s not display anything on output and s.length () return 0 because string not
contain any character or data

Example: what will be output of given code?


package org.techhub;
import java.util.*;
public class ConversionApplication {
static String s ;
public static void main(String[] args) {
System.out.println(s);
System.out.println(s.length());
}
}
If we think about above code we have reference variable s which is null and in
main method we have statement System.out.println(s) which is print null value
and we have statement s.length() this statement is responsible for generate
NullPointerException because we have string without data or reference with null
value so we cannot perform any operation on null so we get exceptions
so in this case you can create string as blank by assing “” in string variable here
“” consider as string object but it is stored in string pool constant area of heap.
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 12
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

shown in following code.

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 {

public static void main(String[] args) {


String s = new String("good");
System.out.println(s);
}
}

3) String ref = new String(char[]): convert character array in to string object.


package org.techhub;
import java.util.*;
public class ConversionApplication {

public static void main(String[] args) {

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 13


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

char ch[]=new char[] {'g','o','o','d'};


String s = new String(ch);
System.out.println(s);
}
}
Methods of String in java
______________________________________________________
int length(): this method help us return length of string.

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.*;

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 14


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

public class StringApplication {


public static void main(String[] args) {
String s="good morning";
int l =s.length();
for(int i=0; i<l;i++) {
char ch=s.charAt(i);
System.out.printf("ch[%d]---->%c\n",i,ch);
}
}
}

Example: WAP to count repetitive character from string.


abcmnopqrabc

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>();

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 17


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

String []words=str.split(" ");


for(int i=0; i<words.length;i++) {
Integer len =map.get(words[i]);
if(len==null) {
len = new Integer(0);
}
len=words[i].length();
map.put(words[i], len);
}
System.out.println("Display all words and its length");
Set<Map.Entry<String, Integer>>data=map.entrySet();
for(Map.Entry<String,Integer> s:data) {
System.out.println(s.getKey()+"------------>"+s.getValue());
}

}
}

String toUppdareCase(): this method help us to convert lower case string to


upper case.
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
String s="good";
System.out.println("Before Conversion "+s);
s.toUpperCase();
System.out.println("After conversion "+s);
}
}
Output:
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 18
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

Before Conversion good


After conversion good

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

Q. what will be output of given code?

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 19


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

if we think about above code we have statement String s=”good”


System.out.println(“Before conversion “+s); good and we have statement
System.out.println(“Hash code of s “+System.identityHashCode(s));
s=s.toUpperCase(); here this statement convert your string to upper case i.e
GOOD and our reference s points to new memory object i.e 11000 in our diagram
so JVM found small good object not use by any reference so JVM delete it from
memory means perform garbage collection with object.

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) {

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 20


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

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);
}
}

Example: Suppose consider we have some paragraph in String and we want to


remove stop word from a string and perform steaming operation on string.
package org.techhub;
import java.util.*;
public class StringApplication {
public static void main(String[] args) {
ArrayList <String>alStopWord = new ArrayList<String>();
String stopWords[]=
{"is","was","then","a","that","where","are","but","am","like","my","i"};
for(String s:stopWords) {
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 23
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

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"};

ArrayList <String>newArrList = new ArrayList<String>();

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);
}

System.out.println("After remove stopword");


for(String data:newArrList) {
System.out.println(data);

}
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

WAP to input string and remove duplicated character from a string


WAP to find the permutation of string abc (geeksforgeeks)

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;

public class SearchStringApplication {


public static void main(String[] args) {
String s="good morning india";
int index=s.indexOf("morning");
if(index!=-1) {
System.out.println("data found");
}
else {
System.out.println("Data not found");
}

}
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;

public class SearchStringApplication {


public static void main(String[] args) {

String s1 = "good";

String s2 = " bad";

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;

public class SearchStringApplication {


public static void main(String[] args) {

String s1 = "good";

String s2 = " bad";


Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 26
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

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;

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 27


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

public class SearchStringApplication {


public static void main(String[] args) {
String s1 = "good";
String s2 = "gaodss";
int result = s1.compareTo(s2);
if(result==0) {
System.out.println("Strings are equal");
}
else {
System.out.println("Strings are not equal");
}
}
}

int comareToIgnoreCase(String): this method can perform comparison between


two string without checking its case.

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

Q. what is diff between string comparison using a equals() and compareTo()?


When we compare two strings with each other using equals() method then string
compare using its hashcode and when we compare two strings using compareTo()
perform comparison using lexical order or character by character and if all
characters of strings are equal return 0 otherwise return diff of fist mismatch ascii
code.

StringBuffer and StringBuilder


_________________________________________________________________
StringBuffer and StringBuilder are the mutable classes of java means we can
modify its value once we initialize it.

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.

Methods of StringBuffer and StringBuilder


void append(int): this method is used for add integer type of value at end of
string
void append(float): this method is used for add float type of value at end of string
void append(double): this method is used for add double type of value at end of
stirng.
Note: this method is overloaded with all data types in java means using this
method we can add any type of data at end 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.

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 29


GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

void delete(int startIndex,int endIndex): this method help us to delete data


between two specified index from a string.
Example:
package org.techhub;
public class SearchStringApplication {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Good Morning india ");
System.out.println("Before Append "+sb);
sb.append(2023);
System.out.println("After Append "+sb);
}
}
if we think about above code we not use any external variable at left hand side of
StringBuffer means we can say we change same object using append() method so
we can say StringBuffer is mutable class in java

Example: insert value on index using StringBuffer


package org.techhub;
public class SearchStringApplication {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Good Morning india ");
System.out.println("Before Insert "+sb);
sb.insert(5,"Hello");
System.out.println("After Insert "+sb);
}
}

Example of delete data between two index using a StringBuffer


package org.techhub;
public class SearchStringApplication {
public static void main(String[] args) {
Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 30
GIRI’S TECH HUB PVT-LTD- PUNE- 9049361265 , 9175444433

StringBuffer sb = new StringBuffer("Good Morning india ");


System.out.println("Before Delete "+sb);
sb.delete(5, 12);
System.out.println("After Delete "+sb);
}
}

Q. what is diff between StringBuffer and StringBuilder?


StringBuffer StringBuilder
StringBuffer is synchronized i.e. thread StringBuilder is non-synchronized i.e
safe. it means two threads cannot call not thread safe. It means two or
the methods of StringBuffer multiple threads call StringBuilder
simultaneously methods simultaneously
StringBuffer is less efficient than StringBuilder is more efficient than
StringBuilder StringBuffer
But In Thread Safety StringBuffer is StringBuilder not refer in thread safety
better than Stringbuilder
StringBuffer introduce by java in JDK 1.0 StringBuilder introduced by java in JDK
version 1.5 version

2) Reference type conversion: reference type conversion means if we perform


conversion between two different object types of data called as reference type of
conversion.

Visit: https://techhubindia.org/ Download App – GIRI’S TECH HUB Page 31

You might also like