< Prev
Next >



Character Wrapper Class





Wrapper classes are used to create an object version of primitive values and there is a separate wrapper class corresponding each primitive data-type. One of these wrapper classes is - Character wrapper class, which is used to create an object version of primitive char value.





//Reading the primitive value out of a Character object

class A
{
public static void main(String... ar)
{
Character ch = new Character('G');
System.out.println(ch); //Directly printing the char primitive value in Character object.


//Calling charValue() method of Character class.
char c = ch.charValue();
System.out.println("Value in a Character object = " +a);
}
}


Output-


G
Value in a Character object = G





Some important methods of Character Wrapper class.


Methods Description
static boolean isDigit(char ch) Returns a true if ch is digit else, false.
static boolean isLetter(char ch) Returns a true if ch is a letter, else false.
static boolean IsLetterOrDigit(char ch) Returns a true if ch is either a letter or a digit, else false.
static boolean isLowerCase(char ch) Returns true if ch is a lowercase letter, else false
static boolean isUpperCase(char ch) Returns true if ch is an uppercase letter, else false.
static boolean isWhitespace(char ch) Returns a true if a ch is a whitespace character, else false.
static char toLowerCase(char ch) returns the lowercase form of ch.
static char toUpperCase(char ch) Returns an uppercase form of ch.





To find if the primitive char value is a letter, digit or a whitespace.


//Using Character wrapper class

import java.util.*;

class A
{
public static void main(String... ar)
{
System.out.println("Is char a digit? : "+ Character.isDigit('a'));
System.out.println("Is char a digit? : "+ Character.isDigit('1'));
System.out.println("Is char a letter? : "+Character.isLetter('a'));
System.out.println("Is char a letter? : "+Character.isLetter('1'));
System.out.println("Is char a letter or a digit? : "+ Character.isLetterOrDigit('-'));
System.out.println("Is char a letter or a digit? : "+ Character.isLetterOrDigit('1'));
System.out.println("Is char a whitespace? : "+ Character.isWhitespace(' '));
}
}

Output-


Is char a digit? : false
Is char a digit? : true
Is char a letter? : true
Is char a letter? : false
Is char a letter or a digit? : false
Is char a letter or a digit? : true
Is char a whitespace? : true





To find the case of a primitive char value or to alter the case.


//Wrapper class Character

import java.util.*;

class A
{
public static void main(String... ar)
{
System.out.println("Is C a lowercase character? : "+ Character.isLowerCase('C'));
System.out.println("Is y a lowercase character? : "+ Character.isLowerCase('y'));
System.out.println("Is a an uppercase character? : "+ Character.isUpperCase('a'));
System.out.println("Is n an uppercase character? : "+ Character.isUpperCase('N'));

//Changing the case of character in Character object.
System.out.println("Converting A to LowerCase : "+ Character.toLowerCase('A'));
System.out.println("Converting a to UpperCase : "+ Character.toUpperCase('a'));
}
}

Output-


Is C a lowercase character? : false
Is y a lowercase character? : true
Is a an uppercase character? : false
Is n an uppercase character? : true
Converting A to LowerCase : a
Converting a to UpperCase : A




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< instanceof operator
Boolean Wrapper Class>

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



COMING SOON
-
PYTHON