Assigning References – Basic Elements, Primitive Data Types, and Operators

Assigning References Copying reference values by assignment creates aliases. Below, the variable pizza1 is a reference to a pizza that is hot and spicy, and pizza2 is a reference to a pizza that is sweet and sour. Click here to view code image Pizza pizza1 = new Pizza(“Hot&Spicy”);Pizza pizza2 = new Pizza(“Sweet&Sour”);pizza2 = pizza1; Assigning … Read more

Method Overloading – Declarations

3.6 Method Overloading Each method has a signature, which comprises the name of the method plus the types and order of the parameters in the formal parameter list. Several method implementations may have the same name, as long as the method signatures differ. This practice is called method overloading. Because overloaded methods have the same … Read more

Type Conversions in an Assignment Context – Basic Elements, Primitive Data Types, and Operators

Type Conversions in an Assignment Context If the target and the source have the same type in an assignment, then obviously the source and the target are assignment compatible and the source value need not be converted. Otherwise, if a widening primitive conversion is permissible, then the widening conversion is applied implicitly; that is, the … 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

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

Examples of Bitwise Operator Application – Basic Elements, Primitive Data Types, and Operators

Examples of Bitwise Operator Application Click here to view code image char v1 = ‘)’;          // Unicode value 41byte v2 = 13;int result1 = ~v1;      // -42int result2 = v1 & v2;  // 9int result3 = v1 | v2;  // 45int result4 = v1 ^ v2;  // 36 Table 2.32 shows how the result is … Read more

The Conditional Operator ?: – Basic Elements, Primitive Data Types, and Operators

2.18 The Conditional Operator ?: The ternary conditional operator ?: allows conditional expressions to be defined. The conditional expression has the following syntax: condition ?expression1 :expression2 It is called ternary because it has three operands. If the boolean expression condition is true, then expression1 is evaluated; otherwise, expression2 is evaluated. Both expression1 and expression2 must evaluate to … Read more

Short-Circuit Evaluation – Basic Elements, Primitive Data Types, and Operators

Short-Circuit Evaluation In the evaluation of boolean expressions involving conditional AND and OR, the left-hand operand is evaluated before the right-hand operand, and the evaluation is short-circuited (i.e., if the result of the boolean expression can be determined from the left-hand operand, the right-hand operand is not evaluated). In other words, the right-hand operand is … Read more