CS712/CS812
Homework 1
Spring 2006
Due Date: Wednesday January 25


The goal for this assignment is to have you read and grapple with The Java Language Specification, 3rd edition, a real specification for a real language.

You should provide answers to the following questions about Java. To support each answer cite the relevant section number and quote the appropriate text of the specification.

  1. What will the following program print? Why?

    class q1 {
        public static void main(String args[])
        {
          Object a = new Integer(19);
          Object b = new Integer(19);
          if (a == b)
            System.out.println("true");
          else
            System.out.println("false");
        }
    }
    

  2. Can null be used as a variable name? Why or why not? Can long be used as a variable name? Why or why not?

  3. Is 10D a valid floating-point literal? Why or why not?

  4. Can you have duplicate statement labels in a method? Why or why not?

  5. What is method overriding? Provide an example Java program to illustrate method overriding. Be sure to include a call of the overridden method via a reference to the superclass.

  6. What is method overloading? Provide an example program to illustrate method overloading.

  7. What does it mean for one field declaration to hide another? Is it legal for a field of one type to hide a field of a different type?

  8. Can a local variable shadow another local variable? Can a local variable shadow a parameter? Can a local variable shadow a field? Can a method parameter shadow a field? Provide examples of these cases.

  9. Can you assign a value of a class type to a variable with the type of its superclass? Why or why not? Can you assign a value of a class type to a variable with the type of its subclass? Why or why not?

  10. How does the language specification rule out an assignment to a literal? What exactly can be on the left-hand side of an assignment operator?

Please turn-in your homework in class on Wednesday January 25. If you cannot be in class that day, please make arrangements to give me your paper in advance. No late papers will be accepted.

You are expected to do your own work on this assignment.


Last modified on January 16, 2006.

Comments and questions should be directed to hatcher@unh.edu