An operator in Java is a special symbol that performs an action on one or more values. The values it works with are called operands. So in the expression a + b, the plus sign is the operator and a and b are the operands.
Here is a simple way to remember it: variables and literals are the nouns of your program, while operators are the verbs. Writing age > 18 is Java asking a yes-or-no question, and writing total = total + 1 is Java following an instruction. Remove every operator from a program and you are left with data that can never change, never get compared, and never drive a decision.
- Operands are the values being processed, such as 10, 3.5, true, or a variable like score.
- Operators are the symbols that combine or transform those values, for example +, ==, && and ++.
- An expression is any valid mix of operands and operators, such as (price * quantity) - discount.
- The result of an expression can be printed, stored in a variable, or reused inside a bigger expression.
ANATOMY OF A JAVA EXPRESSION
┌──────────────────────────────────────────────────────────────┐
│ int total = 10 + 5 * 2; │
│ │ │ │ │ │ │
│ │ │ │ └─┴──► Operands (the numbers 5 and 2) │
│ │ │ └──────► Operator (multiplication) │
│ │ └─────────► Operator (addition) │
│ └──────────────────► Variable that stores the result │
└──────────────────────────────────────────────────────────────┘One detail that surprises beginners is that the same symbol can mean different things depending on what sits next to it. The + sign adds two numbers, but place a String beside it and the very same symbol joins text together. Java allows this only for string concatenation; for your own classes, operators always keep their original meaning.