Algorithms and OOD (CSC 207 2014F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)] [Issue Tracker (Textbook)]
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.
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.
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?
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?
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?
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?
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?
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?
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?
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?
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?
If you find that you have extra time, read the Java tutorial on boxing/unboxing.