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.

0 comments:

Post a Comment