Introduction to Constants: Android Studio Crash Course (Free Tutorial)

Ready to get ahead of the game? Today you’ll be learning something that will give you an edge in computer science. Although constants aren’t often used by programmers, they increase the readability of programs. In this tutorial, we will use a simple math example to see constants’ usefulness in action. If you are a beginner and want to start with the basics of coding for FREE, check out our 30-minute introductory course here:

To follow along with this example in Android Studio, go into Project view. Then go to app > java > (topmost) com.example.zebra.demo > MainActivity. Feel free to hide Project view.

To understand the purpose of constants in programming, let’s look at a simple math problem: if the radius of a circle is 2.54 inches, find the circumference of the circle in centimeters.

To start off, initialize a double variable using the keyword Double. Double variables allow our numbers to have decimal places. Call it radiusInInches, and assign it the value 2.54. Do so by typing the following code beneath setContentView(R.layout.activity_main);:

Double radiusInInches = 2.54;

The next variable to declare is one for the circumference (perimeter) of the circle. Let’s call it perimeterInCentimeters. Assign it the value of the mathematical formula for the circumference of a circle: 2 * pi * r = 2 * 3.14 * 2.54. Note that we also have to multiply the value by 2.54 because that’s approximately the number of centimeters in one inch.

 Double perimeterInCentimeters = 2 * 3.14 * 2.54 * 2.54;

Although our problem is technically solved, our code isn’t quite easy to understand. Later on, when you read this code again, you may wonder what the numbers being multiplied mean. Since our radius is 2.54 and that number appears twice in our formula, you might be tempted to say that we are actually using the formula for the AREA of a circle (pi * r^2).

This is where constants come in. They make code easier to understand. The key word final is used to declare a constant. Note that we can declare constants from any data type desired. In this case, we’ll declare a double constant because our number contains decimal places. On a line between your two declared variables, declare a double constant named CENTIMETERS_IN_ONE_INCH. Set it equal to 2.54.

final Double CENTIMETERS_IN_ONE_INCH = 2.54;

Notice the difference in the naming conventions for constants and variables. As you can see, the names of variables begin with a lowercase letter, and all the other words in the name begin with an uppercase letter. For the names of constants, all letters are in uppercase, and the words are separated with underscores.

Now let’s replace the numbers in perimeterInCentimeters with more meaningful names. Change the first 2.54 in perimeterInCentimeters to radiusInInches. Replace the remaining 2.54 in that line with CENTIMETERS_IN_ONE_INCH.

Hooray! The code looks much easier to understand. However, there’s one more problem we have to tackle: the “3.14”. There’s a predefined constant in Android Studio called Math.PI. It is accurate to 15 decimal places of Pi, which minimizes round-off errors. You can replace 3.14 with this constant for better precision.

That’s that! Now the programs you create will be much more easily read. This is helpful because code is much more often read than written. If you want to improve your coding skills even further, check out our FREE 30-minute beginners course here:

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.