logo

Método Java Carácter valueOf()

El método valueOf(char c) de la clase Character devuelve la instancia del Character que representa el valor de char dado. Si no se requiere el nuevo objeto del Carácter, entonces el método debe usarse con preferencia al constructor Carácter (char).

Es probable que el método anterior proporcione un mejor rendimiento espacial y temporal. Este método almacenará en caché los valores en el rango de 'u0000' a 'u007F' y puede haber algunas posibilidades de almacenar en caché los otros valores fuera del rango.

Sintaxis

 public static Character valueOf(char c) 

Parámetros

C : Es el valor del carácter que debe probarse.

Valor de retorno

El método valueOf(char c) devuelve el objeto de Carácter.

Ejemplo 1

 public class JavaCharactervalueOfExample1 { public static void main(String[] args) { // Create two character primitives c1 and c2. Character c1, c2; // Create two char primitives and assign the values. char ch1 = 'A'; char ch2 = 'h'; // Assign the character values of ch1 and ch2 to c1 and c2. c1 = Character.valueOf(ch1); c2 = Character.valueOf(ch2); String str1 = 'The character value of the character '' + ch1 + '' is given as: ' + c1; String str2 = 'The character value of the character '' + ch2 + '' is given as: ' + c2; // Print the values of ch1 and ch2. System.out.println( str1 ); System.out.println( str2 ); } } 
Pruébalo ahora

Producción:

 The character value of the character 'A' is given as: A The character value of the character 'h' is given as: h 

Ejemplo 2

 public class JavaCharactervalueOfExample2 { public static void main(String[] args) { // Declare two string values. String str1 = 'Hello'; String str2 = 'Everyone'; // Convert string value to character array. System.out.println('The word 'Hello' can be represented as:'); char[] values1 = str1.toCharArray(); for(char ch1:values1){ // Print the value. System.out.println(String.valueOf(ch1)); } System.out.println('The word 'Everyone' can be represented as:'); char[] values2 = str2.toCharArray(); for(char ch2:values2){ // Print the value. System.out.println(String.valueOf(ch2)); } } } 
Pruébalo ahora

Producción:

 The word 'Hello' can be represented as: H e l l o The word 'Everyone' can be represented as: E v e r y o n e