Java char – Character Data Type In Java With Examples

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 10, 2024

In this tutorial, we will learn all about Java char or Character Data Type which is another primitive data type in Java:

This tutorial will also include a brief description of char data type, syntax, range, and example programs that will help you understand this primitive data type in detail.

=> Check ALL Java Tutorials Here

Though this is a small topic, it is very important in terms of the usage of characters in Java. Hence we will cover the small details as well. Apart from that, we will take a look at some of the frequently asked questions related to the topic.

Java char

Java Char

The data type char comes under the characters group that represents symbols i.e. alphabets and numbers in a character set.

The Size of a Java char is 16-bit and the range is between 0 to 65,535. Also, the standard ASCII characters range from 0 to 127.

Given below is the syntax of char Java.

Syntax:

char variable_name = ‘variable_value’;

Characteristics Of char

Given below are the major characteristics of a char.

  1. As mentioned above, the range is between 0 to 65,535.
  2. The default value is ‘\u0000’ and that is the lowest range of Unicode.
  3. The default size (as mentioned above) is 2 bytes because Java uses the Unicode system and not the ASCII code system.

Displaying Characters

Given below is the simplest program of displaying the characters that have been initialized by using the char keyword.

public class example {

	public static void main(String[] args) {
		
		char c1 = 'x';
		char c2 = 'X';
		
		System.out.println("c1 is: " +c1);
		System.out.println("c2 is: " +c2);
		
	}

}

Output:

Displaying characters

Printing Characters Using ASCII Value

In the below example, we have initialized three char Java variables with the integers. Upon printing them, those integers will be converted into their ASCII equivalent. The compiler typecast integer to a character and then the corresponding ASCII value will be displayed.

public class example {

	public static void main(String[] args) {
		
		char c1, c2, c3;
		
		/*
		 *  Since 65 and 67 are the ASCII value for A and C, 
		 *  we have assigned c1 as 65 and c3 as 67.
		 */
		
		c1 = 65;
		c2 = 'B';
		c3 = 67;
		
		System.out.println("The characters are: " + c1 + c2 + c3);
		
	}

}

Output:

Printing characters using ASCII value

Incrementing And Decrementing The chars

In the below program, we have initialized a Java character variable and then we have tried incrementing and decrementing it using the operator.

A print statement is included before and after each operation to see how the value changes.

public class example {

	public static void main(String[] args) {
		
		char c1 = 'A';
		System.out.println("The value of c1 is: " + c1);
		
		c1++;
		System.out.println("After incrementing: " + c1);
		
		c1--;
		System.out.println("After decrementing: " + c1);
		
	}

}

Output:

Incrementing and Decrementing the chars

Breaking String Into Character Java

In this section, we will break a String in the form of Character Java. To start with, we have taken an input String and converted it into a Java character array. Then, we printed the value of the original String and the characters inside that array using the toString() method.

import java.util.Arrays;

public class example {

	public static void main(String[] args) {
		
		String str1 = "Saket";
		
		// conversion into character array
		char[] chars = str1.toCharArray();
		
		System.out.println("Original String was: " + str1);
		System.out.println("Characters are: " + Arrays.toString(chars));
		
	}

}

Output:

Breaking String into Character Java

Represent char Into Unicode System

In this section, we have initialized three Java characters with the Unicode value (escape sequence). Thereafter, we have simply printed those variables. The compiler will take care of the rest as it will explicitly convert the Unicode value into Java character.

Click here for Unicode Character Table.

import java.util.Arrays;

public class example {

	public static void main(String[] args) {
		
		char chars1 = '\u0058';
		char chars2 = '\u0059';
		char chars3 = '\u005A';
		
		System.out.println("chars1, chars2 and chars2 are: " + chars1 + chars2 + chars3);
		
	}

}

Output:

Represent char into Unicode system

Typecast Integer To char Java

In this section, we have initialized a variable with the integer value and then we typecast the integer value to Java char explicitly. All these integer variables that are initialized with the numeric value belong to some character.

For example, 66 belongs to B, 76 belongs to L, etc. You cannot specify any random integer and try to typecast it. In such cases, the compiler will fail to typecast and as a result, it will throw ‘?’ in the output.

import java.util.Arrays;

public class example {

	public static void main(String[] args) {
		
		int number1 = 66;
		char chars1 = (char)number1;
		
		int number2 = 76;
		char chars2 = (char)number2;
	
		int number3 = 79;
		char chars3 = (char)number3;
		
		int number4 = 71;
		char chars4 = (char)number4;
	
		System.out.println(chars1);
		System.out.println(chars2);
		System.out.println(chars3);
		System.out.println(chars4);
		
	}

}


Output:

Typecast integer to char Java

Frequently Asked Questions

Q #1) Can char be a number Java?

Answer: char Java can be a number as it is a 16-bit unsigned integer.

Q #2) What is the scanner for char in Java?

Answer: There is no such method called nextChar() in the Scanner Class. You need to use the next() method with charAt() method to get the char Java or the character Java.

Q #3) Can we convert String to char in Java?

Answer: Yes, by using the charAt() method, you can easily convert String to Java char.

Given below is an example of printing char values.

public class example {

	public static void main(String[] args) {
		
		String str = "Java";
		
		System.out.println(str.charAt(0));
		System.out.println(str.charAt(1));
		System.out.println(str.charAt(2));
		System.out.println(str.charAt(3));
		
	}

}

Output:

Convert string to char

Q #4) Why is char used in Java?

Answer: Java char is a primitive data type that is used to declare the Java character variables.

Q #5) What is the use of \t in Java?

Answer: The \t is an escape sequence in Java that moves the control to the next tab. It is similar to pressing the “TAB” key from the keyboard.

Also read =>> Convert Java String to Integer

Conclusion

In this tutorial, we explained Java char along with its description, range, size, syntax, and examples.

There are plenty of programs covered as a part of this topic that will help you to understand better. Apart from these, some frequently-asked questions were also covered for your better understanding.

=> Visit Here To Learn Java From Scratch

Was this helpful?

Thanks for your feedback!

Leave a Comment