A few words about variables in Android programming
Hello guys! I didn’t manage to write this post yesterday because I engaged myself in the practical part so much that I fell asleep as soon as I finished. Today, on the other hand, I am starting with the post and won’t continue with my Android app development course unless this post is published.
If you have ever had anything to do with programming, you probably know very well what variables are. As Android programming is basically Java, the knowledge of variables and their use is for you fundamental. In this post I will quickly introduce the subject of variables in Android and give you some real-life examples. Let’s start!
What Variables Are
You will probably find tons of different definitions of variables on the Internet and in various courses. The one I like the best comes from Android for All where you can find the meaning of all the Android-related terms. So what is a variable, what it does and where do we use it?
- It’s container (you can think of it as a box) which keeps a value in it. A value can be numbers or text (it’s up to you how you define it).
- Each variable has its name which is written in Camel Case and starts with a lowercase letter. For example: currentQuantity or dogsName.
- It’s called a variable because its value can change (it doesn’t have a fixed value).
- Variables are placed in .java files because Android is based on Java.
Creating Variables In Android Studio
Creating variables in Android is actually very simple. You don’t have to have any previous experience in programming to start writing simple Android apps which make use of variables.
I have created an extremely simple app (with the help and guidance of the Udacity course I am now completing) which calculates the total price for coffee you have to pay depending on the number of coffees you order). This is how the app looks. Initially it shows the quantity and price of 0.
If you hit the ORDER button, then the app calculates the price based on the value of the variable.
Let’s now analyze the three lines of code in which we create and use variables in Android Studio.
Real-life examples of variables in my code
As Android is basically Java, the below lines of code come from the MainActivity.java file that is automatically generated when you create a new app in Android Studio. Here is what they are:
Let’s now break down each line and explain what exactly it does.
You create or define variables in Java by simply following a certain formula which is:
- In the first part of the formula you define what data type this variable will refer to. In the case of the below code “int” means integer (a whole number, any number including zero and negative numbers).
- Next comes the variable name (written in Camel Case and starts with a lowercase letter). It’s totally up to you what name you will choose but it should be descriptive and should indicate what this variable refers to (not too long and not too short). We can compare it to a tag name on this container (variable).
- In the last part (blue square) you put the initial value of a variable. The value we place should be in accordance with the data type we defined in the beginning.
- The equal sign is the assignment operator. According to the rules of Java (and, thus, of Android) it means assigning a value to a variable.
- The statement ends with a semicolon according to the rules of Java (and other programming languages as well).
int numberOfCoffees = 2;
Putting the above line of code in a plain language, we can say that we place 2 in the box with name numberOfCoffees (and assign the initial value of numberOfCoffees to be 2) on it.
Using variables in Android
In the above examples we defined variables. The below two lines of code are the examples of using variables. As you can see we don’t use values but only variable names to refer to them.
display (numberOfCoffees) ;
This line of code simply displays the defined value of variable numberOfCoffes after the app is run. Since we set it to 5, it will display 5 on the screen (and it does).
displayPrice (numberOfCoffees * 5) ;
This line of code is used to calculate the price based on the formula which is the value of variable numberOfCoffees multiplied by 5.
Of course, the above three lines of code are a very simple example of variables in Android. I decided to show them to you so that you can grasp the idea behind variables and never have many problems with them (when you get to the more advanced stuff).
If you have any comments or questions, feel free to use the below comment box. 🙂