Characters and Strings - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Characters and Strings

Description:

In Java, single characters are represented using the primitive data type char. ... Immutability. When a String object is created, it cannot be changed. ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 28
Provided by: amyh99
Learn more at: http://cs.boisestate.edu
Category:

less

Transcript and Presenter's Notes

Title: Characters and Strings


1
Chapter 9
  • Characters and Strings

2
Topics
  • Character primitives
  • Character Wrapper class
  • More String Methods
  • String Comparison
  • String Buffer
  • String Tokenizer

3
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 'X'

4
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

5
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-255)
  • 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.

6
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).

7
Character Class
  • Like the other primitive types, there is a
    wrapper class 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

8
Static CharacterMethods
  • boolean methods
  • isDigit( char)
  • isLetter( char)
  • isSpace( char)
  • isUpperCase( char)
  • isLowerCase(char)
  • other methods
  • char toLowerCase( char)
  • toUpperCase( char)

9
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.
  • We access individual characters of a string by
    calling the charAt method of the String object.

10
Strings
  • Each character in a string has an index that we
    can use to access the character.
  • We know for learning about arrays that Java uses
    zero-based indexing
  • the first characters 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)

11
String Indexing
  • An indexed expression is used to refer to
    individual characters in a string.

12
Strings
  • Since String is a class, we can create an
    instance of a class by using the new method.
  • The statements we have used so far, such as
  • String name1 Kona
  • works as a shorthand for
  • String name1 new String(Kona)
  • But this shorthand works for the String class
    only.

13
Other String Methods
  • String toLowerCase()
  • String toUpperCase()
  • String replace( char oldChar,
  • char newChar)
  • and many more

14
String Comparison
  • String comparison may be done in several ways.
  • The methods equals and equalsIgnoreCase compare
    string values one is case-sensitive and one is
    not.
  • The method compareTo returns an int value
  • Zero (0) if the strings are equal.
  • A negative integer if the first string is less
    than the second.
  • A positive integer if the first string is greater
    than the second.

15
String Equality
  • Comparing String objects is similar to comparing
    other objects.
  • The equality test () is true if the contents of
    the variables are the same.
  • For a reference data type, the equality test is
    true if both variables refer to the same object,
    because they both contain the same address.
  • The equals method is true if the String objects
    to which the two variables refer contain the same
    string value.
  • the same sequence of characters

16
Equality Testing
  • The difference between the equality test and the
    equals method.

17
Equality Testing
18
Equality Testing for Strings
  • As long as a new String object is created using
    the new operator, the rule for comparing objects
    applies to comparing strings.
  • String str new String (Java)
  • If the new operator is not used, a String
    reference refers to the literal string constant.
  • String str Java
  • There will be only one copy of any literal string

19
Memory Diagram
  • The difference between using and not using the
    new operator for String.

20
Immutability
  • When a String object is created, it cannot be
    changed.
  • Look at the documentation - there are no mutator
    methods
  • We say Strings are immutable.
  • Manipulating the content of a String requires the
    creation of a new String object.
  • If you are doing a lot of manipulation this is
    not very efficient.

21
StringBuffer Class
  • Manipulating the content of a string, such as
    replacing a character, appending a string with
    another string, deleting a portion of a string,
    and so on, may be accomplished more efficiently
    using the StringBuffer class.

22
StringBuffer Example
  • StringBuffer word new StringBuffer(Java)
  • word.setCharAt(0, D)
  • word.setCharAt(1, i )
  • changes the string from Java to Diva.

23
Ch9ReplaceVowelsWithX.java
  • This example reads a sentence and replaces all
    vowels in the sentence with the character X.
  • tempStringBuffer
  • new StringBuffer(inSentence)
  • numberOfCharacters tempStringBuffer.length()
  • for (int index 0 index lt numberOfCharacters
    index)
  • if ( isVowel( letter)
  • tempStringBuffer.setCharAt(index,'X')

24
StringBuffer Operations
  • We cannot input StringBuffer objects.
  • We must input String objects and convert them to
    StringBuffer objects.
  • We use the append method to append a String or
    StringBuffer object to the end of a StringBuffer
    object.
  • The method append can also take an argument of
    the primitive data type.
  • Any primitive data type argument is converted to
    a string before it is appended to a StringBuffer
    object.

25
StringBuffer Insertion
  • We can insert a string at a specified position by
    using the insert method.
  • ltStringBuffergt.insert(ltinsertIndexgt, ltvaluegt)
  • where ltinsertIndexgt must be greater than or
    equal to 0 and less than or equal to the length
    of ltStringBuffergt, and the ltvaluegt can be an
    object or a primitive.
  • For example, executing
  • StringBuffer str
  • new StringBuffer(Java is great)
  • str.insert(8, really)
  • changes the string "Java is great" to "Java is
    really great".

26
String Tokenizer
  • A StringTokenizer object can be used to break a
    string up into smaller substrings (tokens)
  • By default, the tokenizer separates the string at
    white space
  • The default StringTokenizer constructor takes the
    original string to be separated as a parameter
  • StringTokenizer st
  • new StringTokenizer( text)
  • An alternate constructor allows you to specify
    different separators (delimiters).
  • StringTokenizer st
  • new StringTokenizer( text, delims)

27
String Tokenizer Methods
  • The hasMoreTokens method returns true if there
    are more tokens in the string.
  • Each call to the nextToken method returns the
    next token in the string.
  • If you need to know how many tokens there are,
    you can call countTokens
Write a Comment
User Comments (0)
About PowerShow.com