×

Java Char Keyword

  • The java char keyword is a data type which is defined as character data type.
  • Char keyword belongs to a primitive data type where the data types are classified into primitive and non-primitive.
  • The char keyword is mainly used for declaring the character-type variables and character type methods.
  • The character data type or keyword has the capability or capacity of storing of holding or provides the unsigned 16-bit character of Unicode.
  • The char data type or keyword is considered as a character's group which provides the symbols of alphabets and numbers in a character set.
  • The size of the keyword char provided with 16 bits.
  • The Char data type or keyword value range is defined between 0 to 65,536.
  • And the characters range according to the standard ASCII is defined from 0 to 127.

Points to be remembered are:

  • The Range of the char lies between 0 to 65,535(inclusive).
  • The default value of the char keyword is given as ‘\u0000’.
  • The Default size of the char data type or keyword is given by 2 bytes.
  • The characters are stored by using or implementing the char data type or keyword.

Characteristics of Char Keyword

The essential characteristics of the char keyword are given below:

  • As mentioned in the above information that char contain the range between 0 to 65,535.
  • The default value of the char ‘\u0000’ is the lowest range of the Unicode.
  • Java uses the Unicode system not ASCII code system that is why the default value is 2 bytes.

Syntax for Java Char Keyword

char_variable name =’variable value’;

For example,

char a =’x’; // where a is the variable name and x is the variable value.

Let us understand more about the char keyword using the example programs

Example 1:

In this simple example we will learn to display the characters that are been implemented using the char data type or keyword.

public class ExOfChar1 {  
    public static void main (String [] args) {  


        char ch1='R';
        char ch2='D';




        System.out.println("ch1: "+ch1);
        System.out.println("ch2: "+ch2);
    }         
}  

Output:

ch1: R
ch2: D

Example2:

Printing or executing Characters using ASCII value

Here in this example the three char variables or declared with the integers. So, on executing those variables, the integer values which are assigned to the variables will be converted to their ASCII values and shown or displayed in the output window.

public class ex {
    public static void main (String [] args)        
        char s1, s2, s3;
        /*
         *  Since 68 and 69 are the ASCII value for D and E, 
         *  We have given s1 as 68 and s3 as 69.
         */
s1 = 68;
s2 = 'B';
s3 = 69;
        System.out.println("The characters are: " + s1 + s2 + s3)      
}
}

Output:

The characters are: DBE

Example3:

Typecasting the integer value to char explicity in java

Here for type casting the integer valueto char explicity we declare the variables and assign them with the integer value.All the numeric value will change to the characters as every numeric belong to some character.

For example, 65 belong to A, 78 belongs to N etc.

public class TypecastExample3 {  
    public static void main (String [] args) {  
    int n1=97;
        char D1=(char)n1;


        int n2=65;
        char D2=(char)n2;


        System.out.println("D1: "+D1);
        System.out.println("D2: "+D2);
    }  
}  

Output:

D1: a
D2: A

                                                                    OR

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


        int n1 = 66;
        char G1 = (char)n1;


        int n2 = 76;
        char G2 = (char)n2;


        int n3 = 79;
        char G3 = (char)n3;


        int n4 = 71;
        char G4 = (char)G4;


        System.out.println(G1);
        System.out.println(G2);
        System.out.println(G3);
        System.out.println(G4);
    }
}

Example4:

Implementing the char value in Unicode System

public class Ex4 {  
    public static void main (String [] args) {  


        char D1='\u0061';
        char D2='\u0041';


        System.out.println("D1: "+D1);
        System.out.println("D2: "+D2);
    } 
}  

Output:

D1: a
D2: A

Example5:

Here in this program, we implement the code for increment and decrement of the char value by 1.

For increment and decrement we use the ++ and – operators. Here we also include the

Print statement that executes before and after values of the given variable.

public class ex {
    public static void main (String [] args) {


        char A1 = 'A';
        System.out.println("The value of A1 is: " + A1);


A1++;
        System.out.println("After incrementing: " + A1);


A1--;
        System.out.println("After decrementing: " + A1);
    }
}

Output:

The value of A1is: A
After incrementing: B
After Decrementing: A

Related Topics

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

4 minutes read.

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Static and Dynamic Binding in Java

Data Binding in Java The relation between a class and method or class and field is known as Data Binding. In Java, we can create a class for Student_info, but until we...

2 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Tree Implementation in Java

Introduction to Tree A non-linear, hierarchical data structure called a "tree" is made up of a number of nodes, each of which contains a values and a sequence of pointers to...

14 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Java vs DotNET

Before understanding the differences between DotNET and Java, one must know about Java and DotNET. Java Java is a general-purpose programming language that is class-based and object-oriented, with minimal implementation dependencies. Regardless of...

9 minutes read.

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is...

3 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.