Local Variables

Find it in GitHub

In my opinion, the first thing a novice programmer should learn about a language, of course after successfully compiling and running their first HelloWorld application, is how to define a variable.

In basic mathematics, if you remember we had two types of variables: dependent and independent. In y = 2x, y is a dependent variable that its value depends on x variable and its value is twice of x which is an independent.

In programming, like mathematics, variables holds the values and they are representative of values that dependently or independently are changing.

In Java though, you define a variable to store a value whether it is constant or static value or it is a calculated value of a formula or expression.
The first thing you need to know about variable in Java or in general in all programming languages is that a variable has a type. It can be primitive type like integer, float, character or it can be a string of characters or even a reference to an instance of a class and etc. To know more about the types of variable, start reading about Java Primitive Types here.

Regardless of what is the type of variable, the rule to define a variable is first you must mention the type of the variable and then the variable name. Here is an example of an integer variable called temperature.

  int temperature;

If you are going to assign a value like 100 to this variable, based on its type you put = (equal) sign in front of the variable and then the value like this:

 int temperature = 100;

I imagine you only know how to define the main method in Java and you have just finished Get Started section. So I limit defining the variable inside the main method block which we call these types of variables: local variables

You define a local variable inside the method block which means inside two curly branes. Here is an example of how I define the same temperature inside main method.

public class TemperatureConverter {
   public static void main(String[] args) {
       int tempInFahrenheit = 350;
       float tempInCelsius = (tempInFahrenheit - 32f) * (5f/9f);
       System.out.println("Fahrenheit: " + tempInFahrenheit + "˚F");
       System.out.println("Celsius: " + tempInCelsius + "˚C");
   }
 }

In this example, I defined an integer variable called tempInFahrenheit and I assigned 350 to it which represents 350˚F. To calculate the temperature in celsius, I had to define a float variable since the formula has division (5/9) with result of a floating point value. I called that variable tempInCelsius and I assigned its value to (tempInFahrenheit – 32f) * (5f/9f). In Java in order to work with different types of numeric values such as Integer, Float, Hex and etc. you need to somehow mention the type of the value by adding one or some characters before or after it. For example to make sure 5 divided by 9 gives you floating result, instead of 5/9 that gives you 0 because java compiler rounds it, you need to use 5f/7f while the character “f” indicates that the value is a float value.
At the end, I printed Fahrenheit value in one line and the calculated Celsius in the next line.

These two variable are only accessible in main method block or in general inside the method that you define them. That’s why you call them local variable.

You can compile the source code like this:

$ javac TemperatureConverter.java

And when you run it, you will see following lines in the console:

$ java TemperatureConverter
Fahrenheit: 350˚F
Celsius: 176.66667˚C

Find the source code of this example in GitHub:
https://github.com/myjavacoretutorial/local-variable.git

Leave a Reply