Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Objects!
You have completed Java Objects!
Preview
Let's refresh our Java learnings using a new tool named jshell in our old friend Workspaces
Pre-requisites
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Hello and welcome back!
0:09
I'm Travis
and I'm an instructor here at Treehouse.
0:10
This course builds
on what we have learned in Java Basics.
0:13
If you haven't checked that out yet,
0:16
I recommend following the link
in the teacher's notes below.
0:17
In this course,
we're going to be talking about objects.
0:21
Java is an object-oriented
programming language
0:24
and literally everything in Java
is an object.
0:27
Well, except for primitive types
like int and boolean,
0:29
but even those have wrapper types,
which are, you guessed it, objects.
0:32
So as you can imagine, understanding
objects is crucial
0:37
to your foundational knowledge of Java,
and I'm excited to explore them with you.
0:40
First, we'll familiarize ourselves
with the basics of objects,
0:45
how to use them, how to create them,
and then, after
0:48
we get a good grasp,
we'll build an application using them.
0:51
Along the way, we'll expand our Java toolset,
learning new tricks as needed.
0:55
Just like in
Java Basics, there will be many new terms,
1:00
and remember, you shouldn't feel like
you have to understand everything fully.
1:03
Immerse yourself in the language,
and I will cover, in detail,
1:07
what I think you need to know
at this stage of your learning.
1:10
We'll be using workspaces again
for this course,
1:14
so you can follow along and code without
installing anything on your local machine.
1:16
We'll also use a tool
that might be new to you, JShell,
1:21
the REPL introduced in Java 9.
1:24
REPL, R-E-P-L, stands for Read, Eval,
Print Loop,
1:26
which allows you to interact with
and explore the language.
1:31
I think you're going to love it.
1:34
Let's launch a workspace and check it out.
1:35
Okay, welcome back to Workspaces.
1:39
Remember,
1:41
if you don't see your console down here,
you can go to View, Show Console.
1:42
I'm going to stretch mine up,
1:47
and in this console, type JShell to start
the REPL.
1:48
It's a powerful tool
and we going to use it now to refresh
1:52
what we learned so far. With JShell,
you can create variables
1:55
just like you do in regular Java.
For example type string firstName
1:58
equals and I'll put Travis. You can of course
put your name though.
2:04
As you can see, it outputs this,
2:09
letting us know that this firstName
variable is set to our string.
2:11
That's kind of its style there.
2:14
You can now access that variable too.
2:16
For example, if we say Boolean isTravis
2:18
equals first name dot equals Travis.
2:22
Which remember that equals
method checks equality
2:28
with our strings
and returns a true or false, or Boolean.
2:30
So you'll see that
I was able to access the firstName
2:35
variable as it's in the scope of this
shell that we're working in.
2:37
It also created this
2:41
new isTravis variable
and stored the results in there.
2:41
Pretty cool, right?
2:45
You can also just say firstName
dot equals
2:46
Brian.
2:49
And you'll notice that JShell
automatically created a new variable here
2:53
called $3.
2:56
You could even then go $3 equals true.
2:58
And that's, of course, false.
3:02
And it even saved that
into a new $4 variable.
3:03
If you haven't seen this double equals,
don't worry, we'll get into that soon.
3:07
So it just kind of stores
whatever is returned
3:10
in these numbered variables
with a dollar sign at the front.
3:12
We can also check
for case-insensitive equality
3:16
with equals ignore case, like we've done
previously.
3:18
First name, dot equals ignore case,
3:22
Travis, all caps.
3:27
And that will also be stored, $5.
3:30
So JShell will automatically store
anything for future reference
3:32
should you need it.
3:36
To clean up your console,
3:37
you can press control
L, and it'll bring us back up to the top.
3:38
If you did the extra credit on Java basics
where we tried to validate input,
3:42
you learned about the contains and to
lowercase methods that exist on string.
3:46
Let's take a look at those real quick.
Let's make a new string. We can say
3:50
string someWords equals
this is a bunch of words
3:54
Real quick
4:00
I like to point out that for most
single line statements the semicolon at
4:01
the end is optional.
I still tend to type it out of habit,
4:04
and there are a few scenarios in Jshell
where it's necessary,
4:08
but if you see me happen to not add one,
that's why.
4:10
Now if I start typing some w
and press tab,
4:15
it'll actually search and find some words
to autocomplete it for us.
4:18
It automatically
does that for methods too.
4:22
So if I now add .conta, then press tab,
4:25
it will autocomplete it
and add the opening parentheses.
4:30
I'll give it the argument of bunch,
4:33
and then contains will look
for the word bunch inside of some words.
4:35
It exists, so it returns true.
4:40
You can also press the up arrow to recall
previous commands, just like a terminal.
4:42
If I adjust our previous call
to see if our variable contains this,
4:47
you'll see that it returns false.
4:51
That is because
contains is case-sensitive.
4:53
Since the casing of the provided
string doesn't match that of the
4:56
this in our variable,
it comes back as false.
4:59
So to get around this,
we could create a new variable and do
5:02
case-insensitive checks on it, right?
5:05
We could say string lowered words
5:07
equals some words dot to lowercase.
5:12
And if we use that
5:19
for our check on this,
you'll see that it comes back as true.
5:20
Lowered words.contains this.
5:24
Cool.
5:28
We could even make it easier
by skipping that extra step
5:29
and chain methods together.
5:32
For example, we could say,
some words dot to lowercase
5:34
.contains this.
5:39
Both are totally fine options.
5:41
This last one
5:43
just prevents us from creating and storing
another variable in memory, though.
5:43
Okay, so we worked with integers, right?
5:48
Like int age of Bob equals 30.
5:50
And int age of Mary equals 28
5:56
You can compare numbers like checking
6:01
if Bob age is greater than Mary ageOfBob
6:02
greater than ageOfMary,
6:06
which is true.
6:09
And we learned that we can use
the integer wrapper class
6:11
to convert strings to numbers, like so.
6:13
int age equals
6:16
integer.parseInt,
and we'll give it the string of 30.
6:19
Nice.
6:26
Finally, let's recall a bit
more about Booleans.
6:27
You can use conditional logic
with AND or ORs.
6:30
For example, Boolean example
6:34
equals false OR false,
6:37
which is false, of course.
6:42
But if we add an OR true at the end,
it returns true.
6:43
With ORs, if any of these are true,
it means the entire expression is true.
6:49
So it's like if this is true,
or this is true, or this,
6:53
and that one is, so it's all true.
6:57
And then we can adjust
that to be ampersands,
7:00
and it will check
if all of these are true,
7:02
which it's not,
so the condition will evaluate to false.
7:08
And what actually is going to happen
here is it's going to take a shortcut,
7:11
so if it sees that
the first operand is false,
7:15
it doesn't even need to check
the rest of them.
7:17
Feel free to play around with expressions
until you feel refreshed
7:20
with what we've covered thus far.
7:23
We'll spend quite a bit more time in here
during the course.
7:25
Real quick, two little notes about JShell.
7:27
All these variables,
or methods, that you make in a JShell
7:30
session are not saved once
you close it out.
7:33
And, to close it out and exit
the REPL, press CTRL-D.
7:35
Great.
7:39
Awesome.
7:41
I'm feeling refreshed.
7:42
That REPL will be super handy
going forward.
7:44
Before we move on, remember,
if anything isn't clear or you're
7:46
pondering something, head over
to the community and share your questions.
7:50
Alright,
I think we're ready to dive into objects,
7:54
right after you ace this refresher quiz.
7:56
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up