{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CH08-StringsAndCharacters

CH08-StringsAndCharacters - TheArtandScienceof CHAPTER 8...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Strings and Characters C H A P T E R 8 Surely you don’t think that numbers are as important as words. —King Azaz to the Mathemagician Norton Juster, The Phantom Toolbooth, 1961 8.1 The principle of enumeration 8.2 Characters 8.3 Strings as an abstract idea 8.4 Using methods in the String class 8.5 A case study in string processing
Image of page 1

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

View Full Document Right Arrow Icon
The Principle of Enumeration Computers tend to be good at working with numeric data. When you declare a variable of type int , for example, the Java virtual machine reserves a location in memory designed to hold an integer in the defined range. The ability to represent an integer value, however, also makes it easy to work with other data types as long as it is possible to represent those types using integers. For types consisting of a finite set of values, the easiest approach is simply to number the elements of the collection. For example, if you want to work with data representing months of the year, you can simply assign integer codes to the names of each month, much as we do ourselves. Thus, January is month 1, February is month 2, and so on. Types that are identified by counting off the elements are called enumerated types .
Image of page 2
Enumerated Types in Java Java offers two strategies for representing enumerated types: Defining named constants to represent the values in the enumeration Using the enum facility introduced in Java 5.0 Prior to the release of Java 5.0 in 2005, Java did not include any direct support for enumerated types. Up until then, Java programmers achieved the effect of enumerated types by defining integer constants to represent the elements of the type and then using variables of type int to store the values. For example, Java programmers might define names for the four major compass points as follows: public static final int NORTH = 0; public static final int EAST = 1; public static final int SOUTH = 2; public static final int WEST = 3; Once that definition was in place, those programmers could then store a direction constant in any integer variable.
Image of page 3

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

View Full Document Right Arrow Icon
The enum Facility Recent versions of Java support a seemingly simpler and more expressive strategy that makes it possible to define an enumeration as a distinct type. In its simplest form, the pattern for an enumerated type definition is If you use the enum facility, you can define a new type for the four compass points like this: public enum Direction { NORTH, EAST, SOUTH, WEST } You can then declare a variable of type Direction and use it in conjunction with the constants defined by the class. public enum name { list of element names }
Image of page 4
Tradeoffs in the enum Facility The enum facility has several advantages over using integers: The compiler chooses the integer codes automatically.
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern