Java for students logo
Need help with Java programming assignment? Get affordable Java homework help.

Variables in Java

All programs, except the simplest ones, require storing some data during running time. It could be input from user, data from file, result of calculations inside the program and so on and so forth. Variables provide mechanism to store data inside a program.

A variable has

  • a type: what one could store inside, what one can do with variable;
  • an identifier: a name of a variable. Using the name one refers to variable in the code;
  • an initializer (optional): you can initialize a variable with a value;

Below we show several examples defining variables:

Code
Explanation
int a;  Define a variable called "a" of type int.

int b = 5;

Define a variable called "b" of type int and initialize it with 5.
int a, b;   Define two variables called "a" and "b".

int a = 5, b;

Define two variables called "a" and "b". Variable "a" is initialized with 5.

Variable's value could be changed using the assignment operator:

a = 5;
a = b + 5;
a = a + b;

Note. Before you are allowed to use a variable it must be initialized. Otherwise program won't compile:

Code

public class Variables {

      public static void main(String[] args) {

            int byteVar;

            // byteVar isn't initialized

            System.out.println("byte variable's value: " + byteVar);        

      }

}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

      The local variable byteVar may not have been initialized

 

      at Variables.main(Variables.java:5)

Which names are allowed

Variable names in Java are case-sensitive. Variable name:

  1. a sequence of Unicode letters and digits of unlimited length;
  2. can begin with a letter, the dollar sign "$", or the underscore character "_";
  3. whitespaces in name is not permitted;
  4. must not be a keyword or reserved word.
Allowed names
Disallowed names
  • name
  • _last_name
  • $myvar
  • RANDOM_NUMBER
  • my2ndName
  • 2nd_name (can't start with a number)
  • n! ("!" is not allowed)
  • last name (whitespaces are not allowed)

 

Above we showed the names, which are correct from the viewpoint of Java language's syntax. But Java also has naming conventions and we strongly recommend you to name your variables according to them. There are simple rules of giving good names. Variables's name:

  1. starts with a letter;
  2. uses camel notation: start name with a small letter. If name consists of two words or more, start all the other words with capitals;
  3. doesn't contain "$" and "_" characters.
Names violating convention
Names complying with convention
  • last_name
  • $myvar
  • RANDOM_NUMBER (good name for a constant)
  • my2ndName
  • lastName
  • myVar
  • randomNumber
  • mySecondName

Example

Variables.java

public class Variables {

      public static void main(String[] args) {

            int a = 5, b, c;

            b = a + 5;

            c = a * b;

            System.out.println("a: " + a);

            System.out.println("b: " + b);

            System.out.println("c: " + c);          

      }

}

Output

a: 5
b: 10
c: 50

 

Partners Ads