Advertisement
English transcript
Earlier in the lesson we learned about how to declare or
create an integer variable.
We're going to follow the same format to create a variable, but
this time the variable holds text because it's a string.
This is the code we added to our app.
This creates a new string variable called priceMessage.
And the value that it holds is the text Free.
It follows a specific format according to the syntax rules of Java.
First, we specify the data type.
In this case, it's a string.
Remember that a string is just a sequence of characters.
Then it's followed by the variable name, which is priceMessage in this case.
Then it's followed by an equal sign, initial value,
and a semicolon to indicate the end of the statement.
Saying that the data type is a string limits what type of information can go
inside this variable.
Only one string can be stored in it at a time.
A string can be made up of letters, numbers, or symbols.
You may recall that a variable name is the name that we give this variable, so
we can refer to it later, to get the value out or to update the value.
It's like giving the variable a name tag.
When you're picking a variable name, it's important to choose something
that's descriptive, that describes what type of information it will be holding.
I could call this variable name Catherine, but
it really does not reflect the information inside of it.
It makes my code harder to read as well,
because no one knows why there's a string variable called Catherine.
On the other hand,
a variable name like priceMessage is a little more helpful, because you know
there's a message inside of it that's related to the price probably.
We looked at this earlier, but there are naming conventions for variable names.
If you forget what those are, you can always look them up.
I just search for variable name Java in Google Search.
I can scroll down here to where it talks about naming.
It says that variable names are case-sensitive, and
you usually start with a lowercase letter.
And then you use camel case if there's multiple words.
Next we have the assignment operator, or the equal sign here.
This operator is in charge of assigning a value to a variable.
So you take this value on the right-hand side and
you assign it into the variable priceMessage.
We specify the initial value of this string to be some text and
double quotes.
This is called a string literal, because a literal has a fixed value.
We first heard about literals when we were learning about integer variables.
A fixed value like two or five or ten are all integer literal
values because they don't change, as opposed to a variable which does change.
Going back to our box analogy, the value,
Free, would go inside this variable.
And last but not least, we also have a semicolon at the end of the statement.
Now that we understand this line of code a little better, go ahead and
try to experiment with different values.
You can try to change the initial value here or change the variable name.
For example, you can use a short string like Free or
change it to be a longer string.
You could make it a completely empty string as well,
which leaves no characters in between the two quote signs.
You could add a space, or you could add numbers as well, or
you could add symbols.
When you're creating your string literal, just be careful
that there are certain characters that you need to deal with specially.
For example, if you use the quote sign in your string,
then you're going to run into an error that says,
can't resolve symbol dollar and a semicolon is expected.
This is because the compiler doesn't know that the whole string is supposed
to be this whole thing.
To solve this problem, let's do a Google search.
In Google, search for Java escape characters.
And scroll down to a section called escape sequences.
In Java, certain characters mean special things.
The start and end quote symbols are to denote a string in Java, but
if you want to actually put a quote inside your string,
you'll need to use an escape sequence.
An escape sequence is a character preceded by a backslash, and
the Java compiler will interpret it in a special way.
If we use a backslash with a double quote, that indicates that we actually
want to use the quote symbol, not the quote that means it's a text string.
Let me show you what I mean.
In Android Studio I can add a backslash before each
quote mark that I actually want to appear in the text.
Now, the whole initial value of this variable will be this whole string,
because it's marked off by the certain end quotes.
And inside that text string, I also want to use quotes, but
I mark those by using a backslash in front of each quote.
If I run on my device, I see the quotes around one dollar, but
I don't see the quotes around the full thing ,which is what we expect.
Another useful escape sequence to know is this backslash n,
which inserts a new line so that the text will continue on to the next line.
You can try this one out in your code as well.
Remember that the semicolon should always be at the end of the statement.
Even though in English it's grammatically correct to put the
punctuation inside the quote, you want to put it on the outside of the quote.
Anything in between here is part of the text string.
And you need to put the semicolon on the outside for the Java compiler.
You can also modify the variable name.
If I change this to something like message, then this gives me an error
here, because it can't resolve the symbol price message.
So I just need to copy it and put it down here as well.
And this should run on your device.
You can't change the data type, though.
It must be spelled string exactly like this.
String is capitalized here because it's an object.
We'll learn about objects in Lesson 3, but for
now just type string with a capital S.
When we were working with integers earlier,
we would declare integers with a lowercase i for int.
It's lowercase because it's a primitive data type,
where primitive means it's one of the basic Java data types.
Experiment with different variable names and
initial values until you're comfortable with creating string variables.
Then I want you to tell me what's incorrect about each of these string
declarations listed below.
I want you to point out the error in each of these lines of code.
create an integer variable.
We're going to follow the same format to create a variable, but
this time the variable holds text because it's a string.
This is the code we added to our app.
This creates a new string variable called priceMessage.
And the value that it holds is the text Free.
It follows a specific format according to the syntax rules of Java.
First, we specify the data type.
In this case, it's a string.
Remember that a string is just a sequence of characters.
Then it's followed by the variable name, which is priceMessage in this case.
Then it's followed by an equal sign, initial value,
and a semicolon to indicate the end of the statement.
Saying that the data type is a string limits what type of information can go
inside this variable.
Only one string can be stored in it at a time.
A string can be made up of letters, numbers, or symbols.
You may recall that a variable name is the name that we give this variable, so
we can refer to it later, to get the value out or to update the value.
It's like giving the variable a name tag.
When you're picking a variable name, it's important to choose something
that's descriptive, that describes what type of information it will be holding.
I could call this variable name Catherine, but
it really does not reflect the information inside of it.
It makes my code harder to read as well,
because no one knows why there's a string variable called Catherine.
On the other hand,
a variable name like priceMessage is a little more helpful, because you know
there's a message inside of it that's related to the price probably.
We looked at this earlier, but there are naming conventions for variable names.
If you forget what those are, you can always look them up.
I just search for variable name Java in Google Search.
I can scroll down here to where it talks about naming.
It says that variable names are case-sensitive, and
you usually start with a lowercase letter.
And then you use camel case if there's multiple words.
Next we have the assignment operator, or the equal sign here.
This operator is in charge of assigning a value to a variable.
So you take this value on the right-hand side and
you assign it into the variable priceMessage.
We specify the initial value of this string to be some text and
double quotes.
This is called a string literal, because a literal has a fixed value.
We first heard about literals when we were learning about integer variables.
A fixed value like two or five or ten are all integer literal
values because they don't change, as opposed to a variable which does change.
Going back to our box analogy, the value,
Free, would go inside this variable.
And last but not least, we also have a semicolon at the end of the statement.
Now that we understand this line of code a little better, go ahead and
try to experiment with different values.
You can try to change the initial value here or change the variable name.
For example, you can use a short string like Free or
change it to be a longer string.
You could make it a completely empty string as well,
which leaves no characters in between the two quote signs.
You could add a space, or you could add numbers as well, or
you could add symbols.
When you're creating your string literal, just be careful
that there are certain characters that you need to deal with specially.
For example, if you use the quote sign in your string,
then you're going to run into an error that says,
can't resolve symbol dollar and a semicolon is expected.
This is because the compiler doesn't know that the whole string is supposed
to be this whole thing.
To solve this problem, let's do a Google search.
In Google, search for Java escape characters.
And scroll down to a section called escape sequences.
In Java, certain characters mean special things.
The start and end quote symbols are to denote a string in Java, but
if you want to actually put a quote inside your string,
you'll need to use an escape sequence.
An escape sequence is a character preceded by a backslash, and
the Java compiler will interpret it in a special way.
If we use a backslash with a double quote, that indicates that we actually
want to use the quote symbol, not the quote that means it's a text string.
Let me show you what I mean.
In Android Studio I can add a backslash before each
quote mark that I actually want to appear in the text.
Now, the whole initial value of this variable will be this whole string,
because it's marked off by the certain end quotes.
And inside that text string, I also want to use quotes, but
I mark those by using a backslash in front of each quote.
If I run on my device, I see the quotes around one dollar, but
I don't see the quotes around the full thing ,which is what we expect.
Another useful escape sequence to know is this backslash n,
which inserts a new line so that the text will continue on to the next line.
You can try this one out in your code as well.
Remember that the semicolon should always be at the end of the statement.
Even though in English it's grammatically correct to put the
punctuation inside the quote, you want to put it on the outside of the quote.
Anything in between here is part of the text string.
And you need to put the semicolon on the outside for the Java compiler.
You can also modify the variable name.
If I change this to something like message, then this gives me an error
here, because it can't resolve the symbol price message.
So I just need to copy it and put it down here as well.
And this should run on your device.
You can't change the data type, though.
It must be spelled string exactly like this.
String is capitalized here because it's an object.
We'll learn about objects in Lesson 3, but for
now just type string with a capital S.
When we were working with integers earlier,
we would declare integers with a lowercase i for int.
It's lowercase because it's a primitive data type,
where primitive means it's one of the basic Java data types.
Experiment with different variable names and
initial values until you're comfortable with creating string variables.
Then I want you to tell me what's incorrect about each of these string
declarations listed below.
I want you to point out the error in each of these lines of code.