0% found this document useful (0 votes)
4 views

WRAV201 Introduction to data structures and algorithms

The document provides an overview of the WRAV201 module, including details about the lecturer, assessments, practicals, and recommended textbook. It covers essential topics such as Java programming concepts, identifiers, variable scope, and memory allocation. Additionally, it includes links to useful Java resources and emphasizes the importance of using Java Language Level 11 for the course.

Uploaded by

Salmah Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

WRAV201 Introduction to data structures and algorithms

The document provides an overview of the WRAV201 module, including details about the lecturer, assessments, practicals, and recommended textbook. It covers essential topics such as Java programming concepts, identifiers, variable scope, and memory allocation. Additionally, it includes links to useful Java resources and emphasizes the importance of using Java Language Level 11 for the course.

Uploaded by

Salmah Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Click to edit Master title style

WRAV201
Introduction, Java, Identifiers

1
Click to edit Master title style
Lecturer & Moodle
Yo u r g u i d e … a n d m a p …

Prof Dieter Vogts


Email [email protected]
Office 090235

Moodle Site: https://funda.mandela.ac.za


Enrolment Key: Latte2025

2 2
Module Overview
Click to edit Master title style

3 3
Click to edit Master title style
Recommended Textbook
Optional – for those who like holding
something tangible…

Essential Algorithms: A Practical


Approach to Computer Algorithms
by Rod Stephens
ISBN 978-1-118-61210-1

4 4
Assessments
Click to edit Master title style

 Module Assessment
1:
Friday 14 March: 14:00-
16:00, and 16:30-18:30

 Module Assessment
2: Saturday 26 April:
9:00-11:00, and 11:30-
13:30

 Module Assessment
3: concession letters ASAP
Email
No letter = no concession
Friday 30 May: 9:00-
5 5

11:00, and 11:30-13:30


Click to edit
Practicals, Master title &
Assignments style
Quizzes

During Practical Sessions Between Practical Sessions

• Weekly sessions (unless told otherwise) • Complete a more substantial assignment


• Thursday • May consult other sources
• Labs 4 & 5
• 10:25-11:35 or 11:45-12:55
• Still own work

• Complete one or more quizzes • Start during a practical session


• theory and/or practical
• during a session, in the labs • Submit before next practical session
• individual, no assistance!! (Wednesday 23:55)
• i.e. weekly mini-tests
• Peer assess in same week (Friday 23:55)
• Start working on next assignment

6 6
Click to edit Master title style

7 7
Click to edit Master title style

Java
Capuchinos are sooo tasty…

8 8
Click to edit Master title style

9
Click to edit
Software Master title style
Installation

• Use same versions of Java and IntelliJ as found in the labs

• Software Install Files (use VPN)


• \\cs2-new.mandela.ac.za\Courses\WRAV2 + WRPV3 Software

• License Server Location


• http://cs-archive-new.mandela.ac.za

10
10
Click to edit Master title style

Java vs C#
Which is better? Does it matter?

11
11
Click to edit Master title style

12
12
Click toStructure
Files & edit Master title style

• Java & IntelliJ


• .java file per public class
• Folder structure mimics packages
• Project
• Container for all related files, e.g. a practical assignment
• Java source code resides in src folder
• A project can contain multiple Modules
• A module represents a sub-part of a project, e.g. a question in a practical
assignment

• C# & Visual Studio


• .cs file, does not need to match class name

• Both have
• Multi-language support, code completion, generation and checking tools
• Debugging tools – use them!!!

13
13
Click
Hello to edit Master title style
World

Java C#

See HelloWorld

14
14
Click to edit Master title style
User Input
Java C#

See LargerNumber 15
15
Click
OOP to edit Master title style
Java C#

See Parent, Child, Main


16
16
Click to edit Master title style
File I/O
Java C#

See SumNumbers
17
17
Click
UsefultoJava
editTutorial
Master Sites
title style

https://www.flaticon.com
• Jenkov - https://jenkov.com/tutorials/java/index.html
• Baeldung - https://www.baeldung.com/java-tutorial
• Official Java documentation - https://docs.oracle.com/javase/tutorial/

• Important!!
• Use Java Language Level 11 for WRAV201/2 (and WRPV301/2)
• due to Android currently supporting up to Language Level 11

• To check/set
• File > Project Structure > Language Level → 11

18
18
Click to edit Master title style

Identifiers & Variable Scope


Not just a number…

19
19
Click to edit Master title style
Identifiers

