Variables

Updated: 3 years ago

Check out other lessons in Learning the Java syntax

Let's not end our learning with a simple display command, it's time for some variables. Variables help us keep track values inside our applications.

Variables are symbolic names associated with a value. You've probably encountered them in math. If you have a statement x = 10 + 7, x is the variable. In this case, x will hold a value of 17. Variables are used all the time in programming to hold different values and are an important part of every application.

Different programming languages have different rules for variables. Java is a statically typed programming language, which means that every variable requires a type. For example, in Java you have to tell if you have 16 oranges or 16 apples. You have a value (16) and a type (either oranges or apples).

Declaring a variable

Let's create our first variable. We will create a variable called age that will hold our age. Its type will be an int which is a primitive type for storing numbers. For the time being just remember that int will tell the compiler that our variable is a whole number. We'll cover primitive types in the next lesson.

 public class Main
 {
     public static void main(String[] args)
     {
          // Let's declare our age variable
          int age = 27;
     }
 }

We first tell the compiler the type (an int), then we tell the name of the variable (in our case its age). Then comes the = which tells Java to assign whatever is on the right side to the variable on the left side. We tell the compiler that we're declaring an int variable with the name age and assigning it a value of 27.

Reassigning a variable

We can reassign variables to different values. Below we are going to first add 1 to our current age and then reassign it to the age variable. When assigning values, just remember that the right side of the = gets evaluated first (meaning all of the math executes first), and then it assigns.

 public class Main
 {
     public static void main(String[] args)
     {
        // Let's declare our age variable
        int age = 27;

        // We had a birthday! We're now a year older.
        age = age + 1;
     }
 }

Because the compiler already knows that age is of type int, we don't need to tell it anymore. We just assign a different value to age and we're done.

Hint! age++ or age--

There's also a nifty way of increasing or decreasing the value by 1. Instead of doing age = age + 1 you can do age++. This will automatically add 1 to age. You can also use age-- to decrement by 1.

Outputting numbers

Guess what. System.out.println() is actually pretty special, and you can put numbers in parenthesis as well.

 public class Main
 {
     public static void main(String[] args)
     {
          // Let's declare our age variable
          int age = 27;

         // We had a birthday! We're not a year older.
         age = age + 1;

         // Let's use System.out.println to out our age
         System.out.println("My age is:");
         System.out.println(age);
     }
 }

Voila!

« Previous lesson:
Our first Java application
» Next lesson:
Primitive types