CH08-StringsAndCharacters

CH08-StringsAndCharacters - The Art and Science of An...

Info iconThis preview shows pages 1–6. 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

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: 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 dont 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 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 . 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. 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 } Tradeoffs in the enum Facility The enum facility has several advantages over using integers: The compiler chooses the integer codes automatically....
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 33

CH08-StringsAndCharacters - The Art and Science of An...

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

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