• Identifier
• name for a variable, method or class
• Variables
• must be declared in a class or method
• Methods
• may only be declared within a class
• may not be nested
• Class
• must have a globally unique full name (package included in full name)
• e.g. javafx.scene.control.Button & java.awt.Button
• typically one class per file, file has same name as class
• may be nested (within another class or method – but more on that in WRPV301)
20
20
Click to edit Operator
Assignment Master title
= style

• Creates variable or object in memory, associates with identifier


• Where created in memory depends on type
• Primitive
• basic non-OOP types, e.g. int, float, double, boolean, …
• int x = 10;
• typically lowercase
• Object
• Object instantiated from a Class using new operator
• Person joe = new Person(“Joe Soap”);
• typically uppercase
• String class exception, explicit new operator not needed, e.g. String s = “Hello”;
• Java has object versions of primitive types
• e.g. Integer for int, Float for float, etc.
• typically can convert between the two automatically, so why????
• Whenever you use the assignment operator, memory is altered
21
21
Click to edit Master title style

Memory
Say what now?

22
22
Click to edit
Allocating Master-title
Memory style
Objects

• Objects stored in heap memory space


• a place where blocks of memory of the required size is allocated to
objects
• each block
• has a unique number (in heap memory)
• provides quick access to object (because we know which block number it is in)

Block number: #1 #2 #3 #4 #5
Object: A B “joe” C Empty

23
23
Click to edit
Allocating Master-title
Memory style
Primitives
#10
Identifier Value

• each object has an associated stack


memory
• where primitives and references are allocated
• stores identifier names and values/references
• fill from the bottom
• reference = block # of object in heap
• altered by
1. variable declaration e.g. int x;
person #3
2. variable assignment e.g. x = 5; OR x = y;
3. method call (method parameters are added to the y 15
stack) x 10
24
24
Click to edit Master title style

Example 1
Basic Example

25
25
Click to edit
Example Master title
1 – initially style is empty
all memory
class Program
{
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}

Block #: #1 #2 #3 #4 #5 #6
Object: Empty Empty Empty Empty Empty Empty

26
Click to edit
Example Master
1 – new titleof
object style
type Program
public class Program
{
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}

Block #: #1 #2 #3 #4 #5 #6
Object: Program Empty Empty Empty Empty Empty
Object 1
1 27
Click to 1edit
Example Master
– stack title
memory forstyle
object created
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}

Block #: #1 #2 #3 #4 #5 #6
Object: Program Empty Empty Empty Empty Empty
Object 1
1 28
Click to edit
Example 1 – Master title
identifier style to stack
x added
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}

Block #: #1 #2 #3 #4 #5 #6
Object: Program Empty Empty Empty Empty Empty
Object 1 x 5
1 29
Click to edit
Example 1 – Master title style
String object created
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}

Block #: #1 #2 #3 #4 #5 #6
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 30
Click to edit
Example 1 – Master title
identifier style to stack
s added
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}

Block #: #1 #2 #3 #4 #5 #6
s #2
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 31
Click to edit
Example 1 – Master title
identifier style to stack
z added
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}

z ‘a’
Block #: #1 #2 #3 #4 #5 #6
s #2
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 32
Click to edit
Example 1 – Master title
identifier pi style
added to stack
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
} pi 3.141
z ‘a’
Block #: #1 #2 #3 #4 #5 #6
s #2
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 33
Example 1 – identifier mylist added to stack and
Click to edit Master
ArrayList title
object added style
to heap
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x; mylist #3
}
} pi 3.141
z ‘a’
Block #: #1 #2 #3 #5 #6
s #2
Object: Program “Hello” ArrayList Object 1 Empty Empty
Object 1 x 5
1 1 1 34
Example 1 – identifier j added to stack and contents
Click to editfrom
copied Master
x title style
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
j 5
int j = x; mylist #3
}
} pi 3.141
z ‘a’
Block #: #1 #2 #3 #5 #6
s #2
Object: Program “Hello” ArrayList Object 1 Empty Empty
Object 1 x 5
1 1 1 35
Example 1 – when method exits all identifiers
Click to editinMaster
declared method title style from stack
are removed
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}

Block #: #1 #2 #3 #5 #6
s #2
Object: Program “Hello” ArrayList Object 1 Empty Empty
Object 1 x 5
1 1 0 36
Example 1 – whenever no references exist to an object in
Click to edit
the heap, MasterCollector
the Garbage title style
(can) remove it
#1 (Program Object 1)
public class Program
{ Identifier Value
private int x = 5;
private String s = "Hello";

public static void main(String[] args)


{
new Program();
}

public Program()
{
char z = 'a';
double pi = 3.141;
ArrayList mylist = new ArrayList();
int j = x;
}
}

Block #: #1 #2 #3 #4 #5 #6
s #2
Object: Program “Hello” Empty Empty Empty Empty
Object 1 x 5
1 1 37
Click to edit Master title style

Method
Parameters

38
38
Click
Methodto edit Master title style
parameters

• when you call a method


• the parameters to the method are copied from the stack to a new location
• and identified by the identifiers given in the method declaration.
• consider the following method:
public void doStuff(double k)
{
k = 20.5;
}

• results in a new entry on the stack for identifier k


• if already a variable of same name, topmost used when referring to
it
39
Example 2 – Program created, added to heap, variables
Click to edit Master
created title style
on stack
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
else return y;
}
}
Block #: #1 #2 #3 #4 #5 #6
x 5
Object: Program Empty Empty Empty Empty Empty 40
Object 1 1
Example 2 – x assigned 20
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
else return y;
}
}
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 41
Object 1 1
Example 2 – larger added to stack
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
else return y;
}
}
larger ?
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 42
Object 1 1
Example 2 – call max, add params & copy values
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
y 10
else return y;
} x 20
}
larger ?
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 43
Object 1 1
Example 2 – assign x (to local variable of max)
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
y 10
else return y;
} x 0
}
larger ?
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 44
Object 1 1
Example 2 – return y (10)
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
y 10
else return y;
} x 0
}
larger ?
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 45
Object 1 1
Example 2 – exit max, so delete variables
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
else return y;
}
}
larger ?
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 46
Object 1 1
Example 2 – assign return value to larger
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
else return y;
}
}
larger 10
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 47
Object 1 1
Example 2 – display larger
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);

}
System.out.println(larger);
10
public int max(int x, int y) {
x = 0;
if(x > y) return x;
else return y;
}
}
larger 10
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 48
Object 1 1
Example 2 – exit constructor, delete larger
Click to edit Master title style
public class Program { #1 (Program Object 1)
private int x = 5;
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
else return y;
}
}
Block #: #1 #2 #3 #4 #5 #6
x 20
Object: Program Empty Empty Empty Empty Empty 49
Object 1 1
Example 2 – exit main, delete
Click to edit Master title style Program
public class Program {
private int x = 5;

public static void main(String[] args) {


new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
else return y;
}
}
Block #: #1 #2 #3 #4 #5 #6
Object: Program Empty Empty Empty Empty Empty 50
Object 1 0
Example 2 – exit main, zero references,
Click to edit Master
so garbage title style
collect
public class Program {
private int x = 5;

public static void main(String[] args) {


new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
else return y;
}
}
Block #: #1 #2 #3 #4 #5 #6
Object: Empty Empty Empty Empty Empty Empty 51
Example 2 – exit program, delete
Click to edit Master title style heap
public class Program {
private int x = 5;

public static void main(String[] args) {


new Program();
}

public Program() {
x = 20;
int larger = max(x, 10);
System.out.println(larger);
}

public int max(int x, int y) {


x = 0;
if(x > y) return x;
else return y;
}
}

52
Click to edit Master title style

Intermission
As far as we got in Lecture 1

53
Click to edit Master title style

Manipulating
Memory
From within a method

54
54
Click to edit
Changing Masterfrom
memory title within
style methods

• the previous example showed why one can not change the original
value of parameters from within a method
• however, because of the fact the objects are stored on the heap, it is
possible to change objects from within a method
• consider the following method:
public void updateList(ArrayList list) {
list.add("World");
}

55
Example 3 – create Program, stack, variables
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}

public void updateList(ArrayList list) {


list.add("World");
}
}

Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList Empty Empty Empty Empty 56
Object 1 1
[] 1
Example 3 – create “Hello” string
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}

public void updateList(ArrayList list) {


list.add("World");
}
}

Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” Empty Empty Empty 57
Object 1 1
[] 1 1
Example 3 – add string
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}

public void updateList(ArrayList list) {


list.add("World");
}
}

Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” Empty Empty Empty 58
Object 1 1
[#3] 1 1
Example 3 – display myList
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
[Hello]
System.out.println(myList);
}

public void updateList(ArrayList list) {


list.add("World");
}
}

Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” Empty Empty Empty 59
Object 1 1
[#3] 1 1
Example 3 – call updateList, copy params & values
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}

public void updateList(ArrayList list) {


list.add("World");
}
}
list #2
Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” Empty Empty Empty 60
Object 1 1
[#3] 2 1
Example 3 – call updateList, copy params & values
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}

public void updateList(ArrayList list) {


list.add("World");
}
}
list #2
Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” “World” Empty Empty 61
Object 1 1
[#3] 2 1 1
Example 3 – call updateList, copy params & values
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}

public void updateList(ArrayList list) {


list.add("World");
}
}
list #2
Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” “World” Empty Empty 62
Object 1 1
[#3, #4] 2 1 1
Example 3 – exit updateList, release values
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);
System.out.println(myList);
}

public void updateList(ArrayList list) {


list.add("World");
}
}

Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” “World” Empty Empty 63
Object 1 1
[#3, #4] 1 1 1
Example 3 – exit updateList, release values
Click to edit Master title style
public class Program { #1 (Program Object 1)
private ArrayList myList = new ArrayList();
Identifier Value
public static void main(String[] args) {
new Program();
}

public Program() {
myList.add("Hello");
System.out.println(myList);
updateList(myList);

}
System.out.println(myList); [Hello, World]
public void updateList(ArrayList list) {
list.add("World");
}
}

Block #: #1 #2 #3 #4 #5 #6
myList #2
Object: Program ArrayList “Hello” “World” Empty Empty 64
Object 1 1
[#3, #4] 1 1 1
Click
Note to edit Master title style

• you cannot change the value of the variable passed in as a parameter


• i.e. you cannot change the original int variable’s value to 10
• i.e. you cannot change the original object pointed too (from #3 to #10),

• but you can manipulate an object passed in as a parameter


• e.g. which could allow you to add/delete/edit items to a list
• i.e. you can change the fields of object #3

65
65
Click to edit Master title style

Equality
Are two things really equal?

66
66
Click to edit
Equality Master==
Operator, title style
#1 (Program Object 1)

Identifier Value
• the equality operator compares values in the stack memory

int x = 10;
int y = 10;
System.out.println(x == y); // prints "true"

String s = "Hello World";


ArrayList g = new ArrayList();
ArrayList h = new ArrayList();
g.add(s); h #4
h.add(g);
System.out.println(g == h); // prints "false" g #3
s #2
Block #: #1 #2 #3 #4 #5 #6 y 10
Object: Program “Hello ArrayList ArrayList Empty Empty x 10
Object 1 World” [#2] [#2] 67
67
Click to &
Strings edit MasterOperator,
Equality title style
==

• the equality operator compares values in the stack memory


• == has been changed for some classes, e.g. String

String s1 = "Bye!"; // e.g. #10


String s2 = "Bye!"; // e.g. #11
System.out.println(s1 == s2); // prints "true"

68
68
Click
== andto Equals
edit Master title style

Person p1 = new Person("Joe", 20);


• if you want objects Person p2 = new Person("Joe", 20);
System.out.println(p1 == p2); // prints "false"
to be compared on System.out.println(p1.equals(p2)); // prints "true"
their contents, not
the value in the public class Person {
stack private String name;
private int age;
• override the equals
method public Person(String name, int age) {
this.name = name;
this.age = age;
}

public boolean equals(Object object) {


if (!(object instanceof Person)) return false;
Person person = (Person) object;
return (name == person.name) && (age == person.age);
}
69
} 69
Click to edit Master title style

DIY
Now you try…

70
70
package memory.example5;
Click to edit Master title style
public void doStuff(String s) {
public class Program { s = "Goodbye";
public static void main(String[] args) { }
new Program();
} public void doStuffWithWrapper(StringWrapper w) {
w.theString = "Cheers";
public Program() { }
String s = "Hello"; }
StringWrapper w = new StringWrapper();

doStuff(s); public class StringWrapper {


System.out.println(s); public String theString = "Hi";
}
doStuff(w.theString);
System.out.println(w.theString);

doStuffWithWrapper(w);
System.out.println(w.theString);
} Hello
Hi
Cheers
71
71
Click to edit Master title style

Homework
Already??

72
72
Click
To doto edit Master title style

• Moodle
• Enroll for WRAV201 on funda.mandela.ac.za and yenza.mandela.ac.za
• Enrolment key is Latte2025
• Book lecture and practical sessions
• Software (at home)
• Install Java Development Kit & IntelliJ
• file://postgrad-new.mandela.ac.za/wrpv%20software
• Go through the IDE Tour after installation
• Practical Sessions
• Start this week
• Complete first quiz during practical session in lab
• Start working on practical assignment, which is due next week
• Do the homework exercise in the Lecture 2 (on Moodle) for next week 73
73

You might also like