String and Character Data Representation in Java – Learn Java #6

Welcome to DevFactor! hello and welcome to the factors they
were gonna talk about two different types of data that we have access to when building Java programs and these
two types of data or the character primitive data type in the string object in a taped so we
can use both char data and string data and her Java
program in there quite similar actually we use both of
them to represent character data but the biggest differences between the
two you are well of course the charted a tape takes up a lot less
memory also the charge it a type is only capable holding a single character at a time the
string data type on the other hand to hold as many characters as you want it’ll
just keep expanding syntactically they are very similar the chart e-type is instantiated by
creating a charming similarly the string is done the same
way know anyone who assign values to these two we assign values to which are using
single quotes and we assign values to a stream using
double quotes see me wonder why these are both in the
same video in the main reason is is because
truckers are very useful in string manipulation so most the time
we’re going to be using strings in not charged but sometimes the 12
manipulate are strings in various ways in the chart data type is just a crime
wave going about doing this so in this clip I’m gonna show you how
to write a method that’ll reverse a string say you give it
one strain and were termed the reverse the version
I love that string in this is possible because strings are
bill outta chars so in order to do this new course need an algorithm and the
algorithm is quite simple so we’re gonna need a for loop but
instead of iterating through our string from the first character to the last
character we’re going to dinner in reverse and we’re gonna iterate from the last
character all the way down to the first character since strings RR 0 index the first
character will be index 0 now using the built-in method Charak which returns the character at a
given location we can take the character each time we
carry so is star at the last character
gradacac Chur places on a new strain or in this case
actually a StringBuilder object which is designed for these
tables operations is more efficient then string concatenation so to reverse a string week iterate through
the string backwards and every time we find a character we story on a new strain or a
StringBuilder finally we get to the end week with the
loop and we return the new string and we have
the reversed version of the string in our example we’re reversing hello
world and if we take the output oh that
reversed hello world and put it back into the string object
in reverse it again will get a hello world as the reversed version the reversed hello world so
that’s about all for today I hope this video helps you guys out and if you like this video tutorial
please consider subscribing we have programming game development
tutorials three times per week right now we’re working on java game
development and it’s a ton of fun thanks for watching the movie Louis the more

Leave a Reply

Your email address will not be published. Required fields are marked *