Following is an example by using
java.lang.Character.charValue() method.
DemoCharacter.java
[java]
package com.SPlessons;
public class DemoCharacter {
public static void main(String[] args) {
// create a Character object c
Character c;
// assign value to c
c = new Character('V');
// create a char primitive ch
char ch;
// assign primitive value of c to ch
ch = c.charValue();
String str = "The primitive char value is " + ch;
// print ch value
System.out.println( str );
}
}
[/java]
To return the value of character charValue() will be used. Where the developer assigned the character that is V.
Output: The result is as follows.
[java]
The primitive char value is V
[/java]
By using java.lang.Character.getNumericValue(char ch) Method
Already discussed the functionality of this method in above table, following is the syntax declaration of this method.
[java]
public static int getNumericValue(char ch)
[/java]
DemoCharacter.java
[java]package com.SPlessons;
public class DemoCharacter {
public static void main(String[] args) {
// create 2 character primitives ch1, ch2
char ch1, ch2;
// assign values to ch1, ch2
ch1 = 'x';
ch2 = '8';
// create 2 int primitives i1, i2
int i1, i2;
// assign numeric values of ch1, ch2 to i1, i2
i1 = Character.getNumericValue(ch1);
i2 = Character.getNumericValue(ch2);
String str1 = "Numeric value of " + ch1 + " is " + i1;
String str2 = "Numeric value of " + ch2 + " is " + i2;
// print i1, i2 values
System.out.println( str1 );
System.out.println( str2 );
}
}
[/java]
The purpose of this method is to give back the int esteem that the predetermined Unicode character represents, here the developer given the character that is
x.
Output: In the result the character x value will be generated.
[java]
Numeric value of x is 33
Numeric value of 8 is 8
[/java]
By using java.lang.Character.isDigit(char ch) Method
DemoCharacter.java
[java]
package com.SPlessons;
public class DemoCharacter {
public static void main(String[] args) {
// create 2 char primitives ch1, ch2
char ch1, ch2;
// assign values to ch1, ch2
ch1 = '7';
ch2 = 'h';
// create 2 boolean primitives b1, b2
boolean b1, b2;
// assign isDigit results of ch1, ch2 to b1, b2
b1 = Character.isDigit(ch1);
b2 = Character.isDigit(ch2);
String str1 = ch1 + " is a digit is " + b1;
String str2 = ch2 + " is a digit is " + b2;
// print b1, b2 values
System.out.println( str1 );
System.out.println( str2 );
}
}
[/java]
The functionality of this method is to know whether given number is a digit or not. if it is digit then it returns true otherwise, returns false.
Output: The result will be as follows. where in code the developer is given number 7 so it is a digit.
[java]
9 is a digit is true
V is a digit is false[/java]