Converting Primitive Types to Objects and Vice Versa Comparing Objects

J.E.D.I

9.5.3 Converting Primitive Types to Objects and Vice Versa

One thing you cant do under any circumstance is cast from an object to a primitive data type, or vice versa. Primitive types and objects are very different things in Java, and you cant automatically cast between the two or use them interchangeably. As an alternative, the java.lang package includes classes that correspond to each primitive data type: Float, Boolean, Byte, and so on. Most of these classes have the same names as the data types, except that the class names begin with a capital letter Short instead of short, Double instead of double, and the like. Also, two classes have names that differ from the corresponding data type: Character is used for char variables and Integer for int variables. Called Wrapper Classes Java treats the data types and their class versions very differently, and a program wont compile successfully if you use one when the other is expected. Using the classes that correspond to each primitive type, you can create an object that holds the same value. Examples: The following statement creates an instance of the Integer class with the integer value 7801 primitive - Object Integer dataCount = new Integer7801; The following statement converts an Integer object to its primitive data type int. The result is an int with value 7801 int newCount = dataCount.intValue; A common translation you need in programs is converting a String to a numeric type, such as an int Object-primitive String pennsylvania = 65000; int penn = Integer.parseIntpennsylvania; • CAUTION: The Void class represents nothing in Java, so theres no reason it would be used when translating between primitive values and objects. Its a placeholder for the void keyword, which is used in method definitions to indicate that the method does not return a value. Introduction to Programming I 144 J.E.D.I

9.5.4 Comparing Objects

In our previous discussions, we learned about operators for comparing values—equal, not equal, less than, and so on. Most of these operators work only on primitive types, not on objects. If you try to use other values as operands, the Java compiler produces errors. The exceptions to this rule are the operators for equality: == equal and = not equal. When applied to objects, these operators dont do what you might first expect. Instead of checking whether one object has the same value as the other object, they determine whether both sides of the operator refer to the same object. To compare instances of a class and have meaningful results, you must implement special methods in your class and call those methods. A good example of this is the String class. It is possible to have two different String objects that contain the same values. If you were to employ the == operator to compare these objects, however, they would be considered unequal. Although their contents match, they are not the same object. To see whether two String objects have matching values, a method of the class called equals is used. The method tests each character in the string and returns true if the two strings have the same values. The following code illustrates this, class EqualsTest { public static void mainString[] arguments { String str1, str2; str1 = Free the bound periodicals.; str2 = str1; System.out.printlnString1: + str1; System.out.printlnString2: + str2; System.out.printlnSame object? + str1 == str2; str2 = new Stringstr1; System.out.printlnString1: + str1; System.out.printlnString2: + str2; System.out.printlnSame object? + str1 == str2; System.out.printlnSame value? + str1.equalsstr2; } } This programs output is as follows, OUTPUT: String1: Free the bound periodicals. String2: Free the bound periodicals. Same object? true String1: Free the bound periodicals. String2: Free the bound periodicals. Same object? false Same value? True Introduction to Programming I 145 J.E.D.I Now lets discuss the code. String str1, str2; str1 = Free the bound periodicals.; The first part of this program declares two variables str1 and str2, assigns the literal Free the bound periodicals. to str1, and then assigns that value to str2. As you learned earlier, str1 and str2 now point to the same object, and the equality test proves that. str2 = new Stringstr1; In the second part of this program, you create a new String object with the same value as str1 and assign str2 to that new String object. Now you have two different string objects in str1 and str2, both with the same value. Testing them to see whether theyre the same object by using the == operator returns the expected answer: false—they are not the same object in memory. Testing them using the equals method also returns the expected answer: true—they have the same values. • NOTE: Why cant you just use another literal when you change str2, rather than using new? String literals are optimized in Java; if you create a string using a literal and then use another literal with the same characters, Java knows enough to give you the first String object back. Both strings are the same objects; you have to go out of your way to create two separate objects. Introduction to Programming I 146 Figure 9.5: Both references point to the same object Figure 9.6: References now point to different objects J.E.D.I

9.5.5 Determining the Class of an Object