string

advertisement
The string data type String
String (1)
• The String data type is the first data type that we learn that
is not built-in into the programming language
The String data type is a class that is constructed using the char data
type:
1. The information of the String data type is stored using a number
of char typed variables
2. The operations on String data are implemented by a number of methods
(= computer programs)
String (2)
• A string is a sequence of characters enclosed between
the double quotes "...“
• Each character in a string is of the type char and uses
the Unicode as encoding method
String literals (1)
• We write string literals between double quotes "...“
• Escape character = a special character that
allow Java to change the meaning of the next character
• \ (backslash) = the escape character for strings
String literals (2)
• Escape sequence = the escape character \ followed by one
character
• The character denoted by an escape sequence is usually one
that you cannot type in with the keyboard
Defining String typed variables (1)
• Syntax to define a String typed variable:
• The class String announces the variable definition clause
• The NameOfVariable is an identifier which is the name of the variable.
• The variable definition clause is must be ended with a semi-colon ";"
• A String typed variable can store the location
(address) of a sequence characters (string)
Defining String typed variables (2)
How strings are stored inside the
computer (1)
• Challenge: Strings have variable lengths,
• So you cannot store a string in a fixed sized variable.
• There are 2 techniques in Computer Science for
storing strings:
1. The location + length method
2. The location + sentinel method
How strings are stored inside the
computer (2)
1. The location + length method:
• The characters of string is stored (using Unicode encoding)
somewhere consecutively inside the RAM memory
• A string variable contains:
the location of the first character of the string
the length of the string (total number of characters in the string)
How strings are stored inside the
computer (3)
How strings are stored inside the
computer (4)
• 2. The location + sentinel method:
• Sentinel = a special character that denotes the end of a string.
(usually the NULL character with Unicode value 0.)
• The sentinel is added after the last character of the string
• A string variable contains:
the location of the first character of the string
How strings are stored inside the
computer (5)
How strings are stored inside the
computer (6)
• The location + length method is much more efficient
• Java uses the location + length method to store String typed
data
• Obtaining the length information of strings in Java:
Let a be a String typed variable.
The expression a.length() returns the length of the
string stored in the string variable a.
How strings are stored inside the
computer (7)
numbers and numeric strings
• Numeric string uses the Unicode encoding method !
• They use different representation (encoding) techniques)
Converting integer and floating
point values to strings (2)
• int <=> String:
• double <=> String:
• Float <=> String:
Converting integer and floating
point values to strings (3)
Operators on the String data type
(1)
• Only one operator (the + operator) defined for the String data
type:
string1 + string2 = concatenate the strings string1 and string2
The + operator on number
typed and String typed data (1)
• Java provides an automatic conversion feature when
the + operator is used between:
a number typed data and a string typed data
• The number is automatically converted to a string
• The + operator is then applied on 2 strings (i.e., the +
operator is a concatenation !)
The + operator on number
typed and String typed data (2)
Reading a string from the keyboard
Other operations on Strings
1. charAt(i): return the character at position i in the string.
2.
substring(i, j): return the substring consisting of the
characters between positions i and j-1 in the string.
Download