CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Data

Data
Basic skills
Assignment statements and arithmetic expressions

Assignment operators

Operator NameAssigns to the variable
= Assignment A new value.
+= Addition The result of adding the operand to the starting value of the variable.
-= Subtraction The result of subtracting the operand from the starting value of the variable.
*= Multiplication The result of multiplying the operand by the starting value of the variable.
/= Division The result of dividing the operand by the starting value of the variable.
%= Modulus The value that is left over after dividing the right operand by the value in the variable.

Statements that use the same variable on both sides of the equals sign

count = count + 1; // count is increased by 1
count = count - 1; // count is decreased by 1
total = total + 100.0; // total is increased by 100.0
total = total - 100.0; // total is decreased by 100
price = price * .8; // price is multiplied by 8
sum = sum + nextNumber; // sum is increased by value of nextNumber

Statements that use the shortcut operators to get the same results

count += 1; // count is increased by 1
count -= 1; // count is decreased by 1
total += 100.0; // total is increased by 100.0
total -= 100.0; // total is decreased by 100.0
price *= .8; // price is multipled by 8
sum += nextNumber; // sum is increased by the value of nextNumber

More examples of simple assignment statements

// character arithmetic char letter1 = 'C'; // letter1 = 'C' Unicode integer is 67 char letter2 = ++letter1; // letter2 = 'D' Unicode integer is 68

Arithmetic operators

Operator Name Description
+ Addition Adds two operands.
- Subtraction Subtracts the right operand from the left.
* Multiplication Multiplies the right and left operands.
/ Division Divides the right operand into the left. If both are integers, the result is an integer.
% Modulus Returns the remainder after a division.
++ Increment Adds 1 to the operand (x = x + 1).
-- Decrement Subtracts 1 from the operand (x = x - 1).
+ Positive sign Promotes byte, short, and char types to the int type.
- Negative sign Changes a positive value to negative, and vice versa.

Examples of simple assignment statements

int x = 14; int y = 8; int result1 = x + y; // result1 = 22
int result2 = x - y; // result2 = 6
int result3 = x * y; // result3 = 112
int result4 = x / y; // result4 = 1
int result5 = x % y; // result5 = 6
int result6 = -y + x; // result6 = 6
int result7 = --y; // result7 = 7
int result8 = ++x; // result8 = 15, x = 15
double a = 8.5;
double b = 3.4;
double result9 = a + b; // result9 = 11.9
double result10 = a - b; // result10 = 5.1
double result11 = a * b; // result11 = 28.90
double result12 = a / b; // result12 = 2.5
double result13 = a % b; // result13 = 1.7
double result14 = -a + b; // result14 = -5.1
double result15 = --a; // result15 = 7.5
double result16 = ++b; // result16 = 4.4

How to code arithmetic expressions and assignment statements

  • An arithmetic expression consists of operands and arithmetic operators.
  • Binary operators operate on two operands. Unary operators operate on just one operand.
  • An assignment statement consists of a variable, an equals sign, and an expression.
  • When an assignment statement is executed, the value of the expression is determined and the result is stored in the variable.
Previous | Primitive data types | Variables | Constants | Assignment statements and arithmetic expressions | Order of preference | Casting | Next