Java Reference
In-Depth Information
Please enter a radix:
16
Please choose one:
1 -- Convert digit to character
2 -- Convert character to digit
2
Enter a character:
A
Convert character to digit: 10
Please enter a radix:
16
Please choose one:
1 -- Convert digit to character
2 -- Convert character to digit
1
Enter a digit:
13
Convert digit to character: d
Fig. 14.16
|
Character
class
static
conversion methods. (Part 2 of 2.)
Line 28 uses method
forDigit
to convert the integer
digit
into a character in the
number system specified by the integer
radix
(the base of the number). For example, the
decimal integer
13
in base 16 (the
radix
) has the character value
'd'
. Lowercase and upper-
case letters represent the
same
value in number systems. Line 35 uses method
digit
to con-
vert variable
character
into an integer in the number system specified by the integer
radix
(the base of the number). For example, the character
'A'
is the base 16 (the
radix
) repre-
sentation of the base 10 value 10. The radix must be between 2 and 36, inclusive.
Figure 14.17 demonstrates the constructor and several instance methods of class
Character
—
charValue
,
toString
and
equals
. Lines 7-8 instantiate two
Character
objects by assigning the character constants
'A'
and
'a'
, respectively, to the
Character
variables. Java automatically converts these
char
literals into
Character
objects—a process
known as
autoboxing
that we discuss in more detail in Section 16.4. Line 11 uses
Char-
acter
method
charValue
to return the
char
value stored in
Character
object
c1
. Line 11
returns a string representation of
Character
object
c2
using method
toString
. The con-
dition in line 13 uses method
equals
to determine whether the object
c1
has the same con-
tents as the object
c2
(i.e., the characters inside each object are equal).
1
// Fig. 14.17: OtherCharMethods.java
2
// Character class instance methods.
3
public class
OtherCharMethods
4
{
5
public static void
main(String[] args)
6
{
7
Character c1 =
'A'
;
Character c2 =
'a'
;
8
Fig. 14.17
|
Character
class instance methods. (Part 1 of 2.)