Java Reference
In-Depth Information
A binary operator uses infix notation. The operator appears in between the two operands.
operand1 operator operand2 // Infix notation
For example,
10 + 15; // 10 is operand1, + is a binary operator, and 15 is operand2
Like a binary operator, a ternary operator uses infix notation.
operand1 operator1 operand2 operator2 operand3 // Infix notation
Here, operator1 and operator2 make a ternary operator.
For example,
/* isSunday is the first operand, ? is the first part of ternary operator, holiday is the second
operand, : is the second part of ternary operator, noHoliday is the third operand
*/
isSunday ? holiday : noHoliday;
An operator is called an arithmetic operator, a relational operator, a logical operator, or a bitwise operator,
depending on the kind of operation it performs on its operands. Java has a big list of operators. This chapter discusses
most of the Java operators. Some of the operators will be discussed in later chapters.
Assignment Operator (=)
An assignment operator (=) is used to assign a value to a variable. It is a binary operator. It takes two operands. The value
of the right-hand operand is assigned to the left-hand operand. The left-hand operand must be a variable. For example,
int num;
num = 25;
Here, num = 25 uses the assignment operator =. In this example, 25 is the right-hand operand. num is the left-hand
operand, which is a variable of type int .
Java ensures that the value of the right-hand operand of the assignment operator is assignment compatible to the
data type of the left-hand operand. Otherwise, a compile-time error occurs. In case of reference variables, you may
be able to compile the source code and get a runtime error if the object represented by the right-hand operand is not
assignment compatible to the reference variable as the left-hand operand. For example, the value of type byte , short ,
and char are assignment compatible to int data type, and hence the following snippet of code is valid:
byte b = 5;
char c = 'a';
short s = -200;
int i = 10;
i = b; // Ok. byte b is assignment compatible to int i
i = c; // Ok. char c is assignment compatible to int i
i = s; // Ok. short s is assignment compatible to int i
 
Search WWH ::




Custom Search