Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java's character and assorted string classes support text-processing

Explore Character, String, StringBuffer, and StringTokenizer

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 10

Character c1 = new Character ('A');
Character c2 = new Character ('B');
Character c3 = new Character ('A');
System.out.println ("c1.compareTo (c2): " + c1.compareTo (c2));
System.out.println ("c1.equals (c2): " + c1.equals (c2));
System.out.println ("c1.equals (c3): " + c1.equals (c3));


System.out.println ("c1.compareTo (c2): " + c1.compareTo (c2)); outputs -1 because A is (numerically) less than B. System.out.println ("c1.equals (c2): " + c1.equals (c2)); outputs false because the Characters that c1 and c2 reference contain different characters (A and B). Finally, System.out.println ("c1.equals (c3): " + c1.equals (c3)); outputs true because, although c1 and c3 reference different Characters, both objects contain the same character (A).

Character-oriented utility methods

Character serves as a repository for character-oriented utility methods. Examples of those methods include:

  • public static boolean isDigit(char c), which returns a Boolean true value if c's character is a digit. Otherwise, false returns.
  • public static boolean isLetter(char c), which returns a Boolean true value if c's character is a letter. Otherwise, false returns.
  • public static boolean isUpperCase(char c), which returns a Boolean true value if c's character is an uppercase letter. Otherwise, false returns.
  • public static char toLowerCase(char c), which returns the lowercase equivalent of c's character if it is uppercase. Otherwise c's character returns.
  • public static char toUpperCase(char c), which returns the uppercase equivalent of c's character if it is lowercase. Otherwise c's character returns.


The following code fragment demonstrates those five methods:

System.out.println (Character.isDigit ('4')); // Output: true
System.out.println (Character.isLetter (';')); // Output: false
System.out.println (Character.isUpperCase ('X')); // Output: true
System.out.println (Character.toLowerCase ('B')); // Output: b
System.out.println (Character.toUpperCase ('a')); // Output: A


Another useful utility method is Character's public static char forDigit(int digit, int radix), which converts digit's integer value to its character equivalent in the number system that radix specifies and returns the result. However, if digit identifies an integer less than zero or greater than or equal to radix's value, forDigit(int digit, int radix) returns the null character (represented in source code as Unicode escape sequence '\u0000'). Similarly, if radix identifies an integer less than Character's MIN_RADIX constant or greater than Character's MAX_RADIX constant, forDigit(int digit, int radix) returns the null character. The following code demonstrates that method:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

good luck with that.By Anonymous on May 11, 2009, 11:47 amgood luck with that.

Reply | Read entire comment

Comparing Characters to a stringBy Anonymous on April 1, 2009, 4:17 amI hve an assignment that requires me to compare 5 characters to a string

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources