Docsity

Understanding Java References and Data Types: Primitive Types, References, Copying, Study Guides, Projects, Research of Java Programming

Partial preview of the text

Download Understanding Java References and Data Types: Primitive Types, References, Copying and more Study Guides, Projects, Research Java Programming in PDF only on Docsity!

References (pointers)

in Java

Data Types in Java

● Primitive Data Types

○ byte, short, int, long, float, double, boolean, char

○ Primitive types variable works like a box that can store a single value

○ Example: int num = 42;

Data Types in Java

● References

○ Example:

Employee empRef = new Employee(“john”, 1000);

public class Employee { private String name; private int salary; public Employee(String name, int salary) { this .name = name; this .salary = salary; } … }

Dereferencing

● Dereferencing:

○ Accessing the value of the pointee for some reference variable

○ Is done with the "dot" (.) operator to access a field or method of an

object

Referencing

● A reference must be assigned a pointee before dereference operations

will work.

● Not assigning a pointee to a reference will cause a

NullPointerException

● null : special reference value that encodes the idea of "points to

nothing”.

○ Initial value of references

Reference Assignments

● An assignment (using equals) of one reference to another makes them point to the same pointee

○ Example:

Employee empRef = new Employee(“john”, 1000);
Employee second = empRef;
After the assignment, testing for second == empRef would return true

Shallow and Deep Copying

● Shallow copy (of a reference) is achieved through sharing

● Deep copy creates a new copy of the pointee

Shallow and Deep Copying Example

alias
deep
copy
// John
// Patrice
update alias
update deep copy