« Previous Page Next Page »
    

Introduction to Java Character Class.

POSTED

AUTHOR: VARUN SINGH

In this tutorial, you are going to learn how to create Java character. In this chapter you also learn java character methods to declare characters in java programming language.


1. Java Character:

Java provides a wrapper class Character in java.lang package. An object of type Character contains a single field, whose type is char.

Syntax for Creating a Character object :

Character ch = new Character('a');

• The above statement creates a Character object which contain 'a' of type char.

• If we pass a primitive char into a method that expects an object, the compiler           automatically converts the char to a Character class object.

• The Character class is immutable once it's object is created, it cannot be changed.


2. Character Class Methods in Java:

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

1. Java isLetter () Method:

boolean isLetter(char ch) : This method is used to determine whether the specified char value(ch) is a letter or not. The method will return true if it is letter([A-Z],[a-z]), otherwise return false.

Syntax:

boolean isLetter(char ch)

Parameters:

ch - Primitive character type.

Returns :

• Returns true if character is a alphabet, otherwise return false

Example:

// Java program to demonstrate isLetter() method
public class Test
{
    public static void main(String[] args)
    {
        System.out.println(Character.isLetter('A')); 
         
        System.out.println(Character.isLetter('2')); 
         
     }
}

Output:

true
false


2. Java isDigit() Method:

This method is used to determine whether the specified char value is a digit or not.

Syntax:

boolean isDigit(char ch)

Parameters:

ch - Primitive character type.

Return Value:

This method returns true, if the passed character is really a digit.

Example:

// Java program to demonstrate isDigit() method
public class TestDigit
  {
    public static void main(String[] args)
    {
      // print false as A is character
      System.out.println(Character.isDigit('A'));
      System.out.println(Character.isDigit('0'));
    }
  }

Output:

false
true

3. Java isWhitespace() Method:

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

Syntax:

boolean isDigit(char ch)

Parameters:

ch - Primitive character type.

Return Value:

This method returns true, if the passed character is really a digit.

Example:

// Java program to demonstrate isWhitespace() method
public class Test {

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

Output:

false
true
true
true

4. Java isUpperCase() Method

This method determines whether the specified char value is uppercase is not.

Syntax:

boolean isUpperCase(char ch)

Parameters:

ch - Primitive character type.

Return Value:

• This method returns true, if the passed character is really an uppercase.

Example:

// Java program to demonstrate isUpperCase() method
public class Test {

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

Output:

true
false
false
false


5. Java isLowerCase() Method

The method determines whether the specified char value is lowercase is not.

Syntax:

boolean isLowerCase(char ch)

Parameters:

ch - Primitive character type.

Return Value:

• This method returns true, if the passed character is really in lowercase.

Example:

// Java program to demonstrate isLowerCase() method
public class Test {

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

Output:

false
true
false
false

6. Java toUpperCase() Method

Syntax:

char toUpperCase(char ch)

Parameters:

ch - Primitive character type.

Return Value:

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

Example:

// Java program to demonstrate toUpperCase() method
public class Test {

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

Output:

A
A


7. Java toLowerCase() Method

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

Syntax:

char toLowerCase(char ch)

Parameters:

ch - Primitive character type.

Return Value:

• This method returns the lowercase form of the specified char value.

Example:

// Java program to demonstrate toLowerCase() method
public class Test {

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

Output:

a
a

8. Java toString() Method

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

Syntax:

String toString(char ch)

Parameters:

ch - Primitive character type.

Return Value:

This method returns String object.

Example:

// Java program to demonstrate toString() method
public class Test {

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

Output:

a
A


The End:

Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

« Previous Page Next Page »

Comments: