Java Reference
In-Depth Information
You can also use cast operators to explicitly treat char data values as int data values, and
int data values as char data values. To treat char data values as int data values, you use
a collating sequence. For example, in the Unicode character set, ( int )('A') is 65 and
( int )('8') is 56 . Similarly, ( char )(65) is 'A' and ( char )(56) is '8' .
2
class String
In the preceding sections, we discussed primitive types to deal with data consisting of
numbers and characters. What about data values such as a person's name? A person's
name contains more than one character. Such values are called strings. More formally, a
string is a sequence of zero or more characters. Strings in Java are enclosed in double
quotation marks (not in single quotation marks, as are the char data types).
Most often, we process strings as a single unit. To process strings effectively, Java provides the
class String .The class String contains various operations to manipulate a string. You
will see this class used throughout the topic. Chapter 3 discusses various operations provided by
the class String . Moreover, technically speaking, the class String is not a primitive type.
A string that contains no characters is called a null or empty string. The following are
examples of strings. Note that "" is the empty string.
"William Jacob"
"Mickey"
""
A string, such as "hello" , is sometimes called a character string or string literal or
string constant. However, if no confusion arises, we refer to characters between double
quotation marks simply as strings.
Every character in a string has a specific position in the string. The position of the first
character is 0, the position of the second character is 1, and so on. The length of a string
is the number of characters in it.
EXAMPLE 2-9
String
"Sunny Day"
Character in the string
'S' 'u' 'n' 'n' 'y' ' ' 'D' 'a' 'y'
Position of the character
in the string
0
1
2
3
4
5
6
7
8
The length of the string "Sunny Day" is 9.
When determining the length of a string, you must also count any spaces contained in the
string. For example, the length of the string "It is a beautiful day." is 22.
 
Search WWH ::




Custom Search