In this lecture, I'm going to talk about object references.
This topic is divided into four parts.
First, I will tell you what object references are.
Second, what makes them different from primitives?
Third, what basic built in object references Java has.
And last, we will have a code in part about how we can use them.
What object reference means, let me explain this, so object is an instance of a class, it is stored
somewhere in the memory and reference is used to point to that memory location where that object is
stored.
You don't have to handle it.
Jabot will do it for you and uses the variable name to get the object.
To visualize this, you can see a class here.
It is like a blueprint, a scheme, a plan for something like a car, a dodge challenger in our case.
But it can be a movie, an RPG character or something else.
So an object can be created based on that blueprint.
Sticking to our example, we have a Dodge challenger because here it had a lot of attributes, like
a number of seats, horsepower or is damaged.
When you create an object, you can add value to all those attributes.
You can have a dodge with three ninety two horsepower or another one with seven hundred and seven horsepower.
But you can create many other objects with different attributes.
You can create almost unlimited amount of objects out of a class.
When you create an object, it is almost the same as with the primitives.
So instead of data type, we use class name.
Then variable name equals a value or the new keyword and the class name parentheses that where you can
add some parameters to the class and a semicolon.
As you can see, I used uppercase as a starter letter in case of class name.
It is not forced by Java, but it is highly recommended, so it's almost a must.
So as I mentioned, you can add parameters in the parentheses and based on that an object will be created.
I will give you more details on this later in the classes lecture, so just to stick to the Dodge Challenger.
We can create a class Dodge Challenger with all the attributes we coded in the previous lecture to create
an object of deduction and your class, you have to do the following.
The Challenger Red Dot challenger equals new Dodge challenger, and in the parentheses you add red and
a semicolon.
So it's going to create a dodge challenger.
And the color of that car is going to be red, so based on this, you can create a green one as well.
So don't change a green dot.
Chinja, new dodge challenger green.
So as you can see, the class name starts with uppercase.
The variable name starts with a lowercase as in case of the primitives or any other variables.
Right.
Then it was a little bit of taste of classes and objects.
Let's see what makes them different from primitives.
The biggest difference is that they have functionality.
So when we wanted to print something out, we used that printer and function and we put the text and
the variables between the parentheses.
In case of the challenger, we can create functionalities like honk and it would print something on
the screen or get color, which would return the color of the car, or we can have it get a description
or turn left or get horsepower.
But the options are almost unlimited.
What you have to keep in mind for now is that you can call these functions when you create an object
and put a dot at the end of the variable name and you can use those functions that are available for
that class.
So, as I said, more on this later to let's see what built in data types Java has.
They are pretty easy because every primitive has its object version with a little name change.
So byte short integer instead of end long float, double boolean and character instead of char.
The values you can store in them is the same.
We also have a new one here.
String String is for storing texts like Red Green Dot Chinja or sentence's.
As I mentioned before, objects have useful functions in case of string to uppercase, to lowercase
or equals that can be used to compare two strings or is empty or is blank.
These functions are very good and used quite regularly, but be careful.
The last two are not the same.
Besides these, there are many other functions that can be used for strings.
So to define a string variable, you have to do the following.
So first the class name string, then car type is the variable name equals in parentheses.
The text we would like to assign to car type variable name, then the semicolon.
It's almost the same like with the primitives.
Well string is a big topic.
There will be a few other lectures to cover that we are going to do some coding exercise with it, but
we will get back to it a bit later.
OK, I know this lecture has a lot of more on this later part, but I wanted to give you some insight
to those Java concepts.
I think it is better to give some explanation now instead of not telling you anything or having a separate
lecture with a ton of information moving back to object references.
I think the best way we can see how they work, what they are, is to use them in the code.
So let's get back to intelligent.