JavaDevNotes.com

Java Tutorial - Variables and Assignment Statements

Variables are a fundamental component of any programming language. This tutorial will give a brief introduction to variables in Java.

Introduction

Variables are container for data that are stored in computer's memory. When variables are created, memory space are reserved for storage.

Here is a sample code that uses a variable:

public class Sample {
  public static void main(String[] args) {
    int num = 10;
    System.out.println("The number is: "+ num);
  }
}

When we run this, we get the output:

The number is: 10

Here is the code section from above that declares a variable.

int num = 10;

  • int is the type of the variable declared. In this case, int stands for integer. Integers are whole numbers (no decimal point) and can either be negative, positive, or zero.
  • num is the name of the variable.
  • = is the assignment operator. It tells to store the value 10 to the variable num. Hence, after the statement, num gets the value of 10.

Different variables refers to different data

Consider this example:

public class Sample {
  public static void main(String[] args) {
    int a = 50;
    int b = 75;
    System.out.println("The first number is: "+ a);
    System.out.println("The second number is: "+ b);
  }
}

The output of this is:

The first number is: 50
The second number is: 75

  • a is a variable of type int and assigned a value of 50
  • b is another variable of type int and assigned a value of 75
  • a and b are different from each other because they have different names

Manipulating variables

The value stored to a variable can be updated. Consider this example:
public class Sample {
  public static void main(String[] args) {
    int num = 20;
    num = num + 5;
    System.out.println("The result is: "+ num);
  }
}

The output of this is:

The result is: 25

  • num is a variable of type int and assigned a value of 20
  • num's value is updated with the statement num = num + 5
    • num + 5 is evaluated first
    • num is 20, by adding 5 the outcome is 25
    • 25 is then assigned to num
    • after the statement, num gets the value 25

Case Sensitivity

Consider this example:

public class Sample {
  public static void main(String[] args) {
    int a = 3;
    int A = 7;
    System.out.println("The first number is: "+ a);
    System.out.println("The second number is: "+ A);
  }
}

The output of this is:

The first number is: 3
The second number is: 7

  • a and A are different from each other. This is because Java is case sensitive.
  • a has the vale of 3
  • A has the vale of 7

Keywords and reserved words

In Java, there are words that have special meaning. They are usually called keywords or reserved words. Variables must NOT use keywords or reserved words as name.
You can NOT use these as variable names:
abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while

Valid variable names

Here are the rules for naming variables:
  • Must be one or more characters long
  • First character must either be:
    • letter (a to z and A to Z)
    • underscore (_)
    • dollar sign ($)
  • Succeeding characters can be:
    • letter (a to z and A to ZZ)
    • underscore (_)
    • dollar sign ($)
    • number (0 to 9)
Sample of valid variable names:
  • num - starts with a letter and succeeding characters are letters
  • _test01 - starts with an underscore and succeeding characters are letters/numbers
  • $hello - starts with a dollar sign and succeeding characters are letters
Sample of invalid variable names:
  • 100percentile - can't start with a number
  • test# - can't use # as part of a name
  • byte - can't use a reserved word.

Notes

Future tutorials will show how to use variables in different ways.

Exercises


  1. public class ExampleA {
      public static void main(String[] args) {
        int a = 10;
        int b = a;
        System.out.println("The value of b is: "+ b);
      }
    }

  2. public class ExampleB {
      public static void main(String[] args) {
        int a = 10;
        int b = a;
        a = 15;
        System.out.println("The value of b is: "+ b);
      }
    }

  3. public class ExampleC {
      public static void main(String[] args) {
        int a = 10;
        int b = A;
        System.out.println("The value of b is: "+ b);
      }
    }

  4. public class ExampleD {
      public static void main(String[] args) {
        int a = 20;
        int b = 15;
        int c = a + b;
        System.out.println("The value of c is: "+ c);
      }
    }

  5. public class ExampleE {
      public static void main(String[] args) {
        int 1a = 10;
        System.out.println("The value of 1a is: "+ 1a);
      }
    }
Answers


  1. The output is:
    The value of b is: 10
    
    Explanation: The content of variable a is 10. The value of a (which is 10) is then copied to b. The variable b will then have the value of 10.

  2. The output is:
    The value of b is: 10
    
    Explanation: The content of variable a is initially 10 which is then copied to b. Modificatin to a later will not affect the value of b. The variable b will still have the value of 10.
  3. The program will not run as the variable A is not declared. What's declared is the variable a. Remember that Java is case sensitive.

  4. The output is:
    The value of c is: 35
    
    Explanation: The content of variable a is 20 and b is 15. The two are added together and copied the result to variable c. The variable c will then have the value of 35.
  5. The program will not run as 1a is not a valid variable name. A variable name can't start with a numeric digit.
Tags: beginner tutorial, console