Skip to main content

Section 5.1 Pointers Syntax

Subsection 5.1.1 Syntax

The fundamental concepts of references, pointees, dereferencing, and assigning discussed earlier are all that you require to construct reference code. However, when discussing reference code, we need to employ a familiar syntax, which may not be particularly engaging. In this case, we will utilize the Java language syntax, which offers the advantage of influencing the syntaxes of numerous other programming languages.

Subsection 5.1.2 Declaring a Reference Variable

To establish a reference to a non-primitive data type (i.e., an object), one must declare a variable of that specific object’s type. Similar to other variables, reference variables are declared in the same manner. The declaration includes specifying the type and identifier of the new variable, and memory is allocated to store its value. However, it is crucial to note that declaring a reference variable does not automatically assign a pointee (object) to the reference. Initially, the reference holds a "bad" value until it is assigned to an actual object.

Subsection 5.1.3 Reference Rules

The rules governing reference structures remain concise, regardless of their complexity. A reference variable serves as a container for storing a reference to its associated pointee. The pointee itself holds valuable information or data. To access the content of a reference, the dereference operation is used. However, it is crucial to note that a reference can only be dereferenced once it has been assigned to a valid pointee. Many issues or errors related to references occur when this rule is violated. Allocating a reference does not automatically assign it to refer to a specific pointee. The act of assigning the reference to a particular pointee is a distinct operation that can sometimes be overlooked. When two references are assigned to each other, they start referring to the same pointee. This allows for object sharing, enabling multiple references to access and manipulate the same underlying object.

Subsection 5.1.4 Java Reference Vs Pointers

Java references possess two key features that distinguish them from the less permissive pointers found in languages like C or C++.
  1. Reduced bugs: Java’s implementation of reference manipulation is accurate and automatic, resulting in fewer common reference-related bugs. Additionally, the Java runtime system verifies each reference value whenever it is used. As a result, if a null reference is dereferenced, the system promptly detects it on the line where the issue occurs. This stands in contrast to languages like C++ where dereferencing a null value might not cause a program crash until later stages. This ability to pinpoint problems immediately can greatly enhance a programmer’s productivity by identifying the exact location of an issue.
  2. Slower execution: Due to the extensive runtime implementation of reference management and the additional runtime checking, Java code tends to run slower compared to languages like C and C++. However, the advantages of improved programmer efficiency and reduced bugs make the trade-off of slower execution worthwhile for many applications.
You have attempted of activities on this page.