(bright music)
- [David] So far I've described variables in Java
that use the primitive data types:
numbers, characters, and Booleans,
but there's a whole other category of Java variables
that are references to objects.
An object in Java is an instance of a Java class.
When you create a variable and you point it at an object,
the variable isn't the object itself,
instead it's simply referencing the object.
And you can actually have multiple reference variables
that are pointing to a single object in memory.
So you might declare an object that has a name of an object,
and then you might have three different variables
that are all pointing to the same thing.
If something changes about that object in memory,
all three of those variables would update.
Here's an example.
Let's say that I've defined a Java class.
This class is named ClothingItem,
and I've declared a string variable
that's a member of the class.
Notice that this string variable
doesn't have the word static.
That means it's something called an instance variable,
also known as a field.
An instance variable is not a member of the class itself.
It's a member of an instance of the class.
Now, in my main method,
I'm creating an instance of the ClothingItem class.
This line of code has a data type,
which is the name of the class,
then the identifier, which is the name of the variable item,
then the keyword, new,
and then finally a call to something called
a constructor method.
I'll go through constructor methods in detail later,
but what you need to know now
is that this is how you create
an instance of a class in Java.
It's called instantiation.
Now, once the instance is created,
the variables that you declared as instance variables
are now available.
So I can assign a value to the type variable or field.
And that's what the next line is doing.
Item.type equals hat.
Class instances or objects can also have instance methods.
And that's what display item is.
This is a method once again, without the static key word.
And therefore it's a member of an instance of a class,
not a member of the class itself.
So, I can call it from item, the object.
And then it will output information
that belongs to that instance of the class.
The expression, this.type means the type variable
that's a member of the current instance of this class.
So instance variable, instantiation, and instance method
are three bits of vocabulary you should know about
in order to work successfully with Java classes
and instances of those Java classes known as objects.
A string in Java is a complex object.
It's an instance of the string class.
The string class just like the helper classes
for the primitive data types is a member of java.lang.
So you don't have to import it
or use any special notation to use the string class.
It's always available to you.
Here's an example
of creating an instance of the string class.
Once again, I start with the data type,
string with an uppercase S,
then I assign the name or identifier,
then after the equal sign, that's the assignment operator,
I have the keyword, new.
Then I'm calling the constructor method of the class
and passing in a value.
This creates a string variable.
The string class is a little bit special
because you can also use a shortcut.
The compiler understands this
to mean exactly the same thing.
Create an instance of the string class
and assign it this value.
But using this syntax, you can eliminate the new key word
and the explicit call to the constructor method.
But again, these two lines of code
mean exactly the same thing.
Here's some other important characteristics of strings.
A string is basically an array of characters.
A character is a primitive, a string as a complex object,
but a string can have as many characters as you want
assuming you have adequate memory
in the computer that's running your code.
So a string is a collection of characters
in a particular order.
This might be a string object, the string, hello,
but you can break it down into an array of Char values.
Another important thing to know about strings
is that they're immutable.
That means that once you instantiate
and assign their values, you can't really change them.
Now, the Java compiler will make it look
like you can change them.
For example, if I start with the variable named String1
and I assign it a value of hello,
and then on the very next line,
I say, String1 equals hello world,
it looks like I'm changing the object,
but in fact, in the background,
I'm dereferencing the original object.
That object can now be cleared from memory
in a process known as garbage collection.
And I'm creating a brand new object with the new value.
It's essential to know this
because if you've worked with languages
where strings aren't immutable,
where you can change them at runtime,
you might be used to simply appending values
over and over again,
but in Java, that results in creating a lot of objects
when you might not mean to do that.
And it can create memory management problems
in the background over which you have very little control.
Here are some other examples
of working with strings and characters.
In this class, I'm declaring an array of characters.
The expression char with open bracket and closed bracket
means an array.
Then I have the variable name, chars.
That's the identifier.
And then I'm explicitly assigning an array using the braces.
Each item in the array is separated from the next
with a comma.
Each of the characters is a literal character
using single quotes.
Now, to turn that into an actual string object,
I create the string and I use the string constructor method,
and I pass in my character array.
And the result is an actual complex object.
(bright music)