Assignment Operators

Assignment Operators

The java assignment operator statement has the following syntax:

<variable> = <expression>

If the value already exists in the variable it is overwritten by the assignment operator (=).

Ex:
public class AssignmentOperatorsDemo {
public AssignmentOperatorsDemo( ) {

// Assigning Primitive Values

 int j, k;

 j = 10; // j gets the value 10.

 j = 5; // j gets the value 5. Previous value is overwritten.

 k = j; // k gets the value 5.

 System.out.println("j is : "+j);

 System.out.println("k is : "+k);

// Assigning References

 Integer i1 = new Integer("1");

 Integer i2 = new Integer("2");

 System.out.println("i1 is : "+i1);

 System.out.println("i2 is : "+i2);

 i1 = i2;

 System.out.println("i1 is : "+i1);

 System.out.println("i2 is : "+i2);

// Multiple Assignments
 k = j = 10; // (k = (j = 10))

 System.out.println("j is : "+j);

 System.out.println("k is : "+k);
 }
 public static void main(String args[]){
 new AssignmentOperatorsDemo();
 }
}

Assignment Operators

x operation= y
is equivalent to
x = x operation y

x and y must be numeric or char types except for "=", which allows x and y also to be object references.
In this case, x must be of the same type of class or interface as y. If mixed floating-point and integer
types, the rules for mixed types in expressions apply.

=
Assignment operator.
 x = y;
y is evaluated and x set to this value.
The value of x is then returned.


Share this

Related Posts

Previous
Next Post »

Recent