Download Java Data Storage: Understanding Integer and Character Types and more Lab Reports Computer Science in PDF only on Docsity! 1 Part 3: Data Storage and Its Implications In this exercise you will: 1. Investigate the storage systems used for three primitive data types, int, byte, and character. 2. Learn how these storage systems are reflected in the execution of Java programs. 2 Data of Type Integer Recall that data items of type integer are normally stored in memory by using two’s complement notation and that this imposes a limit on the size of the values that can be represented. For example, with a two’s complement system using only four bits for storage, the largest value that can be represented is seven. Of course, if this was the largest integer your machine could represent, it would be of little use. Therefore, computer systems use more than four bits for integer storage and can store rather large values. But, limitations still exist. In the Java system, the maximum and minimum values of the numeric types are represented by the constants MAX_VALUE and MIN_VALUE. The smallest and largest int values are defined in the Integer class. These class constants can be accessed by using the name of the class in which the constant is defined, followed by the dot operator ( . ) and the name of the constant. Therefore, to access the largest and smallest int values, use Integer.MAX_VALUE and Integer.MIN_VALUE. Experiment 2.6 In this experiment you will investigate the storage of integer values in Java. Step 1. Compile and execute the program J02E06.java, and record the values of the smallest and largest int values. class J02E06 { public static void main(String[] args) { System.out.println( "In Java the smallest int value is " + Integer.MIN_VALUE); System.out.println( "In Java the largest int value is " + Integer.MAX_VALUE); } } _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Step 2. Besides int, Java has three other integer types, long, short and byte. In this step, let’s investigate the maximum and minimum values of the primitive data type byte. Information about and methods involving type byte are stored in a class called Byte. Both the Integer and Byte classes are in the API package java.lang, which is automatically imported into every Java program. Modify the program to print the smallest and largest byte values. Record the results. 5 _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Step 2. Modify the program in Step 1 to find the Unicode representations for the characters ?, 0 and space. _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Experiment 2.8 Step 1. Compile the program J02E08.java. Record and explain the results. class J02E08 { public static void main(String[] args) { int x; double d; x = 5; d = x; System.out.println("int " + x + " double " + d); d = 2.95; x = d; System.out.println("int " + x + " double " + d); } } _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ Step 2. Correct the program in Step 1 by replacing the statement x = d; 6 with x = (int)d; Record the results. _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________