JAVA BEGINNERS TUTORIAL – Working with Characters

when we need to use one character and dealwith it we use primitive data types char.

char ch = 'a';

// Unicode for the arrow ↘ as character
char uniChar = '\u2198'; 

// an array of chars
char[] charArray ={ 'a', 'b', 'c', 'd', 'e' };

in development, we come across some situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper class Character for primitive data type char.

The Character class offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor:

Character ch = new Character('a');

The Java compiler will also create a Character object for you under some circumstances. For example, if you pass a primitive char into a method that expects an object, the compiler automatically converts the char to a Character for you. This feature is called autoboxing—or unboxing, if the conversion goes the other way.

Escape Sequences:

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.

The newline character (\n) has been used frequently in this tutorial in System.out.println() statements to advance to the next line after the string is printed.

Following table shows the Java escape sequences:

Escape Sequence Description
\t Inserts a tab in the text at this point.
\b Inserts a backspace in the text at this point.
\n Inserts a newline in the text at this point.
\r Inserts a carriage return in the text at this point.
\f Inserts a form feed in the text at this point.
\’ Inserts a single quote character in the text at this point.
\” Inserts a double quote character in the text at this point.
\\ Inserts a backslash character in the text at this point.

When an escape sequence is encountered in a print statement, the compiler interprets it accordingly.

Example:

If you want to put quotes within quotes you must use the escape sequence, \”, on the interior quotes:

public class Test {

   public static void main(String args[]) {
      System.out.println("She said \"Hello!\" to me.");
   }
}

Character Methods:

Here is the list of the important instance methods that all the subclasses of the Character class implement:

isLetter()

The method determines whether the specified char value is a letter.

boolean isLetter(char ch)
  • ch — Primitive character type
  • This method Returns true if passed character is really a character.

Example:

public class Test {

   public static void main(String args[]) {
      System.out.println(Character.isLetter('c'));
      System.out.println(Character.isLetter('5'));
   }
}

 isDigit()

The method determines whether the specified char value is a digit.

boolean isDigit(char ch)
  • ch — Primitive character type
  • This method Returns true if passed character is really a digit.

Example:

public class Test {

   public static void main(String args[]) {
      System.out.println(Character.isDigit('c'));
      System.out.println(Character.isDigit('5'));
   }
}

isWhitespace()

The method determines whether the specified char value is a white space, which includes space, tab or new line.

boolean isWhitespace(char ch)
  • ch — Primitive character type
  • This method Returns true if passed character is really a white space.

Example:

public class Test{

   public static void main(String args[]){
      System.out.println(Character.isWhitespace('c'));
      System.out.println(Character.isWhitespace(' '));
      System.out.println(Character.isWhitespace('\n'));
      System.out.println(Character.isWhitespace('\t'));
   }
}

isUpperCase()

The method determines whether the specified char value is uppercase.

boolean isUpperCase(char ch)
  • ch — Primitive character type
  • This method Returns true if passed character is really an uppercase.

Example:

public class Test{

   public static void main(String args[]){
      System.out.println( Character.isUpperCase('c'));
      System.out.println( Character.isUpperCase('C'));
      System.out.println( Character.isUpperCase('\n'));
      System.out.println( Character.isUpperCase('\t'));
   }
}

isLowerCase()

The method determines whether the specified char value is lowercase.

boolean isLowerCase(char ch)
  • ch — Primitive character type
  • This method Returns true if passed character is really an lowercase.

Example:

public class Test{

   public static void main(String args[]){
      System.out.println(Character.isLowerCase('c'));
      System.out.println(Character.isLowerCase('C'));
      System.out.println(Character.isLowerCase('\n'));
      System.out.println(Character.isLowerCase('\t'));
   }
}

toUpperCase()

The method returns the uppercase form of the specified char value.

char toUpperCase(char ch)
  • ch — Primitive character type
  • This method Returns the uppercase form of the specified char value.

Example:

public class Test{

   public static void main(String args[]){
      System.out.println(Character.toUpperCase('c'));
      System.out.println(Character.toUpperCase('C'));
   }
}

toLowerCase()

The method returns the lowercase form of the specified char value.

Syntax:

char toLowerCase(char ch)
  • ch — Primitive character type
  • This method Returns the lowercase form of the specified char value.

Example:

public class Test{

   public static void main(String args[]){
      System.out.println(Character.toLowerCase('c'));
      System.out.println(Character.toLowerCase('C'));
   }
}

toString()

The method returns a String object representing the specified character value, that is, a one-character string.

String toString(char ch)
  • ch — Primitive character type
  • This method Returns String object

Example:

public class Test{

   public static void main(String args[]){
      System.out.println(Character.toString('c'));
      System.out.println(Character.toString('C'));
   }
}