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
java_lang_character.htm
Advertisements