CSE 115 Tutorial 3 Variables

Designer: Yingrui Liu

  Assignment
Home
Declaration
Assignment
Scope & lifetime
Relationship & Methods

First, we should be familiar with some terms used for assignment.

Expression: There are lots of different kind of expressions. The most common one is the arithmetic expression such as 3+2 and it can be evaluated to produce the value 5. We also use expressions to create a new object by instantiating a class."new Elephant()" is an expression.
Value of the expression: It is the reference to an object.
Notice: A variable can also be an expression and its value is the reference assigned to it.

After we declared a variable, we usually assign a value to the variable which means the variable got a reference to an expression. It can be illustrated using a picture:

We can see from the picture that the order of the assignment is from the right side of equation to the left side of the equation.

back to top

Examples of assignment in java code
(The basic assignment of variables is the same for different variables.)

identifier = expression
e.g. elephant= new Elephant();
We can also combined the declaration and the assignment together as
type identifier = expression;
e.g. Elephant elephant= new Elephant();

back to top

Example of exchanging values between variables

 

 

This is an example to exchange the value of the variable a and variable b. The picture shows what is going on inside the machine when java executes the codes.


back to top