- 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:
- public final class Character
- extends Object
- 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.
- package com.instanceofjavatutorial;
- public class CharacterDemo {
- public static void main(String[] args) {
- Character ch = new Character ('a');
- System.out.println(ch);
- }
- }
Output:
- 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.
- package com.instanceofjavatutorial;
- public class CharacterCharValueDemo {
- public static void main(String[] args) {
- Character ch = new Character ('a');
- char c=ch.charValue();
- System.out.println(c);
- }
- }
Output:
- 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.
- package com.instanceofjavatutorial;
- public class CharactertoStringDemo {
- public static void main(String[] args) {
- char ch ='I';
- String str="";
- str=Character.toString(ch);
- System.out.println(str);
- }
- }
Output:
- 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
- package com.instanceofjavatutorial;
- public class CharactervalueOfDemo {
- public static void main(String[] args) {
- char ch ='S';
- Character chobj='';
- chobj=Character.valueOf(ch);
- System.out.println(chobj);
- }
- }
Output:
- S
- 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
- package com.instanceofjavatutorial;
- public class CharacteruppercaseDemo {
- public static void main(String[] args) {
- char ch ='a';
- char c=Character.valueOf(ch);
- System.out.println(c);
- }
- }
Output:
- 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
- package com.instanceofjavatutorial;
- public class CharacterlowercaseDemo {
- public static void main(String[] args) {
- char ch ='L';
- char c=Character.valueOf(ch);
- System.out.println(c);
- }
- }
Output:
- l
No comments