"Java Objects (Retired)" was retired on January 31, 2017. You are now viewing the recommended replacement.
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
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
[MUSIC]
0:00
Hello, and welcome back.
0:04
I'm Craig and I'm a developer.
0:05
This course is going to build on top of
what we learned together in Java basics.
0:07
If you haven't checked it out yet,
0:11
I recommend that you follow
the link in the teacher's notes.
0:12
It's a prerequisite for this course, but
0:14
you don't need to have any
other programming experience.
0:16
And what we're going to learn in this
course is relevant to many other
0:19
programming languages that
you're going to encounter.
0:22
Don't forget that there are speed
controls on the video player.
0:25
Feel free to speed me up or
slow me down to your heart's contempt,
0:27
I won't mind at all.
0:30
In this course,
we're going to be talking about objects.
0:32
Java is an object oriented
programming language,
0:35
and literally everything
in Java is an object.
0:38
Well, except for the primitive
types like int and Boolean, but
0:41
even those have wrapper
types which are objects.
0:45
So as you can imagine, understanding
objects is super critical to your
0:48
foundational base of the Java language.
0:52
And I'm excited to explore them with you.
0:55
So first we'll familiarize ourselves
with the basics of objects.
0:57
How to use them, how to create them,
and then after we get a good grasp,
1:01
we'll build an application using them.
1:04
Along the way we'll be expanding
our Java toolset, again,
1:06
learning new tricks as we need them.
1:09
Just like we talked about in Java basics,
there are gonna be a lot of new terms.
1:11
And remember, you should feel like you
need to understand everything fully.
1:16
Immerse yourself in the language, and
I will cover in detail what I believe you
1:19
need to know at this point
of time in your learning.
1:23
We're going to be using
workspaces again for this course.
1:26
So you can follow along and
1:28
code without having to install
anything at all on your local machine.
1:29
We're also going to be making use
of a tool that might be new to you.
1:33
It's called JShell, and it's the new
REPL that Java just got in Java 9.
1:36
In case you haven't heard of
the term REPL or R-E-P-L,
1:41
it stands for read eval print loop.
1:45
And it allows you to interact with and
explore the language,
1:47
I think you're gonna love it.
1:49
Let's launch a workspace and check it out.
1:51
Okay, so welcome back to workspaces.
1:53
So down here in the console,
if you just go ahead and type jshell,
1:55
it's going to start up,
it's gonna start the REPL up.
2:00
So again, that's read eval print loop.
2:03
Check the teacher's notes for
more on JShell.
2:05
It's super powerful, and
it's up and coming, and
2:07
let's use it right now to just refresh
ourselves on what we've learned so far.
2:09
It's an awesome tool,
you're gonna love it.
2:14
So what this will let us do, is it'll let
us create variables just like we know.
2:16
So we'll do a string, firstName = "Craig".
2:20
And you'll see what it does there,
is it says that it's set the variable
2:24
name to Craig, it's kind of
that's its style of output there.
2:27
So now we can access
those variables like so.
2:30
So we'll say, isCraig, and
let's do this firstName.to
2:33
call the method, equals("Craig"), right?
2:37
So remember that, we call the equals
method on strings to check equality, and
2:42
then it's stored in the variable.
2:46
Now there's a variable called isCraig, and
you'll notice that I accessed firstName.
2:48
So it's kinda in the scope of this
shell that we're working on here,
2:51
pretty cool, right?
2:54
You can also just go ahead and say,
firstName.equals(" Bob"), all right?
2:55
And what that did,
is it created a new variable.
3:02
Now there's a variable called $3,
and we can say it is $3 == true, and
3:04
it doesn't, right, cuz we know.
3:08
So $3 is a new variable name, and
now it made a $4 called false.
3:10
So it just automatically
creates these variables, and
3:14
it starts with $ in the front.
3:17
It kind of just stores whatever is
returned, right, so if I do firstName,
3:18
let's see, equalsIgnoreCase.
3:22
Remember that that was the one
that allowed us to check,
3:23
no matter if it was uppercase or
lowercase.
3:26
So if I say, equalsIgnoreCase("CRAIG"),
it's gonna create a new, this $5 and
3:28
it's in true,
just kinda stores whatever is there.
3:32
So if you wanna refresh your screen,
you can do a Ctrl + L,
3:35
and it will bring everything
up to the top, there we go.
3:39
So if you did the extra credit on Java
basics, where we tried to validate input,
3:43
you learned about the contains and
toLowercase methods that exist on string.
3:47
Let's take a look at them real quick.
3:51
So let's make a new string,
let's say string
3:52
Some words = This is a bunch of WORDS,
all right.
3:57
So we now have a variable
called someWords.
4:02
And so, if I start typing S-O-M and
I press tab, it's gonna look, and
4:04
it will search, and
it will find that I have someWords there.
4:08
So it'll automatically do that,
and it works for methods too, so
4:11
I'll do contains, and
I'll do tab and I'll pop this over.
4:14
So contains looks for the word
bunch there, inside of some words.
4:17
Does it exist?
4:24
It sure does, there it is, all right?
4:25
And we can also, if I press the up arrow,
things will come back.
4:27
So then I can use the backspace there and
say, does it contain this?
4:31
Which it does, right, there's this.
4:34
So there's true, and
then also if I come and do,
4:37
let's see if it has words in there.
4:41
So this is case sensitive, right, so
4:44
it doesn't contain words even
though it is actually there.
4:46
So, we could create a new variable,
right, we could say,
4:50
String loweredwords = someWords,
and remember this is toLowerCase().
4:55
Yep, and
when there's multiple options there, so
5:02
it's gonna show us the toLowerCase(),
that's what we want.
5:04
So loweredWords, we could then check
if loweredWords contains words.
5:07
And indeed it does, but
what if we did the method chaining?
5:16
And remember this,
we could say someWords.toLowercase(), and
5:21
instead of storing that variable,
we can just access it right here.
5:24
It's called method chain,
remember we don't even need to create it.
5:28
Cool, all right, so
we also did some work with integers, or
5:34
whole numbers which were like this, right?
5:37
So if we say int ageofBob= 30, and
5:39
let's say that Mary, she's 28.
5:44
And we could check and see,
I'm gonna put parentheses around these,
5:52
just so, if Bob is older or
greater than the age of Mary.
5:58
And I'm gonna do parentheses (ageofBob
is less than the age of Mary), right?
6:07
I mean, that's how we talk about
numbers are greater than or less than.
6:15
And finally, let's go ahead and,
remember when you get a number in,
6:18
maybe you're getting in a number from
a program's coming in, you can parse it.
6:23
So let's say that somebody
parsed in the string 30,
6:29
you can get the number out with that,
there we go.
6:32
And finally,
let's remember about Booleans.
6:36
So using conditional logic like AND
or ORS, and
6:40
those look like this in Java, so
you say, OR with a double pipe.
6:43
So if we had say, false or false,
that's definitely going to be false.
6:48
But what if we put a true here,
remember what happens?
6:52
So the way that it works is, if any of
these are true, it means that it's true.
6:54
And this is the case in OR, so it's like,
is this true, or is this true, or
6:59
is this true and it is, so there's that.
7:03
And then if we flip that around,
and again I use the up arrow, and
7:05
I'm gonna remove these to be ampersands,
remember how this works.
7:09
This is basically saying,
are all of these true?
7:13
So is false and false and
true, so it's not true.
7:15
And actually what's gonna happen,
is it's gonna shortcut.
7:20
The second that it finds when it's false,
7:22
it's not even gonna need to
look at the rest of them.
7:24
So, and again, those could of course
be expressions like the age of Bob and
7:26
age of many things.
7:29
So, feel free to play around here until
you feel refreshed on what we covered.
7:30
We'll spend quite a bit of time
in here during this course.
7:34
And I'll show you a couple more cool
things that this awesome tool can do in
7:36
upcoming videos.
7:39
Now to get out of here,
you press Ctrl + D, cool and
7:40
then you can do clear and
clear the screen.
7:43
Awesome, I feel refreshed,
7:47
that REPL is going to be super
handy going forward, right?
7:49
Now before we get started,
I just wanna remind you that,
7:52
during your learnings here.
7:54
If anything at all isn't clear, or
even if you're pondering something,
7:55
head over to the community and
share your question with everyone.
7:58
By sharing your question,
it allows everyone to learn.
8:02
And if someone else is
wondering that very same thing,
8:04
it'll be answered when they
come to search for it.
8:06
I can't stress this enough, the community
is awesome and supportive, and
8:09
I've been blown away by
how quick answers appear.
8:12
I can't even keep up,
you all are so great.
8:15
All right,
8:18
I think we're ready to dive into objects,
right after you ace this refresher quiz.
8:18
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