From leavens@cs.iastate.edu Sun Sep 19 18:06:21 2004 Date: Sun, 19 Sep 2004 18:06:22 -0500 (CDT) From: Gary T. Leavens To: Ted Skjei Cc: cs362s@cs.iastate.edu Subject: Re: problem 3 on practice exam and a few other questions Hi Ted, On Sun, 19 Sep 2004, Ted Skjei wrote: > In problem 3 on the practice exam the method, public int /*@ pure @*/ > compareTo(Object o), throws an exception without having an exception clause > before the method body (throws ClassCastException). I thought you had to have > the clause if you were going to throw an exception in a method; maybe I missed > something in chapter 8. There are two kinds of exceptions: checked and unchecked. ClassCastException is unchecked, as it is a subtype of java.lang.RuntimeException. So although it can be thrown by that method, it doesn't have to be declared. > The other question I had was about the /*@ pure @*/ comment. I wrote some > things down in my notes, but I didn't clarify it well enough for me to remember. > Do these docentation tags have a special name? I was trying to find info. on > them in the book and on the internet but could not find any. Could you give me > information on some others too (such as /*@ non_null @*/, /*@ spec_public @*/, > etc.)? All these things in /*@ ... @*/ and after //@ are JML annotations. There is documentation on jmlspecs.org (and I'll pass some out later in the semester). But for now: pure - means that a method has no side effects, this is needed for methods that are called inside JML assertions. non_null - means that the following field, formal, or return value cannot be null. spec_public - means that the following name is declared to be public for specification purposes. This allows it to be used in (client-visible) assertions for public methods. Let me know if you want more detail on any of these. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ----------------------------------------- From leavens@larch.cs.iastate.edu Sun Sep 19 21:36:24 2004 Date: Sun, 19 Sep 2004 21:36:23 -0500 (CDT) From: Gary T. Leavens To: Aaron Becker Cc: Staff for Com S 362 -- Kun Liang , Matthew Ring Subject: Re: question about new instances Hi Aaron, On Sun, 19 Sep 2004, Aaron Becker wrote: > I'm a bit confused about new objects, and I hope you are willing/have the > time to help me. Sure. > When I declare a new object, where is the object reference > stored? I know the object is put in the heap, but is the object reference in > the stack? There are two separate actions. 1. The expression new C(...), for some class C, creates an object on the heap, runs the appropriate constructor body to intialize it, and returns a reference to that object, as the result of the expresion. 2. When you make a declaration of a local variable, such as C x; this allocates a location on the runtime stack that can hold a reference to a C object (or null, or any subtype of C). It has no reference assigned to it, but you can do that either with an initalizer expression, or in a later assignment expression. Thus if you write declaration of a local variable with an initializer C x = new C(...); you first do action 2, followed by action 1 (although I'm not sure the order matters or is observable), and then the assignment of the reference produced by the expression new C(...) is put into the variable x. Declarations of fields are similar, but they are given initial values in most cases (null for reference types) as part of their declaration and fields are not stored on the stack, but on the heap... > 2.) If obj1 contains as member fields, an obj2 and an obj3, and if obj2 has a > member field obj4, ie: > > _.--->obj2----->obj4 > / > obj1--------->obj3 > > > Then where is the object reference that obj1 has to obj2 stored? in the heap? Yes, the storage for obj1 contains locations for the fields it has declared. Presumably there is a field C2 obj2; in the code of the class of obj1. When the a new expression is used to allocate obj1, it allocates these locations for its fields and the constructor or whatever assigns the reference to obj2 to it. Thus all fields in objects are stored on the heap, as is the entire object. > For that matter, where is the object reference that obj2 has to obj4 stored? Also on the heap. Does that help? Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ------------------------------------------ From leavens@cs.iastate.edu Mon Sep 20 23:28:02 2004 Date: Mon, 20 Sep 2004 23:28:03 -0500 (CDT) From: Gary T. Leavens To: Ted Skjei Cc: cs362s@cs.iastate.edu Subject: Re: hw3 presentation Hi Ted, On Mon, 20 Sep 2004, Ted Skjei wrote: > May we make a PowerPoint presentation? No, sorry, there isn't time to change from one student to the next, so I would ask that you just talk in front of the room. You can bring a poster or draw on the board if you wish. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ----------------------------------