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 Basics!
You have completed Java Basics!
Preview
In this video, we will learn what a variable is, and when they should be used. We will modify our introduction to dynamically use a String variable.
Additional Resources
- Java Class Formatter
- Other escape sequences
- More data types for the impatient
- Statically and strongly typed described (the first paragraph)
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
Let's
take a closer look at what we just did,
0:01
especially the text
we printed on the screen.
0:03
Now we're going to learn two new concepts,
strings and variables.
0:06
In Java, when you put letters
or characters inside double quotes,
0:10
you create what's called a string.
0:14
A good way to imagine a string is a banner
you might hang at a party.
0:18
It's a sequence of individual characters
strung together.
0:21
Strings are how we handle text
in programming, and you'll use them a lot.
0:26
Because they are so common, strings
are one of the basic data types in Java.
0:30
A variable is a way to store data
in a named location
0:36
that you can reference later.
0:39
In Java,
0:41
you must tell the program what kind of
data you want to store in your variable.
0:42
This helps Java know how to handle it.
0:47
For example,
string is a data type for storing text.
0:49
We'll learn about more data types
as we go,
0:53
but if you're curious now,
I've added a link in the teacher's notes.
0:55
Let's go back to our workspace
and learn how to use a string variable.
0:58
Alright, suppose I want
1:03
to print some more text,
Travis is learning Java.
1:04
We could of course do what we did here
and write console.printf,
1:08
Travis is learning Java.
1:13
Close the quotes, close the parentheses,
1:19
and don't forget our semicolon to signify
this line is complete.
1:22
Okay, let's save the file
and compile it again since we made
1:26
new changes
by running javac Introductions.java.
1:29
Then to run the program,
1:38
we type java Introductions.
1:40
So the output works
but it looks a bit messy
1:43
because everything is on a single line
1:45
Let see how we can make it look better
by moving each sentence to its own line
1:47
So if we just press Enter inside
the quotes this it's going to break things
1:52
and we'll get some errors.
1:55
Instead, we use something
called a format specifier.
1:57
The format specifier %n means new line.
2:00
Adding this at the end of a line moves
the next output to a new line.
2:04
Here, let's add one
to the end of each of our lines
2:09
before the ending double quotes.
2:11
Now, because I squished my
2:16
console window down really low,
it got rid of all of my old output,
2:17
but you can clear yours
by entering the clear command.
2:20
And here's a handy terminal trick.
2:25
Press the up arrow key
to scroll through your previous commands.
2:26
So make sure your file is saved,
and let's find the command javac
2:30
introductions.java and run it again.
2:34
we'll run it with Java introductions.
2:41
And now it looks much better,
and it's definitely easier to read.
2:44
Now, what if we want this program
to be about someone else?
2:48
One way is to change every instance
of the name
2:52
Travis in the code,
or whichever name you're using.
2:54
But imagine if our program was huge,
and the name appeared hundreds
2:57
or even thousands of times.
3:01
That would be a nightmare to update.
3:03
A better way is to store
the name in a variable.
3:05
Here's how we declare a variable in Java.
3:08
First, we define its type,
3:11
and since we know
we want a string variable, we type that.
3:12
Notice it's capitalized.
3:16
Then we give it a name that we want
to reference later on in the code.
3:18
The better we name our variables,
the easier it is for others, or ourselves
3:22
in the future to understand its purpose.
So let's name this variable firstName.
3:26
Then we do an equal
3:33
sign and now we give it its value.
3:34
Since it's a string we do our double quotes
and I'll use my name again.
3:37
Make sure you close your quotes
and put in your semicolon.
3:42
So again, this line of code creates
a new variable with the name of firstName.
3:45
We're telling Java
that this variable contains a string,
3:50
and I'm giving mine a value of Travis.
3:54
Then the semicolon at the end of the line
3:57
tells Java that the statement is finished.
3:58
In Java, though
not required, the standard for naming
4:02
variables is what is called camel case.
4:05
The first letter of the first word
is lowercase,
4:08
and we start every new word
with a capital letter, like this here.
4:10
This is an example of camel casing.
4:15
You can name your variables
pretty much anything you like,
4:19
with a few exceptions,
but giving your variables self-explanatory
4:21
names correctly helps others to understand
your code better.
4:25
Earlier, I mentioned that
this printf method prints text,
4:28
but to put it more precisely,
it prints formatted text.
4:32
That's what the F stands for.
4:36
It's also why we were able to use this %n
here, that format specifier.
4:38
You may also commonly see a backslash n
for a new line when using other methods,
4:43
but this %n works
nicely across different platforms.
4:48
This printf also allows us to use
4:52
other special placeholders
which we can replace with variables.
4:54
For example, instead of writing our name
directly in the string,
4:58
we can use %s as a placeholder
for a string.
5:01
Printf happens
to take multiple options, or parameters.
5:05
So the first parameter inside printf
is the format string.
5:10
That's our string here in the quotes.
5:14
Additional parameters
come after it, separated by commas.
5:16
These values will replace the placeholders
in the format string
5:20
So after our ending double quotes
but still inside our parentheses
5:24
we can place a comma
and then add in our first name variable
5:28
Let also do the same down here.
5:34
Let's replace our name with %s,
5:36
add our comma after the quotes,
5:40
and then add our first name variable.
5:45
Nice.
5:48
Let's save and use the up arrow key
to find our previous commands
5:49
compile
5:54
and run again.
5:59
And there we go! Now let's change
6:01
the value of firstName to a friend's name.
6:03
I'll change mine to Brian,
6:06
save, clear,
6:11
compile,
6:21
and run again.
6:24
So we changed our name in one place,
our variable,
6:27
and as expected,
both lines are updated with Brian's name.
6:30
Great!
6:33
Variables to the rescue!
6:34
To sum things up,
when you create a variable,
6:37
you specify its data type first, like string,
6:40
Then you give it a name
and assign a value using an equal sign.
6:44
Strings are created by surrounding text
with double quotes.
6:49
And every statement
in Java ends with a semicolon.
6:52
So that is the syntax, or
structural rules, for creating a variable.
6:57
One thing of note is
Java is case-sensitive,
7:01
you know, uppercase and lowercase.
7:04
So make sure you're using your variables
written exactly as you declared them.
7:06
Nice job!
7:11
Now let's do an exercise to practice all
these new ideas and make sure they stick.
7:12
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