The document discusses pointers and references in C and Java. It explains that pointers in C can perform more operations than references in Java, such as pointer arithmetic, but references are safer. It provides examples of using references to link objects in data structures like binary trees.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views12 pages
15 Pointers and References
The document discusses pointers and references in C and Java. It explains that pointers in C can perform more operations than references in Java, such as pointer arithmetic, but references are safer. It provides examples of using references to link objects in data structures like binary trees.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12
Pointers and References
Apr 21, 2024
Machine addresses : 3FA71CF2 Computer memory consists 3FA71CF3 of one long list of addressable 3FA71CF4 bytes 3FA71CF5 A pointer is a data item that 3FA71CF6 contains an address 3FA71CF7 3FA71CF6 3FA71CF8 3FA71CF9 • A reference is a data item that 3FA71CFA 3FA71CFB contains an address 3FA71CFC 3FA71CF6 3FA71CFD • C has pointers, but Java has 3FA71CFE references 3FA71CFF 3FA71D00 • So what’s the difference? 3FA71D01 : C (and C++) vs. Java In C you can dereference (follow) a pointer In Java you can dereference (follow) a reference
In C you can assign one pointer variable to another
In Java you can assign one reference variable to another
In C you can determine whether one pointer is larger than, equal to, or smaller than another pointer In Java you can determine whether one reference is equal to another reference
In C you can create a pointer to anything
In Java you can have references to objects
In C you can do integer arithmetic on pointers
References Recall that an Abstract Data Type (ADT) has a set of values and a set of operations on those values Pointers and references have the same set of values (memory addresses) Pointers have more defined operations than references Pointers are more flexible and more general than references References are safer than pointers (from error or malicious misuse) References allow automatic garbage collection (pointers don’t) A (non-abstract) Data Type also has an implementation The implementations of pointers and references are similar Java references carry information about the thing referenced; in C, it’s up to the compiler to figure out what it can Data structures Basically, pointers and references are the same thing; they point to (refer to) something else in memory A Data Structure is a description of how data is organized in memory Many (not all) data structures are built from objects pointing/referring to one another Understanding pointers (references) is fundamental to this course If this course were taught in C or C++ instead of Java, all the “nuts and bolts” would be the same This course is in Java, but it’s not about Java You need to know how to create your own data structures I will also teach some Java-specific packages In real life, it’s stupid to redo work that’s already been done for you A trivial example We use a lot of references in Java: "John" class Person { String name; Person spouse; Person (String n) { john name = n; name } spouse } … "Mary" Person john = new Person("John"); Person mary = new Person("Mary"); john.spouse = mary; mary.spouse = john; mary name spouse A more serious example + A binary tree is a data structure in which every node (object) has zero, one, or two children (references to other / nodes) 7 Arithmetic expressions can null null be represented as binary trees 12 To evaluate an arithmetic null 4 expression: null null null If it is a leaf, return its value Otherwise, evaluate its two A binary tree representing the subtrees, and perform the arithmetic expression 12/4+7 indicated operation Binary trees in Java public class BinaryTree { public Object value; // the information in this node private BinaryTree leftChild; private BinaryTree rightChild;
// Constructors and methods...
} To make binary trees as useful as possible, we make the value in a node an Object As implementers of the BinaryTree class, our job is to make sure that the structure of the binary tree is always valid We don’t really care what the user puts in the value field Size of objects Objects in Java have, generally speaking, a fixed size The size of all primitives is known Objects don’t actually “contain” other objects—they just have references to other objects, and a reference is 4 bytes So what about Vectors? A Vector is like an array, but it gets bigger as you put things into it A Vector actually has two “sizes”—its capacity, which is how many references it can hold, and its size, which is how many references are in it right now When you exceed the capacity of a Vector, Java creates a new Vector for you The magic of Vectors Suppose you have two v1 references to a Vector, and v2 you use one of them to add elements to the Vector What happens if Java decides to replace this Vector with a bigger one? It looks like the second reference is a “dangling pointer,” referring to nothing This doesn’t happen! Java protects you from this error But how? Vector’s secret trick A reference to a Vector is v1 actually a reference to a reference to a Vector v2
In this way, the “real” reference has to be changed in only one
place, and all the other, indirect references automatically work It’s clever, but it isn’t magic The End
DATABASE From the conceptual model to the final application in Access, Visual Basic, Pascal, Html and Php: Inside, examples of applications created with Access, Visual Studio, Lazarus and Wamp