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

An in-depth explanation of Java data types, focusing on primitive types and references. It covers the concept of pointers, or references, and how they differ from primitive types. The document also explains dereferencing, the importance of assigning a pointee before dereferencing, and the differences between shallow and deep copying. It includes examples of Employee objects and their corresponding reference variables.

Typology: Study Guides, Projects, Research

2021/2022

Uploaded on 09/27/2022

andreasphd
andreasphd 🇬🇧

4.7

(28)

287 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
References (pointers)
in Java
pf3
pf4
pf5
pf8
pf9
pfa

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