Presentation is loading. Please wait.

Presentation is loading. Please wait.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class.

Similar presentations


Presentation on theme: "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class."— Presentation transcript:

1 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Character Data * In Java, single characters are represented using the primitive data type char. * Character constants are written as symbols enclosed in single quotes: char ch1; ch1 = 'X';

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Character Encoding * Characters are stored in memory as integer values * Each character has a unique "code" that is used to represent it. * Having a standard encoding allows different computers to share information easily. * Several encoding schemes have been used ASCII EBCDIC (not used any more) Unicode

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ASCII Codes * ASCII stands for American Standard Code for Information Interchange. * ASCII is one of the document coding schemes widely used today. each character represented by 8 bits ( values 0-127) * ASCII works well for English-language documents because all characters and punctuation marks are included in the ASCII codes. * ASCII does not represent the full character sets of other languages.

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Unicode * The Unicode Worldwide Character Standard (Unicode) supports the interchange, processing, and display of the written texts of diverse languages. * Java uses the Unicode standard for representing char constants. * Each character is represented with 2 bytes (16 bits).

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Character Arithmetic * You can compare char values with the relational operators ( = >) if ('0'<= ch && ch <= '9') System.out.println("digit); * Since characters are stored as integers, you can do "arithmetic" on them. char ch = 5; int digit = ch - '0'; char letter = 'g'; int position = letter - 'a'; Useful for ciphers for example.

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Using switch with a char * Variables of type char can be used for the control expression in a switch statement char ch; … switch (ch) { case 'a': /*code for a */ break; case 'b': /*code for b */ break; case 'c': /*code for c */ break; default: /*default code*/ }

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Console menu do { printMenu(); input = instream.next( ); switch (input.charAt(0)) { case 'a': methodA(); break; … case 'q': System.exit(0); break; } } while (true);

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Character Class * Like the other primitive types, there is a wrapper class (in java.lang) for the char type. * Useful Methods charValue( ) returns a char that is the value stored in the Character getNumericValue( ) returns the Unicode value for the char

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Static CharacterMethods * boolean methods isDigit( char) isLetter( char) isSpace( char) isUpperCase( char) isLowerCase(char) * other methods char toLowerCase( char) char toUpperCase( char)

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Strings * A string is a sequence of characters that is treated as a single value. * Instances of the String class are used to represent strings in Java.

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Strings * We access individual characters of a string by calling the charAt method of the String object. * Each character in a string has an index that we can use to access the character. the first character’s index is 0, the second is 1, and so on. * To refer to the first character of the word name, we say name.charAt(0)

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. String Indexing * An indexed expression is used to refer to individual characters in a string.

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. String Methods that use char * A char can be added to a String String plural = word + 's'; *String valueOf( char ch) *String replace( char oldChar, char newChar) *int indexOf( char ch) *int indexOf( char ch, int fromIndex) *int lastIndexOf( char ch) *int lastIndexOf( char ch, int fromIndex)


Download ppt "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class."

Similar presentations


Ads by Google