Experiments in Java


Experiments for Session X1: Primitive Types

Name: ________________
ID:_______________

Experiment X1.1: Facts about integers

In this experiment you will investigate the storage of integer values on your own particular machine. (In Java, the machine you are on should not matter, but it is still useful to explore the limitations directly.)

Required file:

Step 1. Make a copy of FactsAboutIntegers.java, which is given by


import SimpleOutput;

/**
 * A program that prints simple ``facts'' about integer values.
 *
 * @author Samuel A. Rebelsky
 * @author Your Name Here
 * @version 1.1 of January 1999
 */
public class FactsAboutIntegers {
  /**
   * Print some interesting facts about integers in Java.
   */
  public static void main(String[] args) {
    SimpleOutput out = new SimpleOutput();
    out.println("The smallest allowable integer (int) value is " +
                Integer.MIN_VALUE);
  } // main(String[])
} // class FactsAboutIntegers


Compile and execute the program and record the value of the minimum integer.


 
 
 

Step 2. Modify the program in step 1 to find the value for the largest integer. Record what you learn.


 
 
 

Step 3. Since integers are represented in two's complement notation, the maximum and minimum integer values should be one less than some power of two (2n - 1, for some integer n). For what value of n is the value of the maximum integer on your machine equal to 2n - 1?


 
 
 

Step 4. Use the information collected in steps 1 through 3 to determine the number of bits used in Java to represent data of type integer. Explain your answer.


 
 
 
 
 
 

Experiment X1.2: Other integer types

Required file:

Step 1. Make appropriate modifications to FactsAboutIntegers to help you fill in the appropriate values. Note that the base types are byte, short, int, and long. The corresponding maximum values are Byte.MAX_VALUE, Short.MAX_VALUE, Integer.MAX_VALUE, and Long.MAX_VALUE.

Note that the number of distinct values is traditionally the number of values that fall between the minimum and maximum value (inclusive).

   Minimum and maximum values of various forms of integers

Type    Minimum Value      Maximum Value     # of Distinct Values

byte

short

int

long

Step 2. What, if anything, can you say about the relationship of the numbers of distinct values (the final column in the table from step 1)?


 
 
 
 
 
 

Step 3. Given the evidence from steps 1 and 2, and any other evidence you can develop, determine the number of bits used to represent each of the primitive integer types.

Experiment X1.3: Exceeding limits

Required file:

In this experiment you will determine how your computer system responds to programs that try to produce integer values that exceed the limits of the particular representation.

Step 1. Make a copy of IntegerLimits.java. Here is the code for that program


import SimpleOutput;

/**
 * Compute some information on what happens when we work near
 * the limits for the values of integers.
 *
 * @author Samuel A. Rebelsky
 * @author Your Name Here
 * @version 1.0 of January 1999
 */
public class IntegerLimits {
  /**
   * The various tests.
   */
  public static void main(String[] args) {
    // The following lines are to be used in a future experiment.
    int i = Integer.MIN_VALUE;
    SimpleOutput out = new SimpleOutput();
    i--;  // Shorthand for i = i - 1
    out.println("When we subtract one from " + Integer.MIN_VALUE +
                " we get " + i);
  } // main(String[])
} // class IntegerLimits


Compile and execute IntegerLimits. Record the result. What does this suggest?


 
 
 

Step 2. Test the limits of each of the four integer types. For example, to test what happens when we subtract 1 from the smallest byte, we would use

    byte i = Byte.MIN_VALUE;
    i--;
    out.println("When we subtract one from byte " + Byte.MIN_VALUE +
                 " we get + " i);

Use the results from your program to fill in the following table.

    Results of subtracting one from smallest value
    
type     smallest value      one less

byte

short

int

long

Step 3. What do those results suggest?


 
 
 
 
 
 

Step 4. What happens if you use

    i = i - 1;

rather than

    --i;

Check the modification on each of the four integer types and record any error messages you receive. What do these messages suggest?


 
 
 
 
 
 

When you have completed your answer, you may wish to check the notes on this problem.

Step 4. Repeat step 2 using two less than the smallest value. Use the results from your program to fill in the following table.

    Results of subtracting two from smallest value
    
type     smallest value      two less

byte

short

int

long

Step 6. Repeat step two using one more than the largest value, two times the largest value, and two times the smallest value. You can use ++ to increment values. You may not be able to fill in all of the spaces in the table for the reasons given in step 4.

   Exceeding the smallest and largest values
   
type     smallest  smallest-1  smallest*2  largest  largest+1  largest*2

byte

short

int

long

Step 7. What do the results of step 6 suggest?


 
 
 
 
 
 

Step 8. Update IntegerLimits to count up from Integer.MAX_VALUE-10 for twenty steps. You might use

    int num = Integer.MAX_VALUE - 10;
    int i;
    out.println("Printing the twenty one values starting with " + num);
    for (i = 0; i <= 20; ++i) {
      out.println(num);
      ++num;
    } // for

Recompile and execute IntegerLimits. Summarize and explain the results.


 
 
 
 
 
 

Step 9. Repeat the previous step, counting down from the smallest integer plus ten. Again, summarize and explain your results.


 
 
 
 
 
 

Experiment X1.4: Facts about reals

In this experiment you will investigate the storage of real values on your own particular machine. (In Java, the machine you are on should not matter, but it is still useful to explore the limitations directly.)

Required file:

Step 1. Make a copy of FactsAboutReals.java, which is given by


import SimpleOutput;

/**
 * A program that prints simple ``facts'' about real values.
 *
 * @author Samuel A. Rebelsky
 * @author Your Name Here
 * @version 1.0 of September 1998
 */
public class FactsAboutReals {
  /**
   * Print some interesting facts about real numbers in Java.
   */
  public static void main(String[] args) {
    SimpleOutput out = new SimpleOutput();
    out.println("The smallest allowable real (float) value is " +
                Float.MIN_VALUE);
    out.println("The smallest allowable real (double) value is " +
                Double.MIN_VALUE);
  } // main(String[])
} // class FactsAboutReals


Compile and execute the program and record the value of the minimum float and double.


 
 
 

Step 2. Modify the program in step 1 to find the value for the largest float and double. Record what you learn.


 
 
 

Step 3. How do the values from steps 1 and 2 relate?


 
 
 
 
 
 

Step 4. Update your program to compute 1 + the largest value of the particular type. Recompile and execute your program. What do you find? Why might this be?


 
 
 
 
 
 

Experiment X1.5: The limits of reals

Step 1. Add the following lines to FactsAboutReals, but do not recompile or execute the program.

    int i;
    float f;
    f = 1;
    for (i = 0; i < 200; ++i) {
      f = f*100;
      out.println(f);
    }

What do you expect those lines to do?


 
 
 
 
 
 

Step 2. Recompile and execute FactsAboutReals. Record your output. What does this output suggest? Would doing more repetitions help you? If so, try that.


 
 
 
 
 
 

Step 3. Update FactsAboutReals so that it squares f at each step, rather than multiplying by 100. What do your results suggest?


 
 
 
 
 
 

Step 4. Add the following lines to the loop.

      g = f + 1;
      out.println(g);

Recompile and execute FactsAboutReals. Record and explain your results.


 
 
 
 
 
 

Step 5. Update FactsAboutReals to repeatedly divide by 100, rather than multiplying by 100. What do you expect the results to be? What are the results? Explain any differences.


 
 
 
 
 
 

Step 6. Repeat the previous step, dividing by two rather than 100. What do these results suggest?


 
 
 
 
 
 

Experiment X1.6: Casting

Required file:

Step 1. Here are the beginnings of the CastingAbout program, which is used to cast values.


import SimpleOutput;

/**
 * A number of tests of casting in Java.
 *
 * @author Samuel A. Rebelsky
 * @author Your Name Here
 * @version 1.0 of September 1998
 */
public class CastingAbout {
  public static void main(String[] args) {
    SimpleOutput out = new SimpleOutput();
    byte b = 1;
    short s = 2;
    int i = 3;
    long l = 4;
    float f = 1.1;
    double g = 2.3;
  } // main(String[])
} // class CastingAbout


Try to compile the program and record any error messages. What do these error messages suggest?


 
 
 
 
 
 

Step 2. Update CastingAbout so that none of the variables is initialized. Does this correct the problems from step 1? Why or why not?


 
 
 
 
 
 

Step 3. Add the following lines to CastingAbout.

    i = 3;
    l = i;
    out.println("l is " + l);

Recompile and execute the program. Record your results. Are they what you would expect? Why or why not?


 
 
 
 
 
 

Step 4. Add the following lines to CastingAbout.

    l = 3;
    i = l;
    out.println("i is " + i);

Recompile and execute the program. Were you successful? Why or why not?


 
 
 
 
 
 

Step 5. Replace the line that reads

    i = l;

with one that reads

    i = (int) l;

Recompile and execute the program. Were you successful? If so, record and explain the output. If not, explain why not.


 
 
 
 
 
 

Step 6. Instead of assigning 3 to l, assign Long.MAX_VALUE. What happens? Why?


 
 
 
 
 
 

Step 7. Add the following lines to CastingAbout.

    l = 3;
    f = l;
    out.println("f is " + f);

Recompile and execute the program. Were you successful? Why or why not? If you were successful, what do the results suggest?


 
 
 
 
 
 

Step 8. Try assigning a float to an integer, instead of vice versa. Note that you may need an explicit coercion. What happens with fractional float values? Float values larger than Long.MAX_VALUE? Other special cases?


 
 
 
 
 
 

Experiment X1.7: Facts about characters

Required files:

Step 1. Here is the FactsAboutCharacters.java program.


import SimpleOutput;

/**
 * A program that prints out some very simple ``facts'' about
 * characters in Java.
 *
 * @author Samuel A. Rebelsky
 * @author Your name here
 * @version 1.0 of September 1998
 */
public class FactsAboutCharacters {
  /**
   * Print some interesting facts about characters in Java.
   */
  public static void main(String[] args) {
    SimpleOutput out = new SimpleOutput();
    // Build a few characters to consider
    char s1 = 'a';
    char s2 = 'A';
    char s3 = '3';
    char s4 = '.';
    // Print some information on those characters
    out.println("The integer that corresponds to " + s1 + " is " +
                (int) s1);
    out.println("The integer that corresponds to " + s2 + " is " +
                (int) s2);
    out.println("The integer that corresponds to " + s3 + " is " +
                (int) s3);
    out.println("The integer that corresponds to " + s4 + " is " +
                (int) s4);
    // Some alternate information for further experiments.
    out.println("The 100th character is " + (char) 100);
  } // main(String[])
} // class FactsAboutCharacters


Compile and execute the program and record the results.


 
 
 
 
 
 

Step 2. Modify the program so that it finds codes for the characters >, b, B, and the space. Summarize your work and findings below.


 
 
 
 
 
 

Step 3. It is also possible to convert codes back to characters by casting them as characters. Modify the program from Step 1 to find the characters immediately before and after the characters a, 2, and Z in the ASCII order. Summarize your modifications and record your findings below.


 
 
 
 
 
 

Step 4. Given the results of the previous steps, does Java use ASCII encoding? Why or why not?


 
 
 
 
 
 

Experiment X1.8: Precedence

Step 1. Create a new program, PrecedenceFun.java, with a main routine that contains the following lines

    SimpleOutput out = new SimpleOutput();
    out.println(1-2-3);
    out.println(1-(2-3));
    out.println((1-2)-3);

What do you expect the output of this program to be?


 
 
 

Step 2. Compile and execute your program, correcting any compilation errors if you discover any. Record the result. Were they what you expected? What did the results suggest?


 
 
 
 
 
 

Step 3. Add the following lines to your main routine.

    out.println(2*2-2);
    out.println(2-2*2);
    out.println(2*(2-2));
    out.println((2-2)*2);

What do you expect the new output to be?

Step 4. Compile and execute your program, correcting any compilation errors if you discover any. Record the result. Were they what you expected? What did the results suggest?


 
 
 
 
 
 

Step 5. Add the following lines to your program.

    out.println(5/2);
    out.println(5.0/2.0);
    out.println(5.0/2);
    out.println(5/2.0);

Recompile and execute the program. Record the results. Explain the results.


 
 
 
 
 
 

After you are finished answering this question, read the notes on this problem.

Step 5. Add the following lines to your program.

    out.println(3/2*1.0);
    out.println(1.0*3/2);

Recompile and execute the program. Record the results. Explain the results.


 
 
 
 
 
 

After you are finished answering this question, read the notes on this problem.

Experiment X1.9: Concatenating strings and numbers

Required files:

Before you begin, obtain fresh copies of SimpleInput.java, SimpleOutput.java, and BasicComputations.java.

Step 1. After the line that prints the square in BasicComputations.java, add a line that reads

    out.println(val + " + 1 is " + (val+1));

What do you think this will do? Will the output be different from that in the previous step? Compile and execute the program to test your hypothesis. Record any discrepancies and suggest why such discrepancies might occur.


 
 
 
 
 
 

After doing so, you may want to look at the notes on this problem.

Step 2. Remove the parentheses from around (val+1). What, if anything, do you think this will do? Compile and execute the program to test your hypotheses. Record any discrepancies and suggest why such discrepancies might occur.


 
 
 
 
 
 

After doing so, you may want to look at the notes on this problem. Reinsert the parentheses when you are done with this step.

Step 3. Remove the parentheses around val*val in

    out.println(val + " squared is " + (val*val));

What, if anything, do you think this will do? Compile and execute the program to test your hypotheses. Record any discrepancies and suggest why they might have occurred.


 
 
 
 
 
 

After doing so, you may want to look at the notes on this problem.


Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Source text last modified Tue Oct 26 11:48:53 1999.

This page generated on Tue Oct 26 15:36:42 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu