Truth or Consequences A Discussion of Java’s Memory Model

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

the javascript language BY SA.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Some containers are called linear because their content (items) are stored in a single row (line). Which of the following are linear?  the rungs of a.
CS 307 Fundamentals of Computer Science 1 Java Basics – Pointers and Object Variables -Topic 3 ` Or else it doesn't, you know. The name of the song is.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Topic 6 Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Arrays And ArrayLists - S. Kelly-Bootle
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1-1 Generic Types in Java Format for a generic (parameterized) type and instantiation of a generic type.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Records Engineering 1D04, Teaching Session 11. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Records  So far we've seen a number of different.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Variables, Primitives, and Objects A Visual Learner’s Guide.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
As you arrive… FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code.
Constructors & Garbage Collection Ch. 9 – Head First Java.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Linear Containers Some containers are called linear because their content (items) are stored in a single row (line). Which of the following are linear?
Topic: Classes and Objects
Primitive/Reference Types and Value Semantics
KS4 Mathematics A2 Linear equations.
Haidong Xue Summer 2011, at GSU
Lecture 5: Some more Java!
Intro To Classes Review
Introducing Java Generics and Collections
Multiple variables can be created in one declaration
Strings and Object References
Creating Objects & String Class
Homework Reading Machine Projects Labs
Topic 6 Generic Data Structures
C++ -> Java - A High School Teacher's Perspective
#include "std_lib_facilities
Advanced Programming Behnam Hatami Fall 2017.
Chapter 10: An Array Instance Variable
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Object Oriented Programming in java
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Chapter 4 Topics: class declarations method declarations
More About Inheritance & Interfaces
Namespaces, Scopes, Access privileges
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
ArrayLists 22-Feb-19.
Java Classes and Objects 4th Lecture
CSE 143 Lecture 5 References and Linked Nodes
Based on slides by Alyssa Harding & Marty Stepp
Simple Control Structures
KS4 Mathematics A2 Linear equations.
Data Structures & Algorithms
Simple Control Structures
Classes, Objects and Methods
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Review for Midterm 3.
Chapter 7 Objects and Classes
Presentation transcript:

Truth or Consequences A Discussion of Java’s Memory Model Steven K. Andrianoff, Joe Kmoch, and David B. Levine 4/18/2019

Motivation Inspired by a talk at NECC, June, 2003 entitled “Java Gotchas” by Tom West Many of the “gotchas” were caused by a lack of understanding of Java’s use of memory This matched our experiences learning Java and those of our students 4/18/2019

Today’s Approach Examine some areas where a misunderstanding of the model may cause a student (or teacher) to be puzzled by a program’s behavior Philosophize a bit Show examples Explain principles 4/18/2019

Today’s topics Creating objects Testing objects for equality Passing parameters (by value) Casting Dealing with linked structures 4/18/2019

On Creationism “There is nothing new under the sun but there are lots of old things we don't know.” Ambrose Bierce The Devil's Dictionary “If you want to make an apple pie from scratch, you must first create the universe.” Carl Sagan 4/18/2019

Creating Objects ERROR!!!! Widget foo; Widget foo(23,”C++”,Color.RED); Widget bar = new Widget( 42, “Java”, Color.MAGENTA); foo = new Widget( 23, “Java”, Color.CYAN); 4/18/2019

Creating Objects in Java Objects in Java are created ONLY through the use of the new operator A declaration of a variable in Java creates a reference (initially null) to an object of the declared type 4/18/2019

On Equality “All animals are equal but some animals are more equal than others.” George Orwell Animal Farm “I never said that I treat my children equally; I do, however, try to treat them fairly.” Ann Levine (Dave’s Mother) 4/18/2019

Memory and Equality true expressions W == X W.equals(X) X.equals(Y) false expressions W == Z W.equals(Z) X == Y 4/18/2019

Equality in Java Java supports two kinds of equality Equality of identity – two references refer to the same object This is the meaning of the == operator Equality of content (equivalence) – two references refer to object(s) with the same content This is the purpose of the equals() method 4/18/2019

On Names “The name of the song is called "HADDOCKS' EYES."' `Oh, that's the name of the song, is it?' Alice said, trying to feel interested. `No, you don't understand,' the Knight said, looking a little vexed. `That's what the name is CALLED. The name really IS "THE AGED AGED MAN."' `Then I ought to have said "That's what the SONG is called"?' Alice corrected herself. `No, you oughtn't: that's quite another thing! The SONG is called "WAYS AND MEANS": but that's only what it's CALLED, you know!' `Well, what IS the song, then?' said Alice, who was by this time completely bewildered. `I was coming to that,' the Knight said. `The song really IS "A-SITTING ON A GATE": and the tune's my own invention.‘” Lewis Carroll Through the Looking Glass, Chapter 8 4/18/2019

“Swapping” Object References swap(x,y) public void swap (Object a, Object b) { Object temp = a; a = b; b = temp; } At end of invocation Before invocation At beginning of invocation After invocation 4/18/2019

“Swapping” Object Contents swap(x,y) At end of invocation Before invocation At beginning of invocation public void swap (Object a, Object b) { … a.setContents( b.getContents()); } After invocation 4/18/2019

Parameter Passing in Java ALL Parameters in Java are passed by value! This is easy to understand for primitive data types (see C++) It is also easy to understand for objects IF you keep the model in mind 4/18/2019

On Casting “The glorious gifts of the gods are not to be cast aside.” Homer The Iliad “Education is the process of casting false pearls before real swine.” Irwin Edman Columbia University 4/18/2019

Casting About ArrayList a = new ArrayList(); String s = new String(“Hello”); a.add(s); System.out.println( a.get(0).length() ); String t = (String) a.get(0); System.out.println(t.length() ); System.out.println(((String) a.get(0)).length() ); OK! The value returned by get() is cast to a String – which does have a length() method! ERROR! ArrayList’s get() returns an Object – which does not have a length() method! CLEARER STILL? 4/18/2019

Types in Java Objects (of derived classes) have more than one type; in particular, they are instances of each class in the inheritance chain The compiler will only permit method invocation if the (believed) type supports that invocation. Casting is a promise to the compiler that an object has a given type. 4/18/2019

On Keeping Your Head “I'm going to memorize your name and throw my head away.” Oscar Levant “I think that I shall never see a structure lovely as a tree.” AP T-shirt (Fran Trees, 1989) inspired by Joyce Kilmer 4/18/2019

insert(groceries,”eggs”); Adding to a list: insert(groceries,”eggs”); After invocation After garbage collection At end of invocation Before invocation At beginning of invocation public void insert( GroceryList list, String food) { list = new GroceryList (list, food); } 4/18/2019

groceries = insert(groceries,”eggs”); Adding to a list: groceries = insert(groceries,”eggs”); At end of invocation After invocation (and assignment) At beginning of invocation Before invocation public GroceryList insert( GroceryList list, String food) { return new GroceryList (list, food); } 4/18/2019

On Keeping Your Head in Java If a method modifies a structure that is a parameter, then the method should return the modified structure. Good class design can avoid this problem in many cases. 4/18/2019

On Life with Java? “What a waste it is to lose one's mind. Or not to have a mind is being very wasteful. How true that is.” J. Danforth Quayle 4/18/2019

In the End All of the “gotchas” in this talk can bite – and hurt when they do. All can be avoided by keeping track of the programming (memory) model being used. The model is consistent (and simple), but it is not the one used by C++! 4/18/2019

<insert pithy closing quote here> 4/18/2019