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

Initial Values for Variables – Declarations

Initial Values for Variables This section discusses what value, if any, is assigned to a variable when no explicit initial value is provided in the declaration. Default Values for Fields Default values for fields of primitive data types and reference types are listed in Table 3.1. The value assigned depends on the type of the … Read more

Constructors – Declarations

3.7 Constructors The main purpose of constructors is to set the initial state of an object, when the object is created by using the new operator. The following simplified syntax is the canonical declaration of a constructor: Click here to view code image access_modifier class_name (formal_parameter_list)throws_clause  // Constructor header{ // Constructor bodylocal_variable_declarationsstatements} Constructor declarations are very … Read more

Equality – Basic Elements, Primitive Data Types, and Operators

2.13 Equality We distinguish between primitive data value equality, object reference equality, and object value equality. The equality operators have lower precedence than the relational operators, but higher precedence than the assignment operators. Primitive Data Value Equality: ==, != Given that a and b represent operands of primitive data types, the primitive data value equality … Read more

The Binary String Concatenation Operator + – Basic Elements, Primitive Data Types, and Operators

2.9 The Binary String Concatenation Operator + The binary operator + is overloaded in the sense that the operation performed is determined by the type of the operands. When one of the operands is a String object, a string concatenation is performed rather than numeric addition. String concatenation results in a newly created String object … 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

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

Boolean Logical Operators: !, ^, &, | – Basic Elements, Primitive Data Types, and Operators

2.14 Boolean Logical Operators: !, ^, &, | Boolean logical operators include the unary operator ! (logical complement) and the binary operators & (logical AND), | (logical inclusive OR), and ^ (logical exclusive OR, also called logical XOR). These operators can be applied to boolean or Boolean operands, returning a boolean value. The operators &, … Read more

Bitwise Compound Assignment Operators: &=, ^=, |= – Basic Elements, Primitive Data Types, and Operators

Bitwise Compound Assignment Operators: &=, ^=, |= Bitwise compound assignment operators for the bitwise operators are defined in Table 2.33. Type conversions for these operators, when applied to integral operands, are the same as for other compound assignment operators: An implicit narrowing conversion is performed on assignment when the destination data type is either byte, … Read more