Java Characters

character in java

Normally, when we work with Java Characters, we use primitive data  types char.

Example:

char ch = ‘a’;
// Unicode for uppercase Greek omega character
char uniChar = ‘\u039A’;
// an array of chars
char[] charArray ={ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ };

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.

Example:

// Here following primitive char ‘a’
// is boxed into the Character object ch
Character ch = ‘a’;
// Here primitive ‘x’ is boxed for method test,
// return is unboxed to char ‘c’
char c = test(‘x’);

Escape Sequences:

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

Escape Sequence         Description
      \tInserts a tab in the text at this point.
      \bInserts a backspace in the text at this point.
      \nInserts a newline in the text at this point.
      \rInserts a carriage return in the text at this point.
      \fInserts 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.

Example:

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

Java Characters Methods:

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

Sr.No.   Method       Description
i.isLetter()Determines whether the specified char value is a letter.
ii.isDigit()Determines whether the specified char value is a digit.
iii.isWhitespace()  Determines whether the specified char value is white space.
iv.isUpperCase()Determines whether the specified char value is uppercase.
v.isLowerCase()Determines whether the specified char value is lowercase.
vi.toUpperCase()Returns the uppercase form of the specified char value.
vii.toLowerCase()  Returns the lowercase form of the specified char value.
viii.toString()Returns a String object representing the specified character value that is, a one-character string.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *