Assignment Operators


x

Lesson:
Main Points:
  1. Assignment operators are used to set the value of a variable
  2. Assignment operators are used to change the value of a variable
  3. A single equal sign (=) is the assignment operator
  4. Compound operators take the form of op= (eg, *=, +=, -=, etc.)
  5. Assignments may be chained for variables of the same type (eg, x = y = z = 8)

Simple Assignment:
The most basic assignment takes the form of variable equals value as in:
  int x = 9;
  String s = "Hello";
  char c = 'c';
  float f = 1.09;

Since variables can contain values assignments can also be done like this:
  int y = x;
  String t = s;
  char d = c;
  float g = f;

Compound Assignments:
Calculation and assignment can be performed in a single statement. The most obvious way to do this is as follows:
  int x;
  x = 5;
  x = x + 10;
  String s = "Hello ";
  s = s + " Sam";

The shorthand way to write the third and fifth lines is:
  x += 10;
  s += " Sam";

This can be done with the other operations as well:
  x *= 10;
  x /= 2;
  x -= 5;

An appreviated way of incrementing by one is:
  x++

Chained Assignments:
Given three integers, they can all be assigned a single value in a single statement:
  x = y = z = 11;


x

Assignment:
There's nothing very tricky about assignment operators. So, you will just write a simple applet which assigns values to four variables (an int, a String, a char, and a float) and alters the values in the variables. Do a before and after kind of display with these variables. Display the full source code for your applet in a textarea on the web page on which your applet is displayed.
x