1
h01
CS56 F16
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
4pm, 5pm or 6pm
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h01: HFJ 3,4: Primitives, References, Instance Variables, Methods

ready? assigned due points
true Wed 09/28 12:30PM Mon 10/03 12:30PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


Please:

  • No Staples.
  • No Paperclips.
  • No folded down corners.

Reading Assignment:

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts (4pm, 5pm or 6pm) in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
  2. Based on your reading in :
    1. (5 pts) In Java, a variable can store a primitive or a reference. Briefly: What’s the difference?

    2. (5 pts) If I write 3.4, is that of type double, or float?

    3. (5 pts) Declare x as a double and assign it the value 3.4 (as a double)

    4. (5 pts) Declare y as a float and assign it the value 3.4 (as a float)

  3. (5 pts) In C++, the name of a plain old array of Student objects is not an object, but is rather a pointer to a Student (i.e. it is of type Student *. What about in Java—is an array an object, yes or no?

  4. Variables that represent a primitive type (e.g. boolean x; or int y;) and variables containing object references (String w; or Student z;) have this in common—they are both composed of bits in memory.

    But, as explained in , they differ in what the bits ‘‘actually’’ represent. You won’t get this one by just guessing—you really have to read the book.

    1. (5 pts) What do the bits that represent int y; represent?

      Assume that y is assigned the value 13

    2. (5 pts) What do the bits that represent String w; represent?

      Assume that w is assigned the value "foo".

  5. Based on your reading in , p. 59-62 and p. 84:

    1. (10 pts) Suppose I have a class called Student.
      How do I declare and allocate space for a plain old Java array called students that can hold 5 references to Student objects?

    2. (10 pts)

      Java for loops look pretty much just like C++ for loops (see HFJ page 10 if you really need to check.) Given that, assuming there is a default constructor Student() that you can call to create a new Student object, write a for loop that initializes all of the elements of the array students (from the previous problem) to be instances of the Student> class.

  6. Consider these questions about memory—answers are in
    1. (5 pts) Does the amount of memory taken up by an object reference differ for different kinds of objects (say String vs. ArrayList<String>?)

    2. (5 pts) Does the amount of memory taken up by the object itself differ for different kinds of objects (assuming the same JVM)?
    3. (5 pts) Can the amount of memory taking up for an object reference for a object particular type (say String) differ from one JVM to another?

  7. discusses the difference between the == operator and the .equals method.

    1. (10 pts) Under what circumstances should you use the == operator to compare two variables?

    2. (10 pts) Under what circumstances should you use the .equals method to compare two variables?