- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Character valueOf() Method
The Java Character valueOf() method returns a Character instance representing the specified char value.
An instance is a variable that is a part of the class but is defined outside the scope of any class method.
However, if a program does not require a new Character instance, this method will be used in preference to the constructor of the Character class: Character(char). It is because this method will most probably yield better space and time performance by caching frequently requested values.
This method will always cache values within the range '\u0000' to '\u007F', both inclusive, and may also cache values outside of the given range.
Syntax
Following is the syntax for Java Character valueOf() method
public static Character valueOf(char c)
Parameters
c − a char value
Return Value
This method returns a Character instance representing c.
Example
The following example shows the usage of Java Character valueOf() method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create 2 Character objects c1, c2 Character c1, c2; // create 2 char primitives and assign values char ch1 = 'i'; char ch2 = 65; // assign Character values of ch1, ch2 to c1, c2 c1 = Character.valueOf(ch1); c2 = Character.valueOf(ch2); String str1 = "Character value of " + ch1 + " is " + c1; String str2 = "Character value of " + ch2 + " is " + c2; // print c1, c2 values System.out.println( str1 ); System.out.println( str2 ); } }
Output
Let us compile and run the above program, this will produce the following result −
Character value of i is i Character value of A is A
Example
Another example to understand the usage of the method is given as follows.
import java.lang.*; public class valueOfDemo { public static void main(String args[]) { char ch = '\u0064'; System.out.println("The character value of " + ch + " is " + Character.valueOf(ch)); } }
Output
Let us compile and run the program above, the output will then be displayed as given below −
The character value of d is d
Example
In this example, we will declare and initialize an int variable with any code point value and perform type casting to pass it as an argument to the method. The program is as follows −
import java.lang.*; public class CharacterDemo { public static void main(String args[]) { int cp = 0x0065; char ch = Character.valueOf((char)cp); System.out.println("The character value of given code point is " + ch); } }
Output
The output printed after compiling and executing the program above will be as follow −
The character value of given code point is e
Example
Following example takes ASCII values of digits as arguments for the method by type casting them.
import java.lang.*; public class CharacterDemo { public static void main(String args[]) { int cp = 53; char ch = Character.valueOf((char)cp); System.out.println("The corresponding character of given ascii value is " + ch); } }
Output
Compile and run the program given above and the output is displayed as follows −
The corresponding character of given ascii value is 5