Week 2 Lecture 02
Week 2 Lecture 02
Object-Oriented Programming
Lecture 2
I further acknowledge the Traditional Owners of the country on which you are on
and pay respects to their Elders, past, present and future.
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been reproduced and communicated to you by or on behalf of the
University of Sydney pursuant to Part VB of the Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright under the Act. Any
further copying or communication of this material by you may be the subject of
copyright protection under the Act.
Do not remove this notice.
● Control Flow
● While/do-while/for loop
● For-each loop
● Static Methods
Refer to Chapter 4.1, page 237, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
5
What if we changed i to 5?
Refer to Chapter 4.1, page 237, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
● while
● do-while
● for
● for-each
The constructs are part of the language’s syntax and typically follow a
similar pattern.
As with if statements, for this branch to start and continue execution the
condition must be true.
A boolean expression is
evaluated here and is
checked on every
initialization; iteration.
while(condition) {
Refer to Chapter 4.1, pages 238-251, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
As with if statements, for this branch to start and continue execution the
condition must be true.
int i = 30;
while(i < 40) {
doWork();
i += 1;
}
Refer to Chapter 4.1, pages 238-251, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
Similar to the while loop but it will always execute the block at-least
once and continue execution if condition is true.
Refer to Chapter 4.1, pages 238-251, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
doWork();
Refer to Chapter 4.1, pages 251-260, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
doWork();
We are able to create
and initialise variables
} for our loop here. They
will be restricted to the
loop’s scope.
doWork();
A common variable is a
counter for our for loop.
}
doWork();
The boolean expression
to the inputted here.
} No different than a
while loop
doWork();
Let’s say we wanted to
loop 10 times
}
doWork();
This is the update
component. Were we
} update any variables
defined within the variable
section (or variables defined
in the outer scope)
doWork();
So we can increment by
1, similar to the while
} loop.
doWork();
So we can increment by
1, similar to the while
} loop.
doWork(binding);
Refer to Chapter 4.1, pages 260, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
A collection is an object
that aggregates other
objects.
doWork(binding);
A binding in this case is
just some variable that
} will represent an
element of the
collection.
System.out.println(str);
Declaration of a String
variable that will be an
} element in the
collection.
String[], ArrayList<String>,
List<String>,
Set<String>,
Deque<String>
for( String str : strings ) { …
System.out.println(str);
System.out.println(str);
System.out.println(str);
Syntax:
static [final] return_type name ([parameters])
Example:
Refer to Chapter 6.2, pages 433-436, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
f(....)
main(....)
● Arrays
● Multidimensional Arrays
● Strings
● StringBuilder
When an array is initialised the array will be allocated and will return the
address of where the array is stored.
0x100C
2 8 3 4 90 12 45 32 43 76 1 -9 2 44 65 78
Within the java language we are able to intiailise an array in a few different
ways. Most commonly and what is typically used is allocate and specify size.
Refer to Chapter 7.1, pages 525-541, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
We have seen primitive type arrays but what about reference type
arrays?
This infers that reference type arrays do not contain a string but a
reference to a string.
Refer to Chapter 7.1, pages 525-541, (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
We are not limited just creating single dimensional arrays. We are able
to create multi-dimensional arrays.
There are two types, one adheres to a matrix-like structure and the
other is commonly referred to as a jagged array.
Arrays are also reference types. When initialised, the variable array will
contain 3 null elements. We are able to specify lengths on each
elements.
jarray[0] = new int[5];
jarray[1] = new int[10];
It will initialise the 2 elements within the array to null. This allows us to
set each element to a separate array.
When initialising a string type, the JVM will allocate memory to contain
the string.
When initialising a string type, the JVM will allocate memory to contain
the string.
Meow
When assigned, the string is allocated and binded to
the variable “cat”.
Any reference type variable holds onto the memory reference of the
object.
System.out.println(cat1 == cat2);
We can see when run the above the output will be true.
Memory
Any reference type variable holds onto the memory reference of the
object.
We can see when run the above the output will be false.
Memory
Refer to Chapter 3.2, pages 189=191 (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
Any reference type variable holds onto the memory reference of the
object.
We can see when run the above the output will be false.
Memory
Refer to Chapter 3.2, pages 189=191 (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
Reference type does not implicitly have the ability to compare itself to
another type (besides reference) without first defining a method. To
compare them, we need to write a method to compare them.
System.out.println(cat1 == cat2);
Refer to Chapter 3.2, pages 189=191 (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
Reference type does not implicitly have the ability to compare itself to
another type (besides reference) without first defining a method. To
compare them, we need to write a method to compare them.
System.out.println(cat1.equals(cat2));
Refer to Chapter 3.2, pages 189=191 (Java, An Introduction to Problem Solving & Programming, Savitch & Mock)
When writing our first program we have been able to define a string
literal. However java employs an intelligent but often mistaken
optimisation for strings.
This allows the compiler to optimise for memory usage. The compiler
will use the same allocation and provide same reference to a string
variable of refers to the same literal.
Recreating a new string and deallocating the old one can be costly for
the java virtual machine (jvm).
Remember: Each time we use += with the String type we are creating a
new string. The String class is immutable and this can have great
benefits for ensuring we have a read only or for simple concatenations.