Login    New User    Forgot Password    Help      

What is a Variable?

Java Tutorials

A variable is a container that stores a meaningful value that can be used throughout a program. For example, in a program that calculates tax on items you can have a few variables - one variable that stores the regular price of an item and another variable that stores the total price of an item after the tax is calculated on it. Variables store this information in a computer's memory and the value of a variable can change all through out a program.

Declaring variables

One variable in your program can store numeric data while another variable can store text data. Java has special keywords to signify what type of data each variable stores. Use these keywords when declaring your variables to set the data type of the variable.

Java data types
Keyword Type of data the variable will store Size in memory
boolean true/false value 1 bit
byte byte size integer 8 bits
char a single character 16 bits
double double precision floating point decimal number 64 bits
float single precision floating point decimal number 32 bits
int a whole number 32 bits
long a whole number (used for long numbers) 64 bits
short a whole number (used for short numbers) 16 bits

Example


Example:
char c;
int i;

c='a';

i=10;

NOTE: A variable must be declared with a data type or an error will be generated! The data type of a variable should be used only once with the variable name - during declaration. After that, you can refer to the variable by its name without the data type.

Naming variables

Rules that must be followed when naming variables or errors will be generated and your program will not work:

  • No spaces in variable names
  • No special symbols in variable names such as !@#%^&*
  • Variable names can only contain letters, numbers, and the underscore ( _ ) symbol
  • Variable names can not start with numbers, only letters or the underscore ( _ ) symbol (but variable names can contain numbers)

Recommended practices (make working with variables easier and help clear up ambiguity in code):

  • Make sure that the variable name is descriptive of what it stores - For example, if you have a variable which stores a value specifying the amount of chairs in a room, name it "numChairs".
  • Make sure the variable name is of appropriate length - Should be long enough to be descriptive, but not too long.

Also keep in mind:

  • Distinguish between uppercase and lowercase - Java is a case sensitive language which means that the variables varOne, VarOne, and VARONE are three separate variables!
  • When referring to existing variables, be careful about spelling - If you try to reference an existing variable and make a spelling mistake, an error will be generated.

Printing variables

Variables are printed by including the variable name in a System.out.print() or System.out.println() method. When printing the value of a variable, the variable name should NOT be included in double quotes. You can also print variables together with regular text. To do this, use the + symbol to join the text and variable values.

Example

class PrintText{

public static void main(String[] args){

//declare some variables
byte aByte = -10;
int aNumber = 10;
char aChar = 'b';
boolean isBoolean = true;

//print variables alone
System.out.println(aByte);
System.out.println(aNumber);

//print variables with text
System.out.println("aChar = " + aChar);
System.out.println("Is the isBoolean
variable a boolean variable? " + isBoolean);
}

}

Output

-10
10
aChar = b
Is the isBoolean variable a boolean variable? true

Java Tutorials