Chapter: Java The Complete Reference - The Java Language - Operators

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

The Assignment Operator - Java

The assignment operator is the single equal sign, =. The assignment operator works in Java much as it does in any other computer language.

The Assignment Operator

 

You have been using the assignment operator since Chapter 2. Now it is time to take a formal look at it. The assignment operator is the single equal sign, =. The assignment operator works in Java much as it does in any other computer language. It has this general form:

 

var = expression;

 

Here, the type of var must be compatible with the type of expression.

 

The assignment operator does have one interesting attribute that you may not be familiar with: it allows you to create a chain of assignments. For example, consider this fragment:

 

int x, y, z;

 

x = y = z = 100; // set x, y, and z to 100

 

This fragment sets the variables x, y, and z to 100 using a single statement. This works because the = is an operator that yields the value of the right-hand expression. Thus, the value of z = 100 is 100, which is then assigned to y, which in turn is assigned to x. Using a “chain of assignment” is an easy way to set a group of variables to a common value.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.