Hi Everyone interesting story all I want to talk about this story in class in Java.
So first of all we need to create a Java project.
So we need to click on File New and then click on Java project.
OK.
So for the project name just type in here.
A strange class.
OK.
And then click on finish and then if we need a main class for our project.
So just click on this as our See folder and then click on File New and then click on class.
So for the class name just type in here made and for the package name we need a reverse domain here.
So just type in here com that your name dot project name is three in class.
OK.
And for the modifiers to publish and here make sure that you check this check box here that says public
static void main effect.
OK.
And then click on finish.
So as you can see here our main class is now created.
So just delete this comment here and then I double click on this main Java tab here in order to make
this a little area wider.
So as you can see here my editor area is now much wider.
So now I want to talk about a predefined class in Java called string.
Ok so what is strange strange is nothing but a sequence of characters.
For example hello is a string of five characters in Java programming language.
Strange is an immutable object which means it is constant and can not be changed once we actually created
that object.
OK first of all in order to create our screened there are two ways.
The first way is string literal and the second way is using the new keyboard.
OK so the first way is strangely the road in Java strings can be created by assigning a string literal
to a string instance.
OK so now let me show you some real examples about strings.
OK.
So first of all in order to create a string object first we need to refer to the name of our string
class as strange as you can see here.
And then if we need to give our object a name for example my first strange.
OK.
And then we need to put in an assignment operator here and then we need to actually assign a string
literal to our string object.
OK.
So I just need to put in here to double quotes and insert the double quotes.
I can just typing Hello.
OK.
And then I need to put in a semi-colon at the end of my statement.
So now as you can see I created a string object called my first string and I assigned this string literal
to our string object.
OK.
So in order to actually create String objects we need to actually put use these double quotes and inside
the double quotes.
We can just put our string literals.
So remember that string object is nothing but a sequence of characters.
OK so now if I want to output the value of this string object to the console I just need to type in
here system that out that princeling.
And then inside the prentices I can just refer to the name of my object my first strange and then I
need to put in a semi-colon at the end of my statement.
So now if I run my project let's see what happens.
As you can see this value is now output to the console.
Hello.
OK.
So this value is the value of this string object.
OK so now let me actually create another string object.
So again I need to refer to the name of my class as strange.
OK.
And then I need to give my object a name.
My second string.
OK.
And then I need to put in an assignment operator here and then I put in double quotes and then inside
the double quotes again I type in Hello OK and then I need to put in a semi-colon at the end of my statement.
And now if you want to output the value of my second string object to the concert I just need to type
in here system that out that print learned and it inside the parenthesis I can just refer to the name
of my object my second string object.
And then I need to put in a semicolon at the end of my statements.
So as you can see here now this value is output to the console.
And as I said earlier in the section this for example this my second string doesn't hold this hello.
Object here.
This my second string only actually contains the address of this object in memory.
OK so this is how our objects are actually saved in memory.
But for primitive data types for example our primitive data types like int and for example the name
of my value is a.
And then I can assign a value to my value.
But here is a valuable actually holds this number free wheeling it.
OK.
But this my second string actually contains the address of this hello object in memory.
OK.
In other words this my second string is actually a pointer to this.
Hello.
Object in memory.
Ok so now let me actually show you another important point about objects in memory.
So as you can see here I have 10 string objects here.
OK.
So now as you can see here I assigned the same value to all of these 10 String instances.
OK.
So as you can see here for example for my first string instance I assigned the word hello to my first
string instance.
OK.
Or for example for my second string instance I assigned the word Hello.
OK.
And for all of my 10 string instances here I assigned the same word hello to all of these string instances.
How this object is actually saved in memory.
So because we actually have 10 strange instances here with this same value the compiler is only actually
create one object which as you can see here divorce.
Hello.
And it's going to actually assign the address of this object in memory to all of these 10 string instances.
OK.
So for example my first string instance actually holds the address of the hello object in memory or
my second string instance actually holds the address of the hello object in memory.
Or in other words these string instances actually point to the hello object in memory.
OK.
So that's it that's how actually the objects are actually saved in memory.
When you actually create String objects by using string literals.
OK but if you actually for example change the value of one of these string instances to another value
that string instance going to actually hold another address for that specific object.
OK but if you have actually the same value for your string instances the compiler is going to actually
create only one object and that is going to actually hold the address of that object to those string
instances.
So now let me actually show you some real examples in order to make this concept more meaningful for
you.
OK so now if you go back inside the eclipse as you can see here these two objects this my first string
object and this my second string object have actually the same value.
OK so the compiler is going to actually create only one object and then it's going to actually hold
the address of that object inside.
Inside of these string instances.
OK.
So now if I for example change the value of my first string object to another value of i need to refer
to the name of our object.
My first strange and that I need to put in an assignment operator here and then I actually change the
value hello to HI.
Okay.
And then I need to put in a semi-colon at the end of my statement.
So as you can see here now if I want to output the value of the first string object to the console I
just need to type in here system that out that princeling and it inside the parenthesis.
I can just refer to the name of my object my first strange and then I need to put in a semicolon at
the end of my statement.
So now if I had on my project as you can see this value is now output to the console.
OK so now the address of this object is actually is actually reading this my first string object.
OK this object is inside another place is in the memory.
And that address is inside of this my first string instance.
OK.
It's my first string instance actually holds the address of this object inside the memory.
And this my second string object actually holds the address of this object.
Hello.
Inside the memory.
OK.
So now if you want to output the value of the second string object to the console.
Let's see what happens.
System that out that prints learn and then inside the parenthesis I can just refer to the name of my
my second string object.
And then I need to put in a semi-colon at the end of my statement.
So as you can see this value Halo is now outputted with the consular kit.