20-ToStringEquals

20-ToStringEquals - Chapter XX Redefining toString and...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter XX Redefining toString and equals Chapter XX Topics 20.1 Introduction 20.2 Redefining the toString Method 20.3 Redefining the equals Method 20.4 Summary Chapter XX Redefining toString and equals 1067 20.1 Introduction There are some mysteries in the Java programming language. For example, how is it that it is possible to use the print method with an ArrayList object and a String object and the array members are all printed or the String is printed. On the other hand, the same approach with a static array does not work. It requires a loop to display every member of a static array. There is a similar problem when you create your own class. You cannot simply use print to display the values stored in an object of your class. Or can you? This is a short, but vitally important, chapter. The mystery of output display will be explained and you will learn how to create your own class in such a manner that any desired values will be displayed using print without benefit of any special loop structure. There seems a similar mystery in the areas of equality checking. Some variables, like simple data types can be checked for equality with the equality operator and others, like String objects require the use of the equals method. The question once again about user-defined classes pops up. Can user-defined objects be tested for equality, and if so how is this done? The title of this chapter is strange, because usually there is some general computer science concept presented in a chapter. A chapter tilte is more commonly found that is called Recursion or Algorithms . Redefining toString and Equals seems to be a very odd title. This is not a title that will make any sense in the introduction. When you are finished with the chapter the title will make perfect sense. Students somewhat underestimate short chapters. The small quantity of pages in the chapter and the short list of topics in the chapter provides comfort and an indication that this chapter will be handled in short order with little effort. Do not take that attitude. This chapter is a significant topic that must be clearly understand for further study in computer science and it must be understood for success on the AP Computer Science Examination. 1068 Exposure Java 2009, APCS Edition 08-08-09 20.2 Redefining the toString Method You have taken the humble print and println methods for granted for many months. It is such an easy set of methods to use. Everything inside the parentheses is displayed in a nice concatenated fashion. Anything placed between double quotes is displayed literally and variables are displayed according to the value they hold. At least that seems to be the case. You may have been surprised by the output of an ArrayList object that was rather casually shoved inside the println parentheses. Every one of the object members was neatly, and individually displayed, complete with comma separation. You were promised that clarification would come in some later chapter. Here you are in the later...
View Full Document

This note was uploaded on 10/14/2010 for the course APSC AP taught by Professor Kurt during the Spring '98 term at Wooster.

Page1 / 26

20-ToStringEquals - Chapter XX Redefining toString and...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online