Introduction
A variable will be assigned a value by using the assignment operator =. The variable on the left-hand side of the operator receives the value. On the right-hand side of the operator there will be either a constant (literal), another variable or an expression.
Assign a Literal to a Variable
A constant can be assigned to a variable by means of the following statement:
variable = literal;
The data type of the literal on the right-hand side of the statement must be equal to the data type of the variable on the left-hand side. If not, Java will throw an error. Note that a decimal constant (without the additional f or d) (e.g. 99.50) is always of type double. In our next example the variable price must be declared double.
Examples: yourAge = 17; price = 99.50; //price must be declared double sorted = true; letter = 'Y';
Assign a Variable to another Variable
A variable (variable1) gets the value from another variable (variable2) by means of the following statement:
variable1 = variable2;
The data type of the variabele (variable2) on the right-hand side must be the same as the data type of the variable (variable1) on the left-hand side.
Examples: yourAge = 17; myAge = yourAge; //myAge (17) gets the value from yourAge (17)
Assign an Expression to a Variable
An expression is a sentence made up of variables, literals and operators that results in one single value.
A variable gets the value from an expression by means of the following statement:
variable = expression;
To avoid error messages, the data type of the resulting value of the expression on the right-hand side must be the same as the data type of the variable on the left-hand side of the statement.
Arithmetic Expression
An arithmetic expression is a combination of variables, literals and arithmetic operators. The arithmetic operators can be found in the next table:
Arithmetic Operator | Description |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo (remainder) |
++ | Increment (increases the value of a variable by 1) (also called a unary operator) |
-- | Decrement (decreases the value of a variable by 1) (also called a unary operator) |
Precedence Rules
Complex expressions (e.g. 5 * 2 / 10 – 5 ) will use several arithmetic operators in one single expression. The order of evaluation is defined by the Java precedence rules. The priorities are defined in the next table. When there are arithmetic operators with the same priority in one expression they are evaluated from left to right.
Priority | Arithmetic Operators |
---|---|
1 | () |
2 | ++ , -- |
3 | *, /, % |
4 | +, – |
Let’s have a look to some examples calculated by the program Expressions.java.
//Expressions public class Expressions { public static void main(String[] args){ double result1, result2, result3; result1 = 5 * 2 / 10 - 5; System.out.println("5*2/10-5 = "+result1); result2 = 5 * 2 / (10 - 5); System.out.println("5*2/(10-5) = "+result2); result3 = 6 / 4 * (10 - 5); System.out.println("6/4*(10-5) = "+result3); } }
Results: 5*2/10-5 = -4.0 5*2/(10-5) = 2.0 6/4*(10-5) = 5.0
Try to explain the results yourself:

By applying the precedence rules result1 and result2 can easily be derived. However, the value of result3is more difficult to explain.
The reasoning is as follows: the division of two integers results in a whole integer value (all decimals are dropped). However, a mixed division (real, double on one side and byte, short, int, long on the other side) will result in a decimal value (type double).
The division of two integers will result in an integer value (all decimals are dropped).
Type Casting
To avoid an automatic conversion, programmers can make use of type casting. Type casting will treat the value of a constant, variable or expression as of the data type specified.
Type casting can be done by placing the data type between parenthesis in front of the constant, variable or expression.
Examples: (double) 6 / (double) 4 = 1.5 (division of two double values) (double (6 / 4) = 1.0 (6 / 4 = 1; (double) 1 = 1.0)
Powerful Assignments
An assignment statement assigns the value of the right hand side of the statement to a variable on the left hand side. This structure is very powerful and provides the ability to build ‘peculiar’ assignments like:
Example: price = price + salesTax;
At the first sight, this instruction does not make sense: the left hand side of the instruction can never be equal to the right hand side. This reasoning is completely wrong: the right hand side of the instruction is assigned to the variable in the left-hand. The = operator in Java does not mean equal to, but has the meaning of assign to.
The following animation shows how the statement price = price + salesTax;
is executed:
