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

Initializing Local Reference Variables – Declarations

Initializing Local Reference Variables Local reference variables are bound by the same initialization rules as local variables of primitive data types. Example 3.3 Flagging Uninitialized Local Reference Variables Click here to view code image public class VerySmartClass {  public static void main(String[] args) {    String importantMessage;       // Local reference variable     System.out.println(“The message length is: … 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

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

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

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 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

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

Class Declarations – Declarations

3.1 Class Declarations A class declaration introduces a new reference type and has the following syntax: Click here to view code image class_modifiers classclass_name extends_clause implements_clause // Class header{ // Class bodyfield_declarationsmethod_declarationsconstructor_declarationsmember_type_declarations} In the class header, the name of the class is preceded by the keyword class. In addition, the class header can specify the following information: … Read more