Advertisement



< Prev
Next >



Character Wrapper Class





Wrapper classes are used to create an object version of a primitive value and there is a separate wrapper class corresponding to each primitive data-type. One of these wrapper classes is - Character wrapper class, which is used to create an object version of a 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');
	//Directly printing the char primitive value in Character object.
	System.out.println(ch); 


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


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 iswhite space(char ch) Returns a true if a ch is a white space 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.



Advertisement




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


//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 white space? : " + Character.iswhite space(' '));
}
}

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 white space? : 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 -





< Prev
Next >
< instanceof operator
Boolean Wrapper Class>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement