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

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

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

Instance Methods and the Object Reference this – Declarations

3.5 Instance Methods and the Object Reference this Instance methods belong to every object of the class and can be invoked only on objects. All members defined in the class, both static and non-static, are accessible in the context of an instance method. The reason is that all instance methods are passed an implicit reference … 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

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