Expressing yourself demo app
Let's try using some declarations, assignments, and operators. When we bundle these elements together into some meaningful syntax, we call it an expression. So let's write a quick app to try some out. We will then use Toast
and Log
to check our results.
Create a new project called Expressing Yourself
, use a Blank Activity, and leave all the other settings at their defaults. The completed code that we will write in this project can be found in the Chapter 7/Expressing Yourself
folder of the download bundle.
Switch to the MainActivity tab in the editor and we will write some code. In the onCreate
method, just before the closing curly brace }
, add this code:
int numMessages;
Directly below the previous line of code we will assign a value to numMessages
. But as you begin to type nu
notice we get a little pop-up message like this:
If you look at the first option in the pop-up message it is in fact numMessages
. Android Studio is offering to complete our typing for us. We can either left-click numMessages
to have the variable name auto-completed or simply select it because it is already highlighted by pressing Enter on the keyboard. Whenever Android Studio thinks it might know what we want to type, it will pop up some options. It is a great time-saving habit to get into.
Add this line of code using auto-complete:
numMessages = 10;
Now that we have introduced the really handy code completion feature we will add a larger chunk of code. Immediately after the previous line of code and before the closing }
of onCreate
, add the following code:
// Output the value of numMessages Log.i("numMessages = ", "" + numMessages); numMessages++; numMessages = numMessages + 1; Log.i("numMessages = ", "" + numMessages); // Now a boolean (just true or false) boolean isFriend = true; Log.i("isFriend = ", "" + isFriend); // A contact and an important message String contact = "James Gosling"; String message = "Dear reader, I invented Java."; // Now let's play with those String variables Toast.makeText(this, "Message from" + contact, Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Message is:" + message, Toast.LENGTH_SHORT).show();
Run the app and we can examine the output and then the code. In the logcat window, you will see the following output:
numMessages =﹕ 10 numMessages =﹕ 12 isFriend =﹕ true
On the screen, you will see two pop-up Toast
messages. The first says Message from James Gosling. The second says Message is: Dear Reader, I invented Java. and is shown in the next screenshot:
Let's step through the code and make sure that each line is clear before moving on.
First, we declared and initialized an int
type variable called numMessages
. We could have done it on one line but we did it like this:
int numMessages; numMessages = 10;
Next we used Log
to output a message. Instead of simply typing the message between the double quote marks ""
, this time we used the +
operator to add numMessages
onto the output. And as we saw in the console, the actual value of numMessages
was output:
// Output the value of numMessages Log.i("numMessages = ", "" + numMessages);
Just to further prove that our numMessages
variable is as versatile as it should be, we use the ++
operator, which should increase its value by one and then add numMessages
to itself +1
. We then output the new value of numMessages
and indeed found its value was increased to 12
from 10
:
numMessages ++; numMessages = numMessages + 1; Log.i("numMessages = ", "" + numMessages);
Next, we created a boolean
type variable called isFriend
and output that to the console. We saw from the output that true
was displayed. This variable type will fully demonstrate its usefulness when we look at decision-making in the next section:
// Now a boolean (just true or false) boolean isFriend = true; Log.i("isFriend = ", "" + isFriend);
After this, we declared and initialized two String
type variables:
// A contact and an important message String contact = "James Gosling"; String message = "Dear reader, I invented Java.";
Finally, we output the String
variables using Toast
. We used a hard-coded part of the message "Message from "
and added the variable part of the message with + contact
. We used the same technique to form the second Toast
message as well. When we add two Strings together to make a longer String, it is called concatenation:
// Now let's play with those String variables Toast.makeText(this, "Message from " + contact, Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Message is:" + message, Toast.LENGTH_SHORT).show();
Now we can declare variables, initialize them to a value, change them around a bit, and output them using Toast
or Log
. Now let's look at how we can make decisions based on the value of these variables and how this is useful to us.