Char_KeyWord

char Java Keyword with Examples

The char keyword is used to declare character variable.
char is a Java primitive type.
In Java, the data type used to store characters is char. The char data type is a single 16-bit Unicode character.
It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).
The Wrapper Character class represents char primitive type as an object.

char Java Keyword Examples
Here is a program that demonstrates char variables:

// Demonstrate char data type.
class CharDemo {
    public static void main(String args[]) {
        char ch1, ch2;
        ch1 = 88; // code for X
        ch2 = 'Y';
        System.out.print("ch1 and ch2: ");
        System.out.println(ch1 + " " + ch2);
    }
}

This program displays the following output:

ch1 and ch2: X Y

Although char is designed to hold Unicode characters, it can also be used as an integer type on which you can perform arithmetic operations.
For example, you can add two characters together, or increment the value of a character variable. Consider the following program:

// char variables behave like integers.
class CharDemo {
    public static void main(String args[]) {
        char ch1;
        ch1 = 'X';
        System.out.println("ch1 contains " + ch1);
        ch1++; // increment ch1
        System.out.println("ch1 is now " + ch1);
    }
}

The output generated by this program is shown here:

ch1 contains X
ch1 is now Y

In the program, ch1 is first given the value X. Next, ch1 is incremented. This results in ch1 containing Y, the next character in the ASCII (and Unicode) sequence.

Summary

The following char constants are available:

  • \b − Backspace
  • \f − Form feed
  • \n − Newline
  • \r − Carriage return
  • \t − Horizontal tab
  • ’ − Single quote
  • " − Double quote
  • " − Backslash

The char data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive).
The Wrapper Character class represents char primitive type as an object and which includes useful static methods for dealing with char variables, including isDigit(), isLetter(), isWhitespace() and toUpperCase().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值