Read Java Interview Questions and Answers for Beginners and Experienced

Java Character Example

In this Java example series, we will study on Java character example. we will see how we write character data type in Java with example.

 


What is character in Java

Character is the single character value in Java like ‘a‘ ‘b‘ ‘c‘ are three single character value.

 


Char data type in Java

char is one of the primitive data type of Java. char represent character value in Java. we can store the character type value in char data type in Java.

 


Syntax of char data type in Java

In Java programming language, syntax of character data type is char. character values in java are assigned to variable which is declared by char data type

char var_name='character_value';

Explanation

  • char is the data type to store the character values.
  • <em>var_name</em> is the name of variable.
  • = is assignment operator which assign the right value to left.
  • character_value  is the actual character value which will be assigned to var_name.

 


Java Character Example

We have read about character and its syntax and why we use character. Now its time to see the Java character example.


public class HelloWorld {

public static void main(String[] args) {

char ch='h';

System.out.print("Value of variable ch is: "+ch);

}

}

Output

output of java character example is

 Value of variable ch is: h 

Explanation

  • As we read that character values are single character value so we have assigned single character value which is ‘h‘ to the character variable ch. Variable ch is of data type character, as it is char.
  • when we will print character variable ch using System.out.print(). character value will be printed.

 

In this Java example series, we study about the Java character example.