Arithmetic Compound Assignment Operators: *=, /=, %=, +=, -= – Basic Elements, Primitive Data Types, and Operators

Arithmetic Compound Assignment Operators: *=, /=, %=, +=, -= A compound assignment operator has the following syntax: variable op= expression and the following semantics: variable = (type) ((variable) op (expression)) The type of the variable is type and the variable is evaluated only once. Note the cast and the parentheses implied in the semantics. Here … Read more

Range of Numeric Values – Basic Elements, Primitive Data Types, and Operators

Range of Numeric Values As we have seen, all numeric types have a range of valid values (p. 41). This range is given by the constants named MAX_VALUE and MIN_VALUE, which are defined in each numeric wrapper type. The arithmetic operators are overloaded, meaning that the operation of an operator varies depending on the type … Read more

Declaring and Initializing Variables – Declarations

Declaring and Initializing Variables Variable declarations (technically called declaration statements) are used to declare variables, meaning they are used to specify the type and the name of variables. This implicitly determines their memory allocation and the values that can be stored in them. Examples of declaring variables that can store primitive values follow: Click here … Read more

Arithmetic Operators: *, /, %, +, – – Basic Elements, Primitive Data Types, and Operators

2.8 Arithmetic Operators: *, /, %, +, – Arithmetic operators are used to construct mathematical expressions as in algebra. Their operands are of a numeric type (which includes the char type). Floating-point operations are now consistently strict; that is, they are executed in accordance with the IEEE-754 32-bit (float) and 64-bit (double) standard formats. This … Read more

Variable Increment and Decrement Operators: ++, — – Basic Elements, Primitive Data Types, and Operators

2.10 Variable Increment and Decrement Operators: ++, — Variable increment (++) and decrement (–) operators come in two flavors: prefix and postfix. These unary operators have the side effect of changing the value of the arithmetic operand, which must evaluate to a variable. Depending on the operator used, the variable is either incremented by 1 … Read more

Unary Arithmetic Operators: -, + – Basic Elements, Primitive Data Types, and Operators

Unary Arithmetic Operators: -, + The unary operators have the highest precedence of all the arithmetic operators. The unary operator – negates the numeric value of its operand. The following example illustrates the right associativity of the unary operators: Click here to view code image int value = – -10; // (-(-10)) is 10 Notice … Read more

Boolean Expressions – Basic Elements, Primitive Data Types, and Operators

2.11 Boolean Expressions As the name implies, a boolean expression has the boolean data type and can only evaluate to the value true or false. Boolean expressions, when used as conditionals in control statements, allow the program flow to be controlled during execution. Boolean expressions can be formed using relational operators (p. 74), equality operators … Read more

Object Reference Equality: ==, != – Basic Elements, Primitive Data Types, and Operators

Object Reference Equality: ==, != The equality operator == and the inequality operator != can be applied to reference variables to test whether they refer to the same object. Given that r and s are reference variables, the reference equality operators are defined as shown in Table 2.25. Table 2.25 Reference Equality Operators r == … Read more

Additive Binary Operators: +, – – Basic Elements, Primitive Data Types, and Operators

Additive Binary Operators: +, – The addition operator + and the subtraction operator – behave as their names imply: They add and subtract values, respectively. The binary operator + also acts as string concatenation if any of its operands is a string (p. 67). Additive operators have lower precedence than all the other arithmetic operators. … Read more

Object Value Equality – Basic Elements, Primitive Data Types, and Operators

Object Value Equality The Object class provides the method public boolean equals(Object obj), which can be overridden (ยง5.1, p. 196) to give the right semantics of object value equality. The default implementation of this method in the Object class returns true only if the object is compared with itself, as if the equality operator == … Read more