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 Variables of Primitive Data Types – Declarations

Initializing Local Variables of Primitive Data Types Local variables are variables that are declared in methods, constructors, and blocks. They are not initialized implicitly when they are allocated memory at method invocation—that is, when the execution of a method begins. The same applies to local variables in constructors and blocks. Local variables must be explicitly … 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

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

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

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

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

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