java.lang.Character valueOf(char c)
Description
The Character.valueOf(char c) java method returns a Character instance representing the specified char value. If a new Character instance is not required, this method should generally be used in preference to the constructor Character(char), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range ‘u0000’ to ‘u007F’, inclusive, and may cache other values outside of this range.
The valueOf(char c) method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:
Character.valueOf(char c)
Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java valueOf() method non statically.
Method Syntax
public static Character valueOf(char c)
Method Argument
Data Type | Parameter | Description |
---|---|---|
char | c | a char value. |
Method Returns
The valueOf(char c) method of Character class returns a Character instance representing c.
Compatibility
Requires Java 1.5 and up
Java Character valueOf(char c) Example
Below is a simple java example on the usage of valueOf(char c) method of Character class.
package com.javatutorialhq.java.examples; /* * This example source code demonstrates the use of * valueOf(char c) method of Character class. */ public class CharacterValueOfExample { public static void main(String[] args) { // initialize a char primitive type char c = 'a'; // convert primitive char to Character object Character value = Character.valueOf(c); // initialize another Character Object anotherValue = new Character('a'); /* * check equality * this is to demonstrate the need of * having Character object than primitive * */ boolean result = value.equals(anotherValue); if(result){ System.out.print("They are equal"); } else{ System.out.print("They are not equal"); } } }
Sample Output
Below is the sample output when you run the above example.