Java - Character charValue() Method



The Java Character charValue() returns the value of a Character object. It is a built-in method that converts the Character object to the char data type.

The primitive data type char and the Character class differ where char is a 16-bit Unicode element and the Character class is a Wrapper class that uses this data type with the help of OOPS concepts.

The charValue() method is simply converting the Character Object into its primitive data type.

Syntax

Following is the syntax for Java Character charValue() method

public char charValue()

Parameters

This method does not accept any parameters.

Return Value

This method returns the primitive char value represented by this object.

Example

The following example shows the usage of Java Character charValue() method. Here, we instantiate a Character object with a primitive char value assigned to it. When we invoke this method on the object we just created, the primitive char value passed is returned. It can be stored in a char variable and be used for various other scenarios.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create a Character object c
      Character c;

      // assign value to c
      c = new Character('a');

      // create a char primitive ch
      char ch;

      // assign primitive value of c to ch
      ch = c.charValue();
      String str = "Primitive char value is " + ch;

      // print ch value
      System.out.println( str );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Primitive char value is a

Example

Another way to display the return value obtained by calling this method on a Character object is shown below.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      Character c;
      c = new Character('a');
      System.out.println("Primitive char value is '" + c.charValue() + "'");
   }
}

Output

If we compile and run the above code, the output is obtained as follows −

Primitive char value is 'a'

Example

Until now, we have just seen how to obtain the char value from a Character class. But it is also possible to overwrite a char value using this method. In this example, we will try to overwrite a char value with a digit Character.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create a Character object c
      Character c, ch;

      // assign value to c
      c = new Character('a');
      ch = new Character('9');
	  
      // print c value before overwriting
      System.out.println("Primitive char value is '" + c.charValue() + "'\n");
      
	   // the char value in the Character object ch is assigned implicitly to the Character object c
      c = ch.charValue();
	  
      // print c value after overwriting
      System.out.println("Primitive char value is '" + c.charValue() + "'\n");
   }
}

Output

The output for the program above after executing it is −

Primitive char value is 'a'
Primitive char value is '9'

Example

Similar to the digit characters, we can also overwrite the char value in one Character object with a symbol character in another Character object.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      Character c, ch;
      c1 = new Character('a');
      c2 = new Character('/');
      
      // print c value before overwriting
      System.out.println("Primitive char value is '" + c1.charValue() + "'\n");
      c1 = c2.charValue();
      
      // print c value after overwriting
      System.out.println("Primitive char value is '" + c1.charValue() + "'\n");
   }
}

Output

Once we compile the program above, the character value of a variable is overwritten by a symbol. The output is −

Primitive char value is 'a'
Primitive char value is '/'

Example

In another scenario, we can use loop statements to invoke this method on each element in a Character array.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create a character array object
      Character c[] = new Character[] {'a', 'b', 'c', 'd', 'e'};
      char ch;
      
      // using for loop, return the character value of each element in the array
      for(int i = 0; i < c.length; i++) {
         ch = c[i].charValue();
         System.out.println("Primitive char value is " + ch);
      }
   }
}

Output

Upon compiling and executing the program above, the output is displayed as −

Primitive char value is a
Primitive char value is b
Primitive char value is c
Primitive char value is d
Primitive char value is e
java_lang_character.htm
Advertisements