char type in java

Previous
Next

Character data type:

  • Character type can store characters, symbols and digits.
  • Character represents using ‘single quotes’.
class CharacterType 
{
	public static void main(String[] args) 
	{
		char x = 'a';
		char y = '5';
		char z = '#';
		System.out.println("x : " + x + "\ny : " + y + "\nz : " + z);
	}
}

Character System:

  • Program is a set of instructions.
  • Instruction consists alphabets, digits and symbols.
  • System converts every instruction into binary format.
  • It uses character system to convert all symbols in the program into binary values.
  • Character system represents all symbols of a language using constant integer values.
  • Examples ASCII and UNICODE.

ASCII: Americans Standard Code for Information Interchange

  • C, C++ languages follow ASCII character system.
  • ASCII set represents all symbols using 0-255 integer values.
  • 1 byte memory can able to store a value between 0-255, hence character size is 1 byte in C and C++ languages.

We can store integer values into character type:

class DataTypes 
{
	public static void main(String[] args) 
	{
		char x = 65;
		char y = 57;
		char z = 35;
		System.out.println("x val : " + x);
		System.out.println("y val : " + y);
		System.out.println("z val : " + z);
	}
}

Output:
x val : A
y val : 9
z val : #

We can display the integer values of symbols by storing directly into int type variables:

class DataTypes 
{
	public static void main(String[] args) 
	{
		int x = 'a';
		int y = '5';
		int z = '$';
		System.out.println("x val : " + x);
		System.out.println("y val : " + y);
		System.out.println("z val : " + z);
	}
}

Output:
x val : 97
y val : 53
z val : 36

Program to display the Complete ASCII character set

class ASCII 
{
	public static void main(String[] args) 
	{
		System.out.println("ASCII Character Set : ");
		for (int i=0 ; i<256 ; i++)
		{
			System.out.println(i + " : " + (char)i);
		}
	}
}

Why character size is 1 byte in C and C++?

  • C and C++ languages are platform dependent.
  • Using C and C++, we can develop only Standalone applications.
  • Standalone application represents only one language character set at a time.
  • Using 1 byte range(256), we can represent the symbols of 1 language.

UNICODE: Universal Code

  • The size of UNICODE character is 2 bytes.
  • The programming languages (java, .net, python…) are using UNICODE character system in web applications development.
  • Web applications need to represent more than one language character set at a time.
  • To represent more than 1 language character set; 1 byte memory is not enough, hence it occupies 2 bytes.
  • For example a web application can run from multiple machines with different languages at a time.
  • A web application needs to specify more than 1 language character set.
  • Hence each character occupies 2 bytes memory – UNICODE
  • UNICODE – 2 bytes
    • 65536
    • 256 x 256
    • 256 languages
  • A Unicode character system can represent up to 256 languages at a time.
Previous
Next

Add Comment

Courses Enquiry Form