All About Operators

Statements can use mathematical expressions by employing the operators +, -, *, /, and %. You use these operators to crunch numbers throughout your Java programs.

An addition expression in Java uses the + operator, as in these statements:

double weight = 205;weight = weight + 10;

The second statement uses the + operator to set the weight variable equal to its current value plus 10. A subtraction expression uses the - operator:

weight = weight - 15;

This expression sets the weight variable equal to its current value minus 15.

A division expression uses the / sign:

weight = weight / 3;

This sets the weight variable to its current value divided by 3.

To find a remainder from a division expression, use the % operator (also called ...

Get Sams Teach Yourself Java™ in 24 Hours, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.