×

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

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Best Java Security Framework

The security of applications is currently our top concern when creating them. The applications or bits of code running over the network are exposed to dangers and may jeopardize integrity,...

3 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Inheritance Program in Java

Inheritance Program in Java Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help...

7 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Java Tutorial

What is Java? Java is an object-oriented, robust, secured and platform-independent programming language. With the help of Java Programming, we can develop console, window, web, enterprise and mobile applications. Java language was...

22 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Java Math toRadians() Method

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

2 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.