Understanding char data type – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #JAVA#3848    siteicon   siteicon  

Understanding char data type

The java char data type and its possible values are explained here.

Learning Objectives

  • Declaration and initialisation of char type with different possible values.

Source Code

TC++ #3848

Source Code


class CharType
{
public static void main(String[] args)
{
char a=0;
char b=10;
char c=33;
char d=65;
char e=126;
char f=255;
char g=65535;
char h='a';
char i='u03C0';
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
System.out.println(i);
}
}

Test it !

Run Output




!
A
~
ÿ
￿
a
π

Code Understanding

char a=0;
Java allows to store the 0 value (null value) also in character type. It doesn’t have special significance is in C/C++.

char b=10;
The non-displayable character values like integer value for control character newline (LF) can also be directly stored.

char c=33;
This is the first displayable ascii character after space which is the character !

char d=65;
This is first uppercase alphabet the decimal value for A.

char e=126;
This is the last 7-bit ascii displayable character ~ .

char f=255;
This is the largest 8 bit character which will be displayed as per the output device capability.

char g=65535;
This is the largest character value to be stored. This will not display anything with normal print commands unless we define proper encoding etc.

char h=’a’;
This is the usual single quote based initialisation of character

char i=’u03C0′;
This is unicode style initialisation. The current case has been shown for character (pi) .

Notes

  • char data type in java is unique in terms of having only positive values as against the short integer type data which is also 16 bit but it can have both positive and negative values.


Suggested Filename(s): CharType.java



Share

sunmitra| Created: 19-Mar-2018 | Updated: 19-Mar-2018|






Back