right so in our previous class uh we have seen
how to work with the streams type and different methods available in the string and how can
we operate strings so today we'll see some more examples and how can we use string Methods
to solve the different type of uh problems okay so the first example so most popular uh program
uh in interview which is uh reverse string how to reverse a string so this is very popular
and famous program so we can do this in many ways and I'll show you three to four different uh
methods to reverse a string and by using logic by writing the logic we can do it and by using uh
string buffer and string Builder there are some predefined classes are there and we can also
use some buil-in methods to reverse this but most of the people uh don't know like there is
no direct method available in Java to reverse a string okay so like other methods are there like
L the concat substring so we have seen different type of methods we can use all those methods on
the string and we can perform different type of operations but if you want to reverse a string
so there is no direct method available in the string class especially okay so if you want
to reverse a string we have to write a logic for that so there is no direct method available
but some programming languages there is a direct method available so directly can say string do
reverse it will automatically reverse the string but especially in Java there is no direct method
available for string reverse especially in string class but there are other classes are there and
they are having reverse method but especially string class we don't have a reverse method to
reverse a string okay so first let us see how can we reverse this Str so what is reverse means
for example uh if if I give some value like this after reversing it should be like this from the
last letter it should print e m c uh l e w like this so this is reverse string so we have to
get this reverse string through program okay and uh this string can be dynamic so not only the
same string every time whenever you provide a new string you should able to reverse that particular
string so this is the problem state okay so now we will try to solve this problem in multiple
ways but the first one we'll see how can we solve this problem by using logic so how can we
implement this logic reverse string okay so let us try to understand the logic first and then we
will try to implement the program so how to uh reverse a string what is the logic behind this
so just think about about generally and don't directly jump into program let me take this let's
say welcome this is sample string I'm taking now I want to reverse this so to reverse this the first
and most important thing is we have to capture the last character from the string okay so we have
to capture let's say we will take one variable called reverse string okay some temporary variable
this is also string variable we have to take and we have to capture the last character and concat
that character to this existing variable e then again get the last character M next character
is M you can put here and next character C you can add here next character C next character L
next character e next character w What does it mean so we have to read each and every character
from the end okay we have to read each and every character from the end at the same time add that
character to this existing variable we will take another variable called reverse and whichever
character you are capturing from the end concat that character to the existing character like
first e then m o c l e w like this but how many times we have to read characters like this till we
reaches zero as an index right so how many times we have to go back like this there should be some
cut off point there should be some condition right so we need to go back like this till the length of
the string should become zero right so initially when we start Here length of a string will be what
seven and after getting this variable then we will go to the next one so that means we have to reduce
this length by minus one till it becomes zero okay so this we have understood how we have to capture
the characters from the end and how many times we have to repeat this from 7 to Zer but we don't
know how much length it is this is the dynamic but how many times we have to repeat this till
length should be zero okay length function we have to use length method one is a method we have
to use that's the one thing second how to extract the last character or one by one how to extract
the characters first last character then next then next next how to parall we have to do two things
we have to go uh we have to repeat and we have to reduce the length by one one by one at the same
time we have to capture the character from that particular position so what is another method we
have to use yes carot method so this is the method we have to pass index number here accordingly
we can capture the character so character of I will capture the character and that character we
have to add to the reverse variable and uh then we have to reduce the number till length becomes
zero okay so this is the logic now same thing we have to implement through program right so two
methods we have to use one is length other one is character length is for what how many times
we have to repeat this Loop for that we need length and what is character of I because
every time we have to extract the character from the string so for that we need character
of I okay let's uh implement the same logic through program go to eclipse and let's create
new package for today let's say date 9 today finish if you understand the logic implementation
is very very easy and let me take the new class reverse a Stringer main method let finish okay
so there are multiple approaches as I said right so this is the first approach and say approach
one so let me take one string variable string uh yes equal you can take this as an input also
from the keyboard later okay now I'm taking one string so after reversing the string I will store
that reverse string in another variable so for that also I'm taking one reverse varable this
is a uh empty string I'm not keeping anything in this empty string single double quotes empty
no value in this just a reverse variable okay and this will store the reverse string later now
the first thing we have to write one F Loop okay in the F loop three things are required so
initialization condition and incrementation so we need to think about these three so what could
be the initialization what could be the condition what is an incrementation so if I look at this
particular string we have to start from here we have to start from end and we have to go back like
this so obviously what is the starting value of I starting value of I how we will know that so here
the based on the string we are saying seven but tomorrow this string is got changed suppose this
tomorrow the string becomes only three characters and 10 characters then it keeps changing right
now we have to dynamically get the length of a string right okay so when you get the length of a
string what it will return seven okay length of a string is seven but actually when you read the
characters we have to use index from zero right index index car method index start from zero
or not car method carot method index is always start from zero but if you look at the length
the length will be counted from one itself 1 2 3 4 5 6 7 okay length means size of a string it is
counted from one but actually in the car method we use indexing concept right based on the index
we will get the character so index will start from what car method zero so what we have to do
is from the first character starting point int IAL to S do length this will give you total number
of characters but we have to start index from zero right so minus one we have to say Okay minus one
we have to say why we should say minus one here because length will give you size of the string
it will not count from zero but car method count from zero index so we have to say minus one so I
equal to s dot length s is a string variable so this will give you total length of a string minus
one so currently where are we now and I say minus one so this is my string okay just a second okay
so this is my string okay so Len s do length minus one what does it mean where are we now so length
is 7 7 - 1 6 six index is what here this is a six according to the Carro method what is an index of
e what is an index of e six only right so how to get six Here length minus 1 length is a 7 7 - 1 6
so according to the carrat method we have to start index from zero but we have to read the characters
from the end so the index of the last character is what obviously six so how many times we have to
reduce like this till this value I value becomes zero and zero character also we required right
we need to capture this zero character also so the condition should become I greater than 0 I
greater than Z equal to I greater than or equal to zero so now let us try to implement this so I
= to S do length minus1 I greater than zero and even zero character also we need so I greater
than or equal to zero and then I ++ or iusus iusus because we want to reduce this six next five
next four next 3 2 1 zero and every time we are index is changing right as soon as the index is
changing we have to capture the character also how to capture the character yes dot carat of I yes
dot carat of I so in every iteration I'm getting the character one by one so let us try to print
this and see how it is going to print system do. pint let's keep this in the print statement
system. print and S do carat of I okay now when I just execute it will just print all the characters
from The End right you can see e m o c l e w so we able to get all the characters or not from the end
yes but now what is Our intention we have to add this character in every iteration we have to add
this character to this reverse variable okay so how we can add yes it's a concatenation obviously
so instead of print Ln I can simply say reverse equal to every character we have to add to the
existing character so reverse equal to reverse you can use concat method directly or you can use
plus operator anything is fine okay reverse plus s do karat of I so then this Loop will be completed
after completion of this Lo once you exited from the F Loop if you print this reverse variable you
will get the reverse string can see here reverse string is can just print reverse variable okay
very simple only two methods we have to use so now we can see this is our reverse string so you
can change the value so you can just make it as a some some different value so let's see whether
it is reversing or not yes it is got re reversed so now we can change the string dynamically
and whichever string you provided here this logic works okay so this is how we can simply
reverse a string by using two methods approach one first one is length and the second second one
is what car so by using these two methods we can do this this is the first approach and this is
most popular one so many people we already know this and another way is also there let me show
you so is this clear everyone so first approach how to reverse the string everyone so please
confirm in the chat box we will go to the next approach and no need to remember each and every
approach just remember at least one approach that is fine okay approach two so how to reverse a
string and I don't want to use carrot okay I don't want to use carrot method and also I don't
want to use length so these without using these two methods without using length and character how
can we achieve this that is a second approach let me comment this the second approach so without
using without using string Methods that's the second approach without using string method so
we don't use any string type of methods so how can we achieve this we want to reverse a string
but without using any string method carrot method length method we are not going to use so let
me tell you so string yes equal to welcome so this is my string variable now I want to reverse
this another logic is first we have to convert the string into character array character array
what does it mean so my string is this one this is my string variable right and this is a full
FL one string single variable and single value once you convert this string into character array
character array means what you will have one array variable and you will have multiple locations so
each character in a string will be stored in every location let's say w e l c o m e like this so we
will convert this string into character type of an array we will convert this string into character
type of an array now in this character type of array all the characters will be stored in the
different locations same order in whichever order we have written all the characters same order
we will store in the array we will basically convert the string into character array once it
is converted we can read this array characters from the end so first we will read e then m o c
l e w so as soon as you read the characters add those characters to the reverse variable first
read the last character e then M so then O then C then l e dou like this now this reverse variable
contains a reverse string so here we are not using any string related methods we are just converting
this string into array so once the characters are available in the array we can read them from
the end from top to bottom we can read right so how to read an array just by writing a looping
statement from the looping statement we can read the values from the beginning to end or end to
beginning so whichever direction you want you can read the values if you have an array right
but how can we convert the string into array how can we convert the string format to array
character array format so for that we have a a different approach very simple so what we can do
is first let us take one reverse variable nothing is there now the first step we have to convert
the string into character array variable so how can we convert we have to take one character array
like this Char type of an array equal to what is a string yes is a string yes dot there is one more
string method is there this is also string method but without using length and character method we
are achieving it there is a method called to care array this is also one of the string method s do
to car array so what this method will do is this method will convert string into array format okay
and this will return character type of an array so whatever characters we have in this string
variable these characters will be stored in this particular array this is called conversion this is
where we can convert the string into character so it will create array like this and it will create
multiple locations so these characters whatever is there in the string will be stored like this
W first position second position third fourth 5 Second like this it will store like this okay now
once you converted into array character array we can simply write a loop statement and read those
values one by one from the end now you guys can tell me how to read the data from the character
array from the end what is the starting point how to know the starting point so now we have to read
the data from array itself so this is my array so here these are the values now I want to read
this is a zero position first second third and from the last index we have to start reading the
data last index what is the last index how we will know the last index of this array yes in I equal
to a do length a do length means this will give the it will give the length of an array length
of an array but how to get the last index minus one when you say minus one it will give you the
last index that is the starting point now where we have to startop even even zero value also we
have to capture so I greater than or equal to zero and we have to reduce or increase I value
minus minus or plus plus minus minus every time we have to reduce the value this is the logic now
we have to extract that particular character based on the index how to extract the character from
the index a do a of I simply pass the I value so this will give you characters let us try to
print first so I'm just trying to print a of I value system print Ln a of I so now when you run
this you will get all the characters like this you can see it is just printed array variables
uh characters in array from the end so now what we can do we can concat or we can join these
characters for the one string variable every iteration so reverse equal to reverse plus a of I
so after After exiting from the for Loop if I just print reverse variable you can see the reverse
string you can say reverse reverse string is can just print reverse like this so this is another
logic without using length and carat I'm just running run as Java application yes now we get a
you get a reverse stream okay so this is approach two by converting or we can say we can put this
another way by converting string to pair array type okay so this is a second approach without using length is not POS because
this length is not a string method actually okay so here this length is different this is a string
method which will return the length of a string here this length is not a method actually this
is a variable which will give length of a array because if you don't know this length of an array
you don't know exactly where you have to stop so where you have to start right because this value
is dynamic tomorrow suppose if this if you know the string size now directly you can hardcode
this value here you can hardcode this value you can start from four but tomorrow if the string
is got changed you don't know the length so you don't know where you have to stop right so until
unless you know the size you cannot know where we have to start so that is a reason we have to find
length first of okay so what we are exactly doing I'm repeating again guys just focus on this it's
not a rocket sense it's very simple everybody's understood this particular step like line number
25 so this will convert the entire string into character array two car array is a method it will
convert the enter thing into character array after conversion you can see all the characters in a
character actually this concept is based on the previous topic itself it's not a new 0 1 2 3 4
5 six this is a index now these characters will be stored like this w e l c o m e like this now
character array is ready now what we have to do we have to read the data from the end like this
one by one at the same time we have to add those characters to the reverse variable so first we
have to capture the last character add it here then go back to the previous now get this add
it here now go back get this character and add here then again go back get this so like this
we have to go back in every iteration at the same time we have to read the characters add
them into reverse so same thing we are doing here reverse plus reverse is existing variable
plus a of I in every iteration we are getting the I value I value means what index first index
is six then five then four then three like this index is keep on changing it is reducing in every
itation so that it will give you the characters from that particular position and as soon as you
get the character from this we are attaching that reverse variable so once this Loop is completed
this reverse variable contains a reverse string I have also shown you by printing these AF of
values when I print a of I values it is printing all the characters from the end but as soon as
you get the character one by one we're adding them into reverse variable so finally the reverse
variable contains a reverse string but how many times this will Repeat based on the condition I
greater than or equal to Z that means as soon as I becomes minus one it will stop even zero value
also we have to capture so I say I greater than or equal to Z next time I value become minus
one then condition become false it will stop and where we have to start how we will know that
we are finding the total length of a array and then say minus one that will give you the last
index of an array and we are starting from there so starting point is what I equal to a do length
minus 1 a do length will give you total length of array minus one give you last index okay so
everybody's understood the second approach by converting string into array and then we will read
that array from the end as soon as you read the characters parall we will add those characters to
the reverse then finally we will get the reverse string so this is the second approach and these
two approaches are logic based so we have to implement this logic we have to write a f Loop
we are reading each and every character we are finding the length so this is the logic first two
approaches are totally based on the logic we have implemented two different approaches now I'm going
to show you two more different approaches without using logic directly by using buil-in methods we
can do it okay so the next approach approach three so let us comment so in Java normally if you
want to store a string we will create that variable as a string variable right so when
you create a string s equal to we can pass some value like this and similarly Java if you
want to store a string in a variable we have two more classes are there one is string buffer
class and this there is another one called uh string Builder class string Builder so just
like a string string buffer and string Builder classes also there in Java okay so just like a
string variable in string variable we are able to store the string value similarly if I create
any variable for string buffer I can store some value in it okay similarly if I create a string
Builder variable I can also store some string in this just like a similar way but string is a
different class string buffer is a different class string Builder is a different class in the next
examples I will tell you what is the difference between string string buffer and string Builder
but the purpose is almost the same the methods can be different whatever methods are there in
the string class the same methods may not be there in the string buffer and string Builder
and whatever methods are there in the string Builder and string buffer the same methods may
not be there in the string class but there may be some common methods are there in the string
and also string buffer and string Builder so just like a string class in Java we also have
string buffer and string Builder classes so by using them also we can store strings into a
variables okay remember this but the thing is in if it is a string we can directly store the
string value like this but if it is a string buffer we have to use a different notation for
example string buffer a equal to we have to use something called new keyword new string buffer of
then we have to specify the value in it like this and same thing for string Builder also string
Builder a equal to new string builder of then you can specify the value so this is the notation
which we have to follow even string also we have to follow the same kind of notation we can follow
that string is equal to new string of welcome you can also specify the string like this in yesterday
class I have told you how many ways we can declare the string but there is a difference in these two
uh in the next example I'll tell you what is the difference so similarly string buffer string
buffer Builder also we can use to define the strings okay so now let us try to do this string
buffer I will use and string Builder also I will try to use first let us say string buffer this is
an approach three using string buffer class using string buffer class how can we reverse a string
now first of all create a string variable by using string buffer class string buffer equal to
string buffer some string is required s is equal to new string buffer and here we have to pass
the value let's take one value like this okay string buffer yes equal to new string buffer
okay yes we can also declare like this string buffer okay I can say string okay buffer s equal
to welcome so you can see this is giving an error type mismatch cannot convert from string to string
buer so this is the problem okay in the string buer and the string Builder we cannot directly
specify the value like this only in the string it is possible but string buffer and the string
Builder we cannot right so that is the reason we should go with the first number so string but
in the string both are possible you can say string s equal to welcome you can put like this
or you can say string s equal to new string Str of welcome you can put like this but in the string
buffer in the string Builder it is not possible uh why because string buffer and string Builders
are mutable objects and string is a immutable object so what does it mean I will tell you in
the next examples okay for now just understand this much so string buffer s equal to new string
buffer and welcome now I have created string like this now if you just print a string value can
we print like this just I'm printing string yes when I run this you can see the S value is
what welcome same thing just like a string right so now we want to reverse this so to reverse this
particular string we know need to write any logic there is a direct method available in the string
buffer class that is s do reverse direct method so this will give you reverse string reverse string
as then s do reverse but in the string class we don't have a reverse method so that's the reason
we have to write a logic there but if you create a string variable by using string buffer you can
directly use reverse method so this will reverse a string so when I run this as a Java application
you can see this is our reverse string so this is a straightforward no logic nothing okay this is
approach three by using string buffer class same thing is applicable for string uh string Builder
also so this is approach four using string Builder this is also another class which is there in
Java so we can create a string variables by using string variable take the same thing instead
of string buffer you can use string Builder class let's come M this instead of string buffer let's
take string Builder class and here also it is a string buildup and reverse as equal to reverse
so reverse method is available even in the string Builder class also so if I execute this will
also able to reverse the string okay so first and second approaches we have to implement the logic
during the interview they will expect the logic not buil-in method okay so they will definitely
expect the logic and some people they will ask you they will put one condition so without using
length and character method how can we achieve this then you need to go with the second approach
okay but if anybody interview ask don't directly tell by using string buffer and string Builder
and don't tell through reverse method we can rever reverse string reverse method is not available in
the string class 90% of the people will tell in correct answer for that the reverse method is not
present in the string class which is present only in the string buffer and string Builder classes
so that's the reason we're able to directly use it and we are able to reverse the string but
other than this if you still want to use only string variable then we have to write a logic
for that in the next examples I will show you what is the difference between string buffer and
string Builder okay we will compare string string buffer string Builder there is a specific example
I will show you what is the difference okay so for now you guys understood how to reverse a string by
using these four methods four different approaches I have told you first two approaches are most
important okay if anybody ask in interview don't directly tell sting buffer and sting Builder they
think they don't you don't know anything okay the logic is important if anybody ask you any any
problem during the interview always answer those questions in the form of logic not in the form
of buil-in methods okay then only your logical skills will project which is most important so
always try to explain the first logic or if they put any condition like without using length and
character then go with the second approach okay second approach is also correct by using logic so
this is how we can reverse a string so we will try to understand what is the difference between
string strting buffer string Builder so we will have one more example for that so before
that we need to understand something called string mutability immutability this is some
different kind of a concept but testing point of view automation point of view is not much
important for developers it is most important but still we need to understand the difference
what is mutability what is immutability if you understand this concept then you can understand
the difference between string string Builder and string buffer okay so let us try to understand
difference between immutability immutable and mutable these are these are two different terms
mutable immutable and mutable so immute uh mutable means what change we can change we can
change immutable means what we cannot change so immutable means we cannot change change is
not possible mutable means we can change right mutable means we can change change is possible
that is the meaning of mutable and immutable in programming concept so why we need to discuss this
mutable and immutable I'll tell you why because string is a mutable object whereas string buffer
and string Builders are the uh sorry string is a immutable object whereas string Builder string
buffer is a immutable mutable objects okay so they are the uh mutable objects I'll tell you how it is
with simple example we'll understand mutable and immutable mutable versus immutable first
understand the terminology and the meaning and then we will try to understand the
differences so what is mutable what is immutable what is mutable what is immutable
so first let us talk about mutable what is mutability so to demonstrate this mutable
I will take a simple example I say take one integer aray I'll store some data in it
some data randomly I'm storing some data like this okay so Random data have taken now if
you want to sort this particular array I have told you one method direct method what is that
array dot array dot how to sort this Aras Dot sort right Aras dot sort of eight this will sort
right so before sorting I'm just trying to print array values so this is for what before sorting
arrays dot sorry how to print array elements I can say array what is an arrays dot two string of
a so this will print array elements after sorting again I'm printing the same array so here I'm
printing the array elements after sorting again I'm printing the array elements just observe this
when I run this as a Java program so these are the first elements and now it is successfully
softed so this is our expected output now if I just observe this this particular method Aras do
sort so this method sort method is changing this array elements or not because before sorting when
I print array these are the elements I have and after sorting I'm again printing the same array
now I'm getting the new values a different order what does it mean this particular method is
changing this array or not please respond is it changing or not because before when I print
array we are getting these values after applying this method again if I print an array we are
getting the different values so what I'm asking is this particular method is changing this array
or not yes it is changing means this is mutable this is mutable arrays not sort this is a mutable
means where able to change this method is able to change the verginal array elements verginal array
elements it is changing that is the reason this is a mutable okay now I will give you example for
immutable just understand this this is example for what mutable means we are able to change the
verginal values mutable means can change it now let let us take one more example for immutable
just first listen carefully don't ask anything now let me fish finish let me finish it then ask
it you should more concentration is required here immutable so still I'm not talking about strings
just understand only this much for now so we understood what is mutable right so now we will
try to understand what is immutable let us try to understand what is immutable so I'm taking one
string variable here string s equal to new string I'm taking one string observe this I'm taking one
you can take variable also no problem now what I will do is I will apply one any string method
on this variable any string method you can take yes dot I'm just saying conat I'm taking conat
and I'm just conting something let's say Java concatenated something to S after that I'm trying
to print yes now tell me what is the value of s what is the value of s when I print s value
what is the value you will get okay so before applying this method again I'm trying to print
AS Val system. print L yes so this is before applying this s do conact after applying this
so this is before apply s do conact this is after applying s do contact so here the value is
what obviously here it is welcome okay and here I have concatenated after concatenation again
I'm printing the yes value what is the value of x what is the value of yes we are expecting
just try to execute r as Java application see still we are getting welcome but we have
done some concatenation here right but after concatenation white is not done any change on
the veral value this is a veral value but after applying the concat it should do some operation
on the string right but still we are getting the welcome here it is not done any change it is
not done any change right so this particular feature is called immutable immutable means we
cannot change we cannot change so mutable means we can change just observe still not completed
hold on so when you apply this s do concare we are still getting the old value that means this
is not able to change the original value of s okay but I'm concatenating right but why it is
happening but I want to get concatenated value I want to get concatenated value but how can we get
concatenated value we have to store this output in some other variable okay take one more variable
okay and then you can say concat string then you print this you will get a concatenated string
okay you can just say concatenated string you can just take this variable then print it here
so then you will get the concatenated string you'll get the concatenated string but this is
not able to change the verginal value of s so this operation cannot change the verginal value of
yes you understood the point but concatenation is happening or not yes concatenation is happening
because we are storing that concatenated value in some other variable and we able to print
here but the thing is this operation is not impacting the existing variable okay but whereas
in out other example in a mutable this operation is changing the verginal array elements here that
is the reason we are saying that is mutable this operation is directly changing the existing
variable the original values are changing but here this operation is not changing the original
operation that's the reason we're still getting the same value but if we stored that value in
another variable then we are able to get the concatenation guys now you understood what is
the difference between mutable and immutable if any method operation is changing the verginal
elements or verginal value of the variable that is called immutable feature if any operation of
any method is not able to change the existing value or original value of the variable that is
called immutable feature mutable and immutable you understood everyone what is mutable what is unmut
here we are using some arrays related method but here we are using string method that's the only
difference method is a method if any method is able to change the original value of the variable
that is called immutable if any method is not able to change the existing value that is immutable
object now string is immutable strings are immutable in Java strings are immutable means any
of the string operations will not impact existing value of the string or original value of the
string this is the main difference between mutable and immutable strings are immutable we
cannot change the value of the original right so now based on this understanding let us try to
compare string string buffer and string Builder okay everybody is clear this example mutable
and immutable please confirm in the chat window everyone string is only immutable in Java all other
things are mutable only string is immutable okay now let me show you another example
and based on this understanding we will try to understand about string string buffer
and string Builder okay so here string is immutable that we have understood string
is immutable object that means we cannot change string buffer and string Builder are
mutable objects those methods will directly impact the variable but in the string it is
not impact but in string buffer and string build they will impact the variables that's
the reason they are mutable objects string is a immutable object in Java okay remember this so
let us see one practical example so then you can understand so string versus string buffer versus
string Builder take this main method and say finish okay now let us try to perform same
operation on three different variables one is string string buffer string Builder and then
we will see the difference which one is mutable which one is immutable we will try to understand
so let us start with the string so as we already know string is what immutable right string
string is what immutable right so let's try to see some example as we already discussed
let me take one variable string s equal to welcome so this is my string variable now I want
to do some concatenation s do concat of two Java so now I have done some concatenation so after
concatenation if you again print s value this will print the original value not a concatenation
value right so we'll get the verginal value so this is not impacted the verginal value so that's
the reason we are saying this is immutable cannot change original value of yes that we have
understood now same thing we will try by using string buffer and string Builder so let me
take string buffer string buffer is what mutable string buffer is a mutable means mutable means
what we can change we can change let me take one variable string buffer s b r string buffer okay
SBF I let me take something SBF or you can take string itself let me comment this the variables
you can use anything no problem so here I'm taking string buffer is equal to new string buffer and
here I'm saying welcome now I want to concat this so in the string buffer in the string Builder also
we have concat concatenation methods but the name is different so here to concatenate we use concat
method in the string if it is a string Builder or if string buffer we have a one more method called
append one more method called append yes do append to Java okay this is the method so here whatever
the concat is doing here append will do the same job the name of the method is different but
it will do the same job okay but in the string Builder and string buffer we don't have a concat
method instead we have another method called append this is exactly the same as concat method
okay now after appending after concatenation I'm trying to print the value of s okay now we can
see the difference here when I run as a Java application now we can see is concatenated right
we are not getting the verginal value here here we are getting the versional value directly after
concatenation but here after concatenation we are getting the concatenated value that means the
verginal value is got changed by this method means what it is mutable we can change original value of
yes now you understood the difference guys string and string buffer what is mutable and what is
immutable string is a immutable string buffer is a mutable because all these methods in the string
buffer we able to back the versal value of the variable this is got changed that's the reason we
are getting the latest is a mutable we can change the value of s okay so here we are still getting
the welcome but here we get the concatenated value same as string Builder also
string Builder this is also mutable object so let's create a string Builder
okay and yesal Al to Let's command this string Builder s equal to new string
Builder and take any value now we want to apply one method you can say s do append I'm
using this method append and here appending some value okay now I have done some operation
by using append now I'll write to print yes value exit okay now we can see it is concatenated
that means what this is not this is impacted the original value of yes so this is same output
so string buffer and string Builder is almost similar so they are mutable string is only
immutable object other than string almost every collection every type is uh mutable we
can change except string okay so that is the difference between string string buffer string
Builder so again in string buffer and string Builder there are memory related differences
are there but user perspective everything works similar okay yes we are us used here upend work
with the string so here in the string class we have a concat method right but in the string
buffer and the string Builder we have append method it's exactly similar to the conand but
in the string class we don't have append in the string class we don't have append method we have
a concat method but in the string buffer and the string bu Builder we have a append method it
is similar to con just name is changed purpose is same the name is got changed that is okay in
the string class we don't have append because we already have concat if it is the same job
how can they create two different methods two different names purpose is same right so
they haven't created so in the string class we have a concat method in the string buffer
and the string Builder we have a append method okay so in the string buffer
and string Builder there are memory related differences are there related to
memory management now I'm not going into that part is not important also for us just
try to understand they are mutable objects string buffer and string Builders are mutable
objects but most of the times we will use only strings okay we don't prefer to use string
for string Builders all those things only string class we will try to use okay right so
now the last example string comparison which is another very very important in question
two inter questions you will get here string comparison in the string comparison there are two
interiew questions you will get one is what is the difference between dou equal to and equals
method while comparing the strings we can use double equal to we can also use equals method
right so what is the difference when we have to use W equal to when we have to use equals
both are same or both are different if it is a different how they are different okay and at the
same time it is also related to this one when you create a string variable we will do like this
string s equal to welcome this is the one way we create a string another way is what through
object string s equal to by using new keyword we can create string s equal to new string of
welcome you can do like this also then what is the difference between these two so these two
are again related to the first one so these are two different questions you may get in interview
what is the difference between dou equal to and equals method and what is the difference between
this one and this one at the time of declaring so with single example we will try to understand uh
both one by one so go back and create new class string comparison string comparison
I will show you different scenarios first try to understand them then
I will elaborate different cases I will tell you string comparison let's take
one first case or first scenario scenario one I'm taking one string variable string
S1 equal to welcome first string I'm taking another string string S2 equal to string S2 also
welcome now I have taken two string variables S1 and S2 S1 contains welcome even S2 is also
contains welcome both are exactly the same value right now I'm trying to use S1 = S2 and
S1 dot = to S2 now observe this in the first statement I'm comparing both by using double
equal to in the second statement I'm comparing both by using equals method so what is an output
You're Expecting from these two please guess it first statement and second statement what is an
output anyway both will return a Boolean value right true or false so what is a first statement
output what is the second statement output true or false okay let us try to execute as a Java
application now you can see both are true so when you create a string variables like this okay
you can use either equal to double equal to or you can use equals there is no difference in this
particular context when there is no difference if you create create a string variables like
this in this format without using new keyword without using new keyword if you create a
variables like this there is no difference in this you will get exactly same value this
is optional you can use either double equal to or you can use equals method no difference
when there is no difference if you create variables in this format okay this is the first
scenario first case now let me tell you another scenario the next scenario I will create
variables like this let's take this is a case one now let's see the case two here I will create
string variables S1 equal to by using new keyword string S1 new string of Welcome string S2 equal
to new string of welcome so when you print S1 and S1 you will get the same data when you print
S1 and S2 you will get both welcome only okay observe clearly when I print S1 also I'm trying
to print S2 I'm printing S1 and S2 you will get the same data welcome and welcome right now when
I use this same statements I'm trying to use it here okay while comparing this observe
very carefully very important when you print values of S1 and S2 you're getting
same value and here I'm using dou equal to here I'm using equals method can you
guess what is an output of these two stat Ms here I'm comparing by using equal
to here I'm comparing by using equals method okay let's try to run it yes now first
statement is rning false second statement is returning true when I print the values we
are getting the same values but once you compared by using equal to and equals there
is a difference okay there is a difference here we need to understand why this dou equal
to is written false why this equals method RS true okay so now let us try to understand this
whenever you create a variables okay so normally dou equal to used for comparing the objects so
equal to we use to compare the objects remember this point double equal to we have to use to
compare the objects whereas equals method we will use to compare values of objects there
is a lot of difference in this I will tell you double equal to will be used to compare the
objects whereas equals method will be used to compare the values of objects okay values
of objects what is the difference what is comparison of object what is the comparison
values of object let us try to understand this so when you create a variable like this what
will happen when you create a variable by using new keyword then what will happen that we need
to understand so when I use double equal to what I am saying it will use for comparing the objects
so whenever you use new keyword it will create a new object okay string S1 equal to new string
whenever you see new string it is created one object and it will have some value in it so the
value of in this object is welcome and what is the name of this object is S1 so S1 is a name of this
object now take this next one when I say string St equal to new string again it is created another
object the the value is the same but it is another object the name of the object is what S2 okay now
if you see double equal to compare the objects S1 and S2 are the two are two different objects are
not both are different objects so they are not equal that is the reason it is returning false
S1 and S2 are the two different type of objects so that's the reasons it is dou equal to return
return Force because dou equal to will compare only objects it doesn't need about values it
don't care about the values of the object it SC about only objects are same or different that
is the reason dou equal to return false now come to the next statement equals method will compare
the values of objects now S1 is having welcome S2 is also having welcome values are same that is
the reason equals method returns true so basically what we need to understand here is double equal
to will compare the objects whereas equals method will compare the values of object and when
object will be created whenever use this new keyword the object will be created okay is this
clear everyone I will show you some more cases this is a difference so when you can see the
difference between double equal to and equals when you create strings as an objects you can see
the difference when you create a strings as a just of variables you don't see the difference both
are wring true okay now I'll show you one more case listen very focus is attention is required
now you guys should answer this question third case okay just a second let me
comment this third Case Case three I'm taking one string variable
string S1 equal to ABC normal string variable normal string I will cover all the
scenarios just listen first okay string S1 equal to ABC normal string variable now I'm
saying string s 2 equal to new string of ABC okay now I'm taking two different things
string S1 equal to ABC string S2 equal to new string of ABC right now let us try to use
the same statements dou equal to and equals method always remember double equal to
will compare the objects equals method will compare the values of objects now can you tell me what is an output of this S1
equal to S2 S1 equal to S2 the first one okay let me just uh show you if I look at S1
and S2 in S2 anyway we are used new keyword this will create a new object right S2 is created a
new object that is having ABC anyway this is a new keyword we have used S2 is a totally different
object S1 it can be object or it can be variable doesn't matter okay so S1 is totally different
than S2 or not okay it is a variable or object we don't bother because S2 is already object because
we use a new keyword so S1 is totally different so when you compare S1 S2 objects are same or not
objects objects are same or not no so it is false and values of object is same or not values of
object is same or not yes it should return return true okay you are understanding now so
equal to will compare the objects equals method will compare the values of objects so
in this case the first one is returned false second one is returns true this is case three
but when the objects becomes equal so when the objects becomes equal in this particular case
S1 and S2 are totally two different objects here S1 S2 are again two different but when the
objects will be equal we will see in the next Case Case four is four see this taking string variable string S1
equal to AB C normal string variable string S2 equal to new string of ABC string object now
I'll say string S3 = to S2 see I'm assigning S2 in another S3 S3 is a string again I'm assigning
S2 into S3 okay three strings I have taken now now I'm trying to print S1 W = to S2 tell me output
of this S1 W = to S2 true or false S1 W = to S2 or false double equal to means compare the objects S2
is anyway one object S2 is S1 is a different so S1 and S2 not equal so this is false false because it
will compare the objects S2 is anyway a different object S1 is a different so false expected now the
next statement system it S1 dot equals of S2 now tell me this S1 do equals S2 equals we'll compare
the values of an object so values are same so this will returns true this is also expected right now
the next statement is system. out. print Ln S2 equal to S3 now tell me output of this S2 equal Al
to S3 double equal to I'm using dou equal to I'm using two or false S2 is one object same object we are
assigning to S3 or not right we same object we are assigning to S3 so first of all S2 will be already
created this is a new keyword we created this is called S2 object this object is referred by what
S2 now what we are doing same S2 we are asscending to the S3 that means what S3 is also referring
to the same object okay S2 is referring the same object S2 S3 is also referring to the same object
now S2 and S3 are same or not equal or not when I use dou equal to S2 and S3 objects are same or not
yes objects are same so this will return true S2 equal to S3 will returns true because objects are
same true returns true because objects are same or objects are equal okay now suppose if I use
like this system doint S2 dot equals S2 do equals of S3 now tell me this S2 do equals of S3 equals
method I'm using equals equals means what compare the values of an object now values are same or not
yeah if objects are same values obviously same so this will also returns true compare the values
of objects so this is rning the true okay now let's see one more system out. print and here
I say S1 equal to S3 now now tell me S1 equal to S3 double equal to compare the objects so S3
is a different object S1 is a totally different object so obviously it returns false and same
S1 dot equals to S3 equals compare the values of an object so S1 value is ABC S3 value is
also ABC both are equal so this will return true okay so guys now you understood when we
have to use double equal to when we have to use equals okay so double equal to will compare the
objects whereas equals method will compare the values of objects okay and in the first case if
you create a string variables just like this you you don't see the difference you can use anything
but when you are creating variables by using new keyword then only you have to carefully check dou
equal to or equals method okay whenever you are using new keyword you have to use equals method
okay when I use double equal to Method you will not get the right output so remember that part
so when you create a string variables just like this you can use anything your choice double
equal to is also correct equals also right but sometimes when you create a strings by using
this particular notation you have to use only equals method okay we don't compare objects most
of the time we will always compare the values of an object so when you create a string variables by
using a new keyword always prefers only equals not double equal okay got the clarity now everyone
what is the difference between double equal to and equals when we have to use double equal to
when we have to use equals method which is very very important from strings concept so damn sure
in question reverse string and string comparison so these two are most important questions from
interview perspective you should able to clearly explain with [Music] examples right so this is all
about different operations which we can do on the string so with this we have done with the strings
concept and I will also give you some assignments on strings uh today's topic based on today's
topic you guys can check assignments all are most important guys you can't miss even single
one you have to practice there is no other way okay so come back to assignments uh I'll try to
give five different assignments all are related to string concept I will give a solu Solutions
also you guys can check the first assignment check a string is palindrome or not this is actually
related to reverse string okay first you have reverse string right this is related to palr
means what after reversing if you get the same value that is called palr so if you go back to the
reverse string this is the original string after applying this logic we are getting the reverse
string in the reverse variable right so after reversing what you have to do is you have to write
more if condition in the if condition you have to check the verginal string is equal to this reverse
string then the string is palindrome else string is not palindrome so you guys can easily do that
so this is also related to reverse string so this is the example I have given if I take one string
like this after reversing we are getting the same string that is a pal and after reversing if you're
not getting the same thing not pal so this is the link I have given and you can go through it and
you can understand you can pry this next remove junk or special characters in a string these are
all very popular interview questions guys we have to just practice this very simple so remove
junk or special characters in a string suppose sometimes you may see some special characters
dollars dot dot some iphon some Stars carrots you can see some JK characters in your strings
and I want to remove all all those Jun characters I want to get plain string okay for example you
have string like this welcome and in between you have some junk characters like this so like this
okay some some junk characters it can be anything so like this or like this so if you want to remove
this junk characters in a string how can we remove this so this is a program remove Jung or special
characters in a string and also given the link here you can refer this and then how to remove
white spaces white spaces in the sense normal spaces let's say I have one string like this okay
and Suppose there is a space w e l c o m e so this is actually one single string but there are some
spaces are there this is one space another space here another space right multiple spaces are there
I want to remove all the spaces then I should get versional string like this so that's called how
to remove white spaces in a string okay that I can you can do one assignment this is the reference
video link I'm giving the next one so you can try this in multiple ways there are so many ways are
there in Java to do this just try one way at least next fourth one count occurrences of a character
in a string very important count occurrences of a character in a string what does it mean suppose
if I give one string like this ABC ABC ABC a a a b b b c c c if I give string like this how many
times a is repeated how many times B is repeated how many times C is repeated that I want to find
out okay from that string particular character how many times it is repeated how many times it is
occurred in a string that we have to count okay so how many times a is repeated we have to count
one two and here it is three four five six like this and totally a repeated six times so like this
we have to find count occurrences of character in a string very important and very easy also count
number of words in a string means what suppose you say welcome to Java selenium how many words are
there in this string how many words totally four words that we have to find out through program
we have to find out how many words are there in a string okay so this is a reference L and all are
very easy programs actually the problem is seems very complex but once you understand the logic
once you start implementation it's very simple so by watching the video you will understand how
can we do this so first you try yourself don't Google it and don't find anywhere else because
that is a your mind exercise so thinking about logic is always gives exercise to your mind so you
have to start like that initially think if it is a manual process how you will do and once you
frame the logic try to put into the statements okay and once you run your program at least 80%
you have achieved that is fine okay then you can refer the video and then you can compare how
we have done and how in which way the video is explaining and compare this and then correct it
and then try multiple times so that is a process of uh improving the logic okay so both all are
logic based programs and interior perspective these type of programs they will ask okay so
try this assignment for today's session and as soon as you finish those assignments try to
upload them with the drive itself okay that's all for today's session we will stop it here
and we will continue in the tomorrow session
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact