Java Pebbles Has a New Blog Location

Please visit Jessica's Journal for it's new blog location. Thanks for the visits.

Declaring Object References, Instantiating Objects and Initializing Object References

Object reference variables are variables containing an address to an Object in memory.

To declare, instantiate and initialise an Object Reference Variable:
  • Declare - Specifies its identifier and the Object Type of the referenced class
  • Instantiate - Using the new keyword
  • Initialise - Assigns the Object to the Object Reference Variable
Declaring Object Reference Variables:
Syntax:
Classname identifier;
where
Classname is the Class or the type of Object referred to with the Object Reference
identifier is the variable name of type Classname

Instantiating an Object:
Syntax:
new Classname();

where
new keyword is used to create an Object Instance from a Class
Classname is the Class or Type of Object being created

Initializing Object Reference Variables:
Syntax:
identifier = new Classname();

Using an Object Reference Variable to Manipulate Data:
The dot (.) operator is used with the Object Reference to manipulate the values or to activate the Object methods

Storing Object Reference Variables in Memory:
Object Reference Variables hold the memory address of Objects in memory. Memory addresses are usually written in hexadecimal notation such as 0x332006. Local Object Reference Variables and their values are stored in Stack Memory while the Objects that they reference to are stored in Heap Memory.

Assigning a Reference From One Variable to Another:
Upon execution of the above codes, both myShoe and yourShoe reference variables contain the same address to the same object. Unless another reference variable was pointing myShoe object, it would be garbage collected.

Using Promotion and Type Casting

Promotion is used when you try to assign a data type that holds a smaller value range to data type that holds a bigger value range. This is to prevent precision lost in the assigned data type value. For example, promoting a byte data type to integer data type.

Typecast is used to lower the value range by changing its data type such as double floating point to floating point. This is normally done when developer need to access methods that only accept certain data types as arguments or to save memory.

Promotion In Use:

Data Type Promotion may occur automatically by Compiler if the data would not be lost. For examples:
  • Assign a smaller data type to a larger data type
  • Assign an integral data type to a floating point data type (no decimal value places to lose)
Type Casting:

Syntax:
identifier = (target_type) value
where
identifier is the Variable Namevalue is the value assign to the Identifier
value is the value assigned to the Identifier
(target_type) is the type that you wish to type cast the value

For example:
int number1 = 20; //32 bits of memory used
int number2 = 10; // 32 bits of memory used
byte number3 = 0; // 8 bits of memory lost
number3 = (byte) (number1 + number2); // no data loss

Compiler Assumptions for Integral and Floating Point Data Types:
  • Values are automatically converted to an int value (or higher) if the primitive data type values are used in an expression with certain operators (*, /, -, +, %).
  • Values assigned to a floating point data types are always defaulted to a double data type.

Using Arithmetic Operators

Standard Mathematical Operators:
  • Addition ( + )
  • Subtraction ( - )
  • Multiplication ( * )
  • Division ( / )
  • Remainder ( % )

Increment ( ++ ) and Decrement ( -- ) Operators:

  • Pre-Increment and Pre-Decrement - the operation is applied before any subsequent calculations or assignments are performed.
  • Post-Increment and Post-Decrement - the operation is applied after any subsequent calculations or assignments are performed.

Examples:

Pre-Increment:
int x = 6;
int y = ++x;
x is 7, y is 7

Post-Increment:
int x = 6;
int y = x++;
x is 7, y is 6


Pre-Decrement:
int x = 6;
int y = --x;
x is 5, y is 5


Post-Decrement:
int x = 6;
int y = x--;
x is 5, y is 6


Operator Precedence Order:

  • Parentheses
  • Increment and Decrement Operators
  • Multiplication and Division Operators (Evaluated from Left to Right)
  • Addition and Subtraction Operators (Evaluated from Left to Right)

Declaring Variables and Assigning Values to Variables

Naming a Variable:
  • Must have a unique and descriptive Identifier

Assigning a Value to a Variable:

  • Variables are automatically initialised according to their data types (if value assignments are not available during declaration) such as integral types are set to 0; floating point types are set to 0.0; char type is set to null character ('\u0000); and boolean type is set to false.
  • Assign value when the variable is declared.
  • Assign the value of one variable to another variable.
  • Assign the result of an expression to integral, floating point ot boolean type variables.
  • Assign the return value of a method call to a variable.

More than 1 Variables can be declared on the same line of code, but only if they belong to the same data type.
Syntax:
type identifier = value [, identifier = value];

The Use of Constants:

  • Represents values that cannot be changed.
  • Example: final double UNIT_TAX = 3.85; where the final keyword indicates that the value cannot be changed once it is set.
  • Program needs to be re-compiled if the constant value is changed in the program

Storing Primitives Constants in Memory:

  • Heap Memory is dynamically allocated memory which contains information used to hold objects (Attribute Variables and Methods).
  • Stack Memory is used to store items for a brief period which is shorted than the life of an object such as Local Variables declaration.