• Character class is presented in java.lang package
  • java.lang.Character class is used to represent primitive character value to character object.

Character Class Definition:

  1. public final class Character
  2.  extends Object 
  3. implements Serializable, Comparable<Character>

Character Class Constructors:

1. public Character(char value)
  • Character class having only one constructor which accepts char primitive value.

Java Program to convert char primitive value to Character Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharacterDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Character ch = new Character ('a');
  8.   System.out.println(ch);
  9.  
  10.  
  11. }
  12. }

Output:
  1. a

 Character Class Methods:

1.public char charValue()

  • This method returns the primitive char value from the Character object.

Java code to get char primitive value from Character Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharacterCharValueDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Character ch = new Character ('a');
  8.   char c=ch.charValue();

  9.  System.out.println(c);
  10.  
  11. }
  12. }

Output:
  1. a

2.public static String toString(char c) :

  • toString(char c) method present in Character class accepts "char" primitive values and returns String objects representing that character.
Java code to convert char primitive value to String object

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharactertoStringDemo {
  4.  
  5. public static void main(String[] args) {

  6.   char ch ='I';
  7.  
  8.   String str="";
  9.  
  10.   str=Character.toString(ch);
  11.  
  12.   System.out.println(str);
  13. }
  14. }

Output:
  1. I

3.public static Character valueOf(char c)

  • valueOf(char c)  is static method present in Character class used to convert char value to Chraracter object.
  • It accepts char primitive value and returns Character object

Java code to convert char primitive value to String object

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharactervalueOfDemo {
  4.  
  5. public static void main(String[] args) {

  6.   char ch ='S';
  7.  
  8.   Character chobj='';
  9.  
  10.   chobj=Character.valueOf(ch);
  11.  
  12.   System.out.println(chobj);
  13. }
  14. }

Output:
  1. S
4.public static char toUpperCase(char ch)

  • toUpperCase(char ch) static method present in Character class used to convert a character to uppercase.
  • It accepts a character and returns same character in uppercase.


Java code to convert char primitive value to uppercase

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharacteruppercaseDemo {
  4.  
  5. public static void main(String[] args) {

  6.   char ch ='a';
  7.  
  8.   char c=Character.valueOf(ch);
  9.  
  10.   System.out.println(c);
  11. }
  12. }



Output:
  1. A

5.public static char toLowerCase(char ch) 

  •  toLowerCase(char ch) static method present in Character class used to convert a character to lowercase.
  • It accepts a character and returns same character in lowercase.


Java code to convert char primitive value to lowercase

  1. package com.instanceofjavatutorial;
  2.  
  3. public class CharacterlowercaseDemo {
  4.  
  5. public static void main(String[] args) {

  6.   char ch ='L';
  7.  
  8.   char c=Character.valueOf(ch);
  9.  
  10.   System.out.println(c);
  11. }
  12. }

Output:
  1. l



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu