Variables in Java are used to store information that may change through the course of the program, either through computation or user input. There are two main types of variables: primitive and reference.
When naming variables, programmers should choose names that indicate the value that the variable holds. For example, itemCost is much more descriptive than x. Also, when naming a variable in Java the following rules apply:
The variable name must begin with a letter
Variable names can only contains letters, numbers and underscores
Variable names cannot be reserved words (e.g. class, double, main, etc).
It is a convention in Java to name variables beginning with lower case letters and using capitalization to enhance readability (camelCase)
The process of declaring a variable is giving it a type and a name. Variable declarations can happen on different lines (lines 1 and 2), or on the same line if they are the same type (line 3).
int age;
int year;
String firstName, lastName;
The process of assigning a variable is to give it a value. When assigning a variable, the variable goes on the left, followed by an equals sign (=), and the value on the right. Variables can be assigned to literals (programmer defined values), input results, or computations.
Basic computation operators include +, -, *, /, and %. Modulus (%) is an operation that gives the remainder when two values are divided.
When assigning String values, we surround the string in double quotes (line 3). For char type variables, we surround the character in single quotes (line 2). For all numeric and boolean type variables, no quotes are utilized (line 1).
age=47;
middleInit='G';
language="Java";
The first time we assign a value to a variable it is called initializing the variable (initialization). This can happen anytime after the declaration (lines 1 and 2) OR at the same time as the declaration (line 3).
boolean isSoph;
isSoph=false;;
double gpa=3.92;
Types of Variables
The two main types of variables are primitive and reference. When a variable is declared a space for it is created in the computer's memory and labeled with the variable name. Primitive type variables are native to Java and have a pre-defined size. The value of a primitive variable is stored right in the allotted space. Reference type variables are user created and thus, do not have a pre-defined size that the computer is aware of. So, a reference variable stores a 'reference' to another memory location where the information is stored.
Primitive type variables are native to Java. Types include, but are not limited to:
byte, short, int and long are used to store integer values of different ranges.
char type variables store single character entities, such as 'G' or '&'.
float and double type variables store decimal numbers of different ranges.
boolean type variables store a value of true or false.
Note: the variable types in bold are the ones that we will focus on in this course. The other types are valid Java types and can be used to better manage the storage footprint of your program.
Primitive type variables store the value in the variables memory location. The most commonly used variable types in learning programming are int, double, char and boolean. Below, you will see examples of variable initializations for each of these types:
int zip=19462;
double weight=1.5;
char initial='M';
boolean isMale=true;
Reference type variables are object instances of classes. When declared, they need to be instantiated using the keyword new and the class constructor.
A new term in Java is intantiation. Instantiating a variable means to call a constructor method (used for reference type variables).
origin=new Point(0,0);
Scanner reader=new Scanner(System.in);
//combining the declaration and instantiation
The middle ground between these two types of variables is the String type. In reality, the String class is a reference type variable, but the creators of Java realized its importance and allow it to often work more like a primitive. For example, a String type variable can be declared and initialized without a constructor call.
String name=new String("Jim");
String name="Jim"
Assignment Operators
As discussed earlier, when assigning a variable, we use the = assignment operator. The variable should be on the left side with the value on the right.
age=17;
There are other assignment operators such as +=, -=, /=, *=, and %=. These types of operations are typically used as a shorthand for including the variable in a calulcation. For example:
int total=10;
total+=5;
System.out.println(total);
//the output is 15
In the example above, writing total+=5 is the same as writing total=total+5. In other words, take the current value of the variable, add 5 to it and store the result back in the original variable.
Constant Variables
Some variables, we want to make sure that their values do not change through the course of the program. These are called constants and are typically denoted in all caps and use the keyword final in their declaration. An example is:
final double SALES_TAX = 06;
Using this notation serves several purposes:
The all capital variable name serves quick notice to programmers that it is a constant.
The ability to protect a variable against accidental assignment or alteration within the program
Allows one time alterations should the programmer need to change the value (e.g. a city/state with a different value for sales tax)