Algorithms and OOD (CSC 207 2014S) : Labs

Lab: Automatic Boxing and Unboxing


Summary: We conduct a series of experiments to help ourselves understand the basics of autoboxing and autounboxing, as well as a few related issues in type conversion.

Prerequisite Knowledge: Java basics. Primitive types. Objects.

Preparation

a. Fork and clone the repository at https://github.com/Grinnell-CSC207/lab-boxing.

b. Skim the class named Experiments and make note of what types each procedure expects.

c. Skim the main method and make note of the type of each variable.

d. Compile and run Experiments and make note of the value of each variable.

Exercises

Exercise 1: Normal Calling Mechanisms

Consider the following instructions.

    pen.println("square(bigi): " + square(bigi));
    pen.println("sqr(littlei): " + sqr(littlei));
    pen.println("bracket(str): " + bracket(str));
    pen.println("bigSum(bigvals): " + bigSum(bigvals));
    pen.println("littleSum(littlevals): " + littleSum(littlevals));
    pen.println();

a. Do you expect the Java compiler to accept these instructions? Why or why not?

b. If the Java compiler accepts these instructions, what output would you expect to see when you run the program?

c. Check your answers to a and b experimentally.

d. What, if anything, does this exercise suggest about autoboxing/unboxing or related issues?

Exercise 2: Integers and Strings

Consider the following instructions.

    pen.println("bracket(bigi): " + bracket(bigi));
    pen.println("bracket(littlei): " + bracket(littlei));
    pen.println();

a. Do you expect the Java compiler to accept these instructions? Why or why not?

b. If the Java compiler accepts these instructions, what output would you expect to see when you run the program?

c. Check your answers to a and b experimentally.

d. What, if anything, does this exercise suggest about autoboxing/unboxing or related issues?

Exercise 3: Basic Autoboxing/Unboxing

Consider the following instructions.

    pen.println("square(littlei): " + square(littlei));
    pen.println("sqr(bigi): " + sqr(bigi));
    pen.println();

a. Do you expect the Java compiler to accept these instructions? Why or why not?

b. If the Java compiler accepts these instructions, what output would you expect to see when you run the program?

c. Check your answers to a and b experimentally.

d. What, if anything, does this exercise suggest about autoboxing/unboxing or related issues?

Exercise 4: Yet Another Square Procedure

Consider the following procedure.

  /**
   * Square an Integer, using boxing/unboxing.
   */
  public static Integer
  sq(Integer x)
  {
    return x * x;
  } // sq(Integer)

Consider also the following instructions, which will get added to main.

     pen.println("sq(bigi): " + sq(bigi));
     pen.println("sq(littlei): " + sq(littlei));
     pen.println();

a. Do you expect the Java compiler to accept this new procedure and the corresponding instructions? Why or why not?

b. If the Java compiler accepts these instructions, what output would you expect to see when you run the program?

c. Check your answers to a and b experimentally.

d. What, if anything, does this exercise suggest about autoboxing/unboxing or related issues?

Exercise 5: More Fun with Strings

Consider the following instructions.

    pen.println("parenthesize(str): " + parenthesize(str));
    pen.println("parenthesize(bigi): " + parenthesize(bigi));
    pen.println("parenthesize(littlei): " + parenthesize(littlei));
    pen.println();

a. Do you expect the Java compiler to accept these instructions? Why or why not?

b. If the Java compiler accepts these instructions, what output would you expect to see when you run the program?

c. Check your answers to a and b experimentally.

d. What, if anything, does this exercise suggest about autoboxing/unboxing or related issues?

Exercise 6: Boxing and Arrays

Consider the following instructions.

    pen.println("littleSum(bigvals): " + littleSum(bigvals));
    pen.println("bigSum(littlevals): " + bigSum(littlevals));
    pen.println();

a. Do you expect the Java compiler to accept these instructions? Why or why not?

b. If the Java compiler accepts these instructions, what output would you expect to see when you run the program?

c. Check your answers to a and b experimentally.

d. What, if anything, does this exercise suggest about autoboxing/unboxing or related issues?

Exercise 7: What about null?

The reading notes that one potential issue with autounboxing is that we have to deal with null as a special case.

Consider the following instructions.

    Integer ex7 = null;
    pen.println("square(ex7): " + square(ex7));
    pen.println();

a. Do you expect the Java compiler to accept these instructions? Why or why not?

b. If the Java compiler accepts these instructions, what output would you expect to see when you run the program?

c. Check your answers to a and b experimentally.

d. What, if anything, does this exercise suggest about autoboxing/unboxing or related issues?

Exercise 8: What about null? Revisited

The reading notes that one potential issue with autounboxing is that we have to deal with null as a special case.

Consider the following instructions.

    String ex8 = null;
    pen.println("bracket(ex8): " + bracket(ex8));
    pen.println();

a. Do you expect the Java compiler to accept these instructions? Why or why not?

b. If the Java compiler accepts these instructions, what output would you expect to see when you run the program?

c. Check your answers to a and b experimentally.

d. What, if anything, does this exercise suggest about autoboxing/unboxing or related issues?

Exercise 9: What about null? Re-revisited

The reading notes that one potential issue with autounboxing is that we have to deal with null as a special case.

Consider the following instructions.

    String ex9 = null;
    pen.println("parenthesize(ex9): " + parenthesize(ex9));
    pen.println();

a. Do you expect the Java compiler to accept these instructions? Why or why not?

b. If the Java compiler accepts these instructions, what output would you expect to see when you run the program?

c. Check your answers to a and b experimentally.

d. What, if anything, does this exercise suggest about autoboxing/unboxing or related issues?

For Those With Extra Time

If you find that you have extra time, read the Java tutorial on boxing/unboxing.

Copyright (c) 2013-14 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.