Transcript for:
Understanding Strings in Java

it's time to talk about a very simple concept and complex at the same time because to use it's very simple but when you try to understand what is happening behind the scene that's why it gets complicated so let's try to understand that in this topic of string so in this we'll just talk about what is string now basically we have worked with string before example whenever you try to create a variable we can have we have to specify the type of it right example we have worked with integer float double Boolean in the same way if you want to store a character stream let's say if you have multiple characters something like this let's say you want to store a name like Naveen now this Naveen is not a single character right it's a bunch of characters and when you want to store this together of course you can use a character array we will talk about earlier later but then we can create a character array here which is a array of different different characters or we can create a string here now how do we represent that in a string now one thing to remember whenever you talk about string in Java you have to put that in double quotes the way you work with characters and characters you use single quote here we'll be using double quotes So if you want to store Naveen so you have to first put that into double quotes and then where you will store this of course you have to you you need a variable here right so what I will do is I will create a variable called name now this variable name needs a type now what type of this this is of type string now one thing to observe when you talk about primitive data types we have end flow double character Boolean and many more right now all those actually starts with small letter right uh this string as starts with capital which means that string in Java is not a primitive type it's a class basically if you go to string class you will find string is actually a class okay that actually makes some sense but then when you say class we have seen this right whenever you work with classes and if you want to create object of it we use a new keyword right we are not doing it here so this does not look like a correct syntax it works but it's not something we are used to right whenever you work with class we normally create a new object okay this works but then why this will work that we'll discuss later at this point if you want to go in a general sense if you want if you have a class the way you can create a string is by the way you can get object of string is by saying new string and you can give the round packet now this is how you create a string variable a string object now basically this is a variable right reference variable which is referring to a string object now what might be happening behind the scene so it's very it's not that difficult uh if you imagine this as your jvm The Big Box inside this of course you will be having your stack memory right now this is your stack and then of course in this uh you will be creating a reference variable which is name and then it will have some address but this address or the object will be created inside Heap memory imagine this is your HEAP memory you know we should make this a big one this time let's say a bigger Heap memory so that I can write some multiple things there now the reason we are creating a heap memory in another structural format is because there is no fixed size to it it may expand depend upon your requirement so we got a heap here now when you talk about this line line number eight when you say new string of course it will create a new object for you you got object here right and it will have let's say some address I will say the address here is one zero five so of course you will create a variable which is name here and then the address is one zero five so there's a link here and of course in this particular box there is no data it's it's empty right okay uh this empty makes sense actually but then if you try to print this let's try let's see what it prints if you try to print this data here I will compile this code and run you can see it spin it prints nothing basically it prints blank thing okay it's because it's blank now how will you provide data now in this case what you can do is in this new string in the Constructor you can pass this string whichever you want to example if I want to store Naveen that's the D time storing ignore this original this is given by the IDE but this is our value which is Naveen so inside this string Constructor you are passing the data which is Naveen right now if you try to print this you can say we got Naveen here okay so that means in this thing uh we are creating the data which is Naveen that's what we are expecting as of now let's see if that works okay now there's also a concept of hash and equals right so what I will do here is I will try to print the hash code for this one so if you try to print the hash code and if you run this code you can say this is the hash code for this depend upon data depend upon some calculation now what else we can do here so example let's say if I want to concaten this Naveen with hello so let's say I want to print hello Naveen what you can do is uh of course you can write a different statement to print hello and then you can print name but then string by default does not work with any other operators like subtraction division but only there's one exception which is plus orbital so you can use string with plus operator okay so let's say I want to print hello and Naveen both so in this case you can print hello and then to concatenate to mix this two you will use a plus operator here so you can simply say plus and now with this compile and run you can say we got hello nabin I hope that makes sense so you can use plus to do to print this what else we can do uh we have certain methods here which we can use now since it's a class of course it will have some methods right uh so there are some handy methods here example you can check uh what is a character of a particular location so let's say I want to know what is a character at location two I mean index number one so if I pass index 1 here it will give me a character at index one so if you look at this Naveen it is n is index 0 a in is index one then two then three then four and if you try to compile and run this you can see we got a so yeah this is how you can basically use certain methods there which is character add or you can also concatenate two strings okay so let's say if you concatenate my name with I will give a space and ready so basically what I'm doing is I'm concentrating Naveen with ready here let's see if that works yeah it work and see that we got Naveen ready so that's how we can basically use this now since strings are so important for the programmers what we got here is uh let me just remove let me just remove this line yeah so string is so important for programmers what we got in Java is instead of writing this new string you can actually you know directly write Naveen here even that works so behind this it will create an object you don't have to worry about it so when you say string name equal to Naveen it will create the object for you you don't have to worry about configuration of those stuff so most of the time you will see this syntax hardly you will say uh string name new string and then Naveen that's hardly anyone do that so this is famous and it even this works you can still try this out and that's why before when we were using string we directly assigned the value it was working so now I hope string makes sense in the next video let's try to talk about different types of strings