An assignment statement designates a value for a variable. An assignment statement can be used as an expression in Java.
After a variable is declared, you can assign a value to it by using an assignment statement. In Java, the equal sign is used as the assignment operator. The syntax for assignment statements is as follows:
An expression represents a computation involving values, variables, and operators that, when taking them together, evaluates to a value. For example, consider the following code:
You can use a variable in an expression. A variable can also be used on both sides of the operator. For example:
In the above assignment statement, the result of is assigned to the variable . Let’s say that is before the statement is executed, and so becomes after the statement execution.
To assign a value to a variable, you must place the variable name to the left of the assignment operator. Thus the following statement is wrong:
Note that the math equation ≠ the Java expression
In Java, an assignment statement is an expression that evaluates a value, which is assigned to the variable on the left side of the assignment operator. Whereas an assignment expression is the same, except it does not take into account the variable.
That’s why the following statements are legal:
Which is equivalent to:
And this statement
is equivalent to:
Note: The data type of a variable on the left must be compatible with the data type of a value on the right. For example, would be illegal, because the data type of is (integer) and does not accept the value without Type Casting.
inty=1; // Assign 1 to variable y
doubleradius=1.0; // Assign 1.0 to variable radius
intx=5*(3/2); // Assign the value of the expression to x
x=y+1; // Assign the addition of y and 1 to x
doublearea=radius *radius *3.14;// Compute the area
1=x; // Wrong
//If a value is assigned to multiple variables, you can use this syntax:
Java Assignment Operators
Assigning a value to a variable seems straightforward enough; you simply assign the stuff on the right side of the '= 'to the variable on the left. Below statement 1 assigning value 10 to variable x and statement 2 is creating String object called name and assigning value "Amit" to it.Statement 1: x =10; Statement 2: String name = new String ("Amit");
Assignment can be of various types. Let’s discuss each in detail.
The equal (=) sign is used for assigning a value to a variable. We can assign a primitive variable using a literal or the result of an expression.int x = 7; // literal assignment int y = x + 2; // assignment with an expression int z = x * y; // assignment with an expression with literal
Casting lets you convert primitive values from one type to another. We need to provide casting when we are trying to assign higher precision primitive to lower precision primitive for example If we try to assign int variable (which is in the range of byte variable) to byte variable then the compiler will throw an exception called "possible loss of precision". Eclipse IDE will suggest the solution as well as shown below. To avoid such problem we should use type casting which will instruct compiler for type conversion.byte v = (byte) a;
For cases where we try to assign smaller container variable to larger container variables we do not need of explicit casting. The compiler will take care of those type conversions. For example, we can assign byte variable or short variable to an int without any explicit casting.
Assigning Literal that is too large for a variable
When we try to assign a variable value which is too large (or out of range ) for a primitive variable then the compiler will throw exception “possible loss of precision” if we try to provide explicit cast then the compiler will accept it but narrowed down the value using two’s complement method. Let’s take an example of the byte which has 8-bit storage space and range -128 to 127. In below program we are trying to assign 129 literal value to byte primitive type which is out of range for byte so compiler converted it to -127 using two’s complement method. Refer link for two’s complement calculation (http://en.wikipedia.org/wiki/Two's_complement)
Java Code: Go to the editor
Reference variable assignment
We can assign newly created object to object reference variable as belowString s = new String(“Amit”); Employee e = New Employee();
First line will do following things,
- Makes a reference variable named s of type String
- Creates a new String object on the heap memory
- Assigns the newly created String object to the reference variables
You can also assign null to an object reference variable, which simply means the variable is not referring to any object. The below statement creates space for the Employee reference variable (the bit holder for a reference value) but doesn't create an actual Employee object.Employee a = null;
Compound Assignment Operators
Sometime we need to modify the same variable value and reassigned it to a same reference variable. Java allows you to combine assignment and addition operators using a shorthand operator. For example, the preceding statement can be written as:i +=8; //This is same as i = i+8;
The += is called the addition assignment operator. Other shorthand operators are shown below table
Below is the sample program explaining assignment operators:
Java Code: Go to the editor
- Assigning a value to can be straight forward or casting.
- If we assign the value which is out of range of variable type then 2’s complement is assigned.
- Java supports shortcut/compound assignment operator.
Java Code Editor: