Chapter 8 Characters and s OJECTIVES After you have read and studied this chapter, you should be able to Declare and manipulate data of the char data type. Write string processing programs using and uffer objects. Differentiate the and uffer classes and use the correct class in solving a given task. Distinguish the primitive and reference data types and show how the memory allocation between the two is different. Tell the difference between equality and equivalence testings for objects. Show, by using the state-of-memory diagrams, how objects are passed to methods and returned from methods. 1 of 12
TALE 8.1 ASCII Codes. 0 1 2 3 4 5 6 7 8 9 0 nul soh stx etx eot enq ack bel bs ht 10 lf vt ff cr so si dle dc1 dc2 dc3 20 cd4 nak syn etb can em sub esc fs gs 30 rs us sp! " # $ % & ' 40 ( ) * +, -. / 0 1 50 2 3 4 5 6 7 8 9 : ; 60 < = >? @ A C D E 70 F G H I J K L M N O 80 P Q R S T U V W X Y 90 Z [ \ ] ^ _ ` a b c 100 d e f g h i j k l m 110 n o p q r s t u v w 120 x y z { } ~ del FIGURE 8.1 An indexed expression is used to refer to individual characters in a string. = "Sumatra"; 0 1 2 3 4 5 6 S u m a t r a The variable refers to the whole string..charat(3) The method returns the character at position #3. 2 of 12
FIGURE 8.2 The taxonomy of data types. Data Type Primitive Reference byte short int long float double boolean char MainWindow Graphics Applet Messageox Outputox etc. FIGURE 8.3 Effect of assigning an integer value to a variable. num1 14 3 of 12
FIGURE 8.4 Effect of assigning values to integer variables. int num1, num2; after A is executed A num1 = 14; num2 = num1; num1 14 num1 += 5; num2 14 int num1, num2; after is executed num1 = 14; num2 = num1; num1 19 num1 += 5; num2 14 4 of 12
FIGURE 8.5 Effect of assigning a value to a variable. str is a variable of type, so its content is an address. str 1020 2036 This value 2036 is the address where the string data are actually stored. These are addresses of memory locations. 2036 J a 2040 k a 2044 r t We are assuming 4 bytes to a row. Each character requires 2 bytes, so each row can hold up to two characters. 2048 a FIGURE 8.6 Hypothetical memory management scheme for storing a value. str 1020 J k r a a a t 5 of 12
FIGURE 8.7 Memory shortage problem with the hypothetical memory management scheme. str 1020 I r 1024 i a 1028 n <space> This memory location already contains a value, so we don t have enough space to store the last three characters. 1032 J 1036 134590 1040 FIGURE 8.8 a y a A preferred style of diagram for representing memory allocation for a reference data type. str Jakarta str Jakarta Object Diagram Representation 6 of 12
FIGURE 8.9 Effect of assignment statements on reference data type. oth 1 and 2 are allocated memory, but no actual objects are created yet, so both are null. after A is executed A 1, 2; 1 = new (""); 2 = 1; 1 2 oth 1 and 2 are allocated memory, but no actual objects are created yet, so both are null. One object is created and assigned to 1; that is, 1 points to the object. after is executed 1, 2; 1 = new (""); 2 = 1; 1 2 Content of 1 (which is a reference to the object) is assigned to 2, making 2 also point to the same object. after C is executed C 1, 2; 1 = new (""); 2 = 1; 1 2 7 of 12
FIGURE 8.10 The difference between the equality test and the equals method. Case A: Referring to the same object. 1 2 1 == 2 is true 1.equals( 2 ) is true Note: If x == y is true, then x.equals(y) is also true. The reverse is not always true. Case : Referring to different objects having identical string values. 1 2 1 == 2 is false 1.equals( 2 ) is true Case C: Referring to different objects having different string values. 1 2 ali 1 == 2 is false 1.equals( 2 ) is false 8 of 12
FIGURE 8.11 How the memory space for parameters are allocated and deallocated. Execution Flow public void mymethod( uffer A struf ) { tester.mymethod( ); struf.setcharat(0, 'Y'); C D } at A before mymethod values are copied at struf The local variable does not exist before the method execution. The value of the argument, which is an address, is copied to the parameter. at D after mymethod after C is executed Yava Yava struf The parameter is erased. The argument points to the same (now modified) object. The content of the object referenced by the parameter is modified. 9 of 12
FIGURE 8.12 How a local copy of the passed object is created and manipulated. The original object will not change. Execution Flow public void mymethod( uffer A struf ) { tester.mymethod( ); uffer localcopy C = new uffer( struf.to() ); E localcopy.setcharat(0, 'Y'); } D values are copied at at A before mymethod struf after C is executed at E after mymethod struf local- Copy after D is executed A local copy is created and manipulated. Original object will not be affected. struf Yava local- Copy 10 of 12
FIGURE 8.13 How the reference data type is returned from the sayhi method. Execution Flow himsg = tester.sayhi( ); A public sayhi( ) { greeting; D greeting = "Hi, " + ; return greeting; } C at A before sayhi the address is copied at ill ill (sayhi) himsg himsg at D after sayhi after C is executed ill ill (sayhi) himsg Hi, ill himsg Hi, ill greeting The content of greeting is returned and assigned to himsg. 11 of 12
FIGURE 8.14 The object diagram for the EggyPeggy program. Note: and uffer objects are not shown here. EggyPeggyMain eggypeggy EggyPeggy main start mainwindow MainWindow responseox Responseox outputox Outputox inputox Inputox 12 of 12