Transcript Slides_18

BIT 115: Introduction
To Programming
Professor: Dr. Baba Kofi Weusijana
(say Doc-tor Way-oo-see-jah-nah,
Doc-tor, or Bah-bah)
[email protected]
http://edutek.net/cascadia/bit115
BIT 115: Introduction To Programming
Quiz
• https://catalysttools.was
hington.edu/webq/surve
y/babaw/56789
BIT 115: Introduction To Programming
2
• Today:
– Ch 8.2 Reference Variables
• Monday:
– Review for final exam
• Wednesday:
– Final Exam! (you'll be here, right?)
BIT 115: Introduction To Programming
Logistics
•
A4, J5 due today
•
All A04 regrades due next Monday
–
–
before class
I won’t grade anything but the final exam
turned in after that
BIT 115: Introduction To Programming
Reference Variables
• How they are stored in memory
– Review the textbooks diagrams in 8.2!
– A primitive type always has memory available
– A reference type uses pointers and can actually
be set up so that it doesn't refer to anything
•Robot bob = null;
•Robot joe;
// Also points to
'null', although Java won't let you
use it till you initialize it
BIT 115: Introduction To Programming
5
What’s passed how?
•
•
•
•
Primitives are passed 'by value'
Arrays are passed 'by reference'
All Objects are passed 'by reference'
How they're "copied"
– Copy by value, vs. copy by reference
– Having more than one reference/name is
aliasing, no copy has been made of the actual
object!
– You can copy objects/reference variables by
cloning them with the clone method
• You should override and define the clone method
for Classes
you write
BIT 115: Introduction To Programming
6
Equality testing
• primitives: ==
• references themselves: ==
– checks to see if they refer to the same object in
memory
• The objects that the references refer to:
.equals method
– checks for the same internal value
• Demo: StringPointers.java
BIT 115: Introduction To Programming
7
Automatic Garbage Collection
• In ICE 17 you part 1 you wrote:
–System.out.println(userText.toUpperCase());
• toUpperCase produces a new object,
distinct from the original object referred to
by userText. A reference to that new
object is passed to println, which then
print the String object on the screen.
BIT 115: Introduction To Programming
8
Automatic Garbage Collection
• Once that's done, though, nobody keeps a reference
to that new String object.
• Since we don't have a reference to the object, we
can't (ever again) use it.
• Thus, the memory is inaccessible – it's dead space.
• In a language like C++, this would be bad! C++
would never realize that the memory won't be used,
and we'll never use it – that object would occupy
space that we can't use for anything else.
– Thus, we have leaked memory.
BIT 115: Introduction To Programming
9
Automatic Garbage Collection
• One of the main selling points of Java is that it's a language that
offers automatic memory management, or "garbage collection".
– Java WILL be able to figure out that that memory won't be used any more,
and will reclaim it.
– Java will collect the garbage memory, and re-use it.
• Drawback:
– For most JVMs, this means that when you try and create a new object (as in
new String("Baba") ), if there's no memory left, the JVM might try
to collect garbage memory.
• In order to do this, it'll probably stop your program, do it's
collection, and then let your program resume
– That’s one reason why Java is slower than C++ for some
applications
– Also why Java is much easier to use than C++
• This is an example
a typical
engineering trade-off
BIT 115:of
Introduction
To Programming
10
ICE 18
• How to do printArray correctly
BIT 115: Introduction To Programming
11