Title: Characters and Strings
1Lecture 3.2
2Objectives
- Declare and manipulate data of the char data
type. - Write string processing programs using String and
StringBuffer objects. - Differentiate the String and StringBuffer classes
and use the correct class in solving a given
task. - Distinguish the primitive and reference data
types and show how the memory allocation between
the two is different. - Tell the difference between equality and
equivalence testings for String objects. - Show, by using the state-of-memory diagrams, how
objects are passed to methods and returned from
methods.
3Characters
- In Java single characters are represented using
the data type char. Character constants are
written as symbols enclosed in single quotes, for
example, 'a', 'X', and '5'. - To represent characters in computer, U. S.
computer manufacturers devised several coding
schemes. - One coding scheme widely used today is ASCII
(American Standard Code for Information
Interchange). - To accommodate the character symbols of
non-English languages, the Unicode Consortium
established the Unicode Worldwide Character
Standard, commonly known as Unicode.
4ASCII Table
5Character Processing
6Question-1
- Determine the output of the following statements
- System.out.println ( (char) 65 )
- System.out.println ( (int) C )
- System,out.println ( Y )
- If ( A lt ? )
- System.out.println ( A )
- else
- System.out.println ( ? )
7Strings
- A string is a sequence of characters that is
treated as a single value. - The String data type is used to represent strings
in Java. - We have been using String objects all along. For
example, to display a text with messageBox, we
write
8String is an Object
- String is a class in the java.lang package.
- Because String is a class, we need to create an
instance of String in Java for string processing.
Like any other objects, we need a declaration and
object creation for the instances of the String
class. For example,
But we normally use a shorthand, instead,
treating String objects much like primitive data.
For example,
9Accessing Individual Elements
- Individual characters in a String accessed with
the charAt method.
10Determining the Size
- We determine the number of characters in a String
with the length method.
Error because no object is created for str3, so
it is a null.
11Example Counting Vowels
Heres the code to count the number of vowels in
the input string.
Alternatively could have used the toUpperCase
method of class String
12Example Counting Words
Problem Inner loops could cause index to become
equal to numberOfCharacters, which is an error.
13Example Counting Words - 2
Problem wordCount will be one more than the
actual count if the sentence ends with one or
more spaces.
14Example Counting Java
Continue reading words and count how many times
the word Java occurs in the input, ignoring the
case.
15Other Useful String Operators
- See the String class documentation for details.
16Questions-2
- Assume
- String str1 programming
- String str2 language
- Determine the value of each of the following is
they are valid - str1.compareTo (str2)
- str2.compareTo (str2)
- str2.substring (1,1)
- str2.substring (0,7)
- str2.charAt (11)
- str1.length ( ) str2.length
17Answers-2
- Positive number
- 0
- // Empty string
- languag characters from location 0 to 7-16
- Invalid out of pounds error
- 11 8 19
18Primitive versus Reference Types
- Data types are classified into two groups
primitive and reference. - Non-numerical data types char and boolean and all
of the numerical data types are primitive. - All of the objects you have learned so far are
reference data types.
19Effect of Assignment on Primitives
Code
State of Memory
20Memory Allocation for Reference Data Type
Code
State of Memory
21Effect of Assignment on References - 1
Code
Both word1 and word2 are allocated memory (to
store references), but the objects themselves are
not yet created, so they both contain null.
State of Memory
Null References
22Effect of Assignment on References - 2
Code
One String object is created and assigned to
word1, so word1 contains the address of this
object.
State of Memory
23Effect of Assignment on References - 3
Code
Content of word1, which is an address, is
assigned to word2, making word2 refer to the same
object.
State of Memory
24Equality () vs. equalsCase 1
word1 and word2 point to the same object.
word1 word2
word1.equals( word2 )
25Equality () vs. equalsCase 2
word1
word2
word1 and word2 point to different objects having
the same string.
word1 word2
word1.equals( word2 )
26Equality () vs. equalsCase 3
word1
word2
word1 and word2 point to different objects with
different strings.
word1 word2
word1.equals( word2 )
27StringBuffer
- A String object is immutable, which means that
once a String object is created, we cannot change
it. - We can read individual characters in a string,
but we cannot add, delete, or modify characters
of a String object. - Remember that the methods of the String class,
such as toUpperCase and substring, do not modify
the original string they return a new string. - Java adopts this immutability restriction to
implement an efficient memory allocation scheme
for managing String objects. - Creating a new string from the old one will work
for most cases, but sometimes manipulating the
content of a string directly is more convenient. - Manipulation here means operations such as
replacing a character, appending a string with
another string, deleting a portion of a string,
and so forth.
28Sample StringBuffer Processing - 1
- Replace all vowels in the sentence with X.
29Sample StringBuffer Processing - 2
- Creates a sentence with words having even number
of letters. Stop when the input word is STOP.
30Question-3
- Determine the value of str after the following
statements have are executed - String str Caffeine
- StringBuffer str1 new StringBuffer
(str.substring (4,8)) - str1.insert (3, f)
- str De str1
- Find the errors in the following code
-
- String str Caffeine
- StringBuffer str1 str.substring (1,3)
- str1.append (e)
- System.out.print (str1)
- str1 str1 str
-
31Answer-3
- Deeinfe
- Cannot assign a String value to a StringBuffer
variable.
32Passing Objects as Parameters
- Example What is the output of this code ?
- class Tester
-
- public void myMethod (StringBuffer strBuf)
-
- strBuf.setCharAt (0, Y)
-
-
- Tester tester
- StringBuffer word new StringBuffer (Java)
-
- tester new Tester ( )
- tester.myMethod (word)
- outoutBox.printLine ( Word is word )
33Passing Objects to Methods - 1
Code
public void myMethod( StringBuffer strBuf
) strBuf.setCharAt( 0, Y )
//StringBuffer word is //created
here tester.myMethod( word )
A. Local variables do not exist before the
method execution
State of Memory
34Passing Objects to Methods - 2
Code
public void myMethod( StringBuffer strBuf
) strBuf.setCharAt( 0, Y )
//StringBuffer word is //created
here tester.myMethod( word )
B. The value of the argument, which is an
address, is copied to the parameter.
State of Memory
35Passing Objects to Methods - 3
Code
public void myMethod( StringBuffer strBuf
) strBuf.setCharAt( 0, Y )
//StringBuffer word is //created
here tester.myMethod( word )
C. The content of the object referenced by
strBuf is changed.
State of Memory
36Passing Objects to Methods - 4
Code
public void myMethod( StringBuffer strBuf
) strBuf.setCharAt( 0, Y )
//StringBuffer word is //created
here tester.myMethod( word )
D. The parameter is erased. The argument still
points to the same (now modified) object.
State of Memory
37Question-4
- Determine the output of the following code
- String str Internet
- Tester tester new Tester ( )
- System.out.println (str is tester.toUpper
(str))
38Returning an Object from Methods
- Passing an object as an argument to a method
means passing the address of the object to the
method. - The previous four slides illustrate the effect of
passing an object to a method. - The same rule applies when we return an object
from a method. It means the address of the
object is passed back from the method.