The Assignment Operator

You have been using the assignment operator since Chapter 1. Now it is time to take a formal look at it. The assignment operator is the single equal sign, =. This 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:

Image

This fragment sets the variables x, y, and z to 100 using a single statement. This works because the ...

Get Java, A Beginner's Guide, 5th Edition, 5th 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.