Chars are digits?


When I was a kid for my computer classes, my master said – “Computer understands only binary digits”.

I thought – he is bluffing.  

I was into a position to digest — Okay computer understands 1 as 1, 2 as 2 — Coz I had a knowledge of decimal to binary conversion.

1 — 0000 0000 0000 0001 ( for 16 bit m/c)

5 — 0000 0000 0000 0110 ( for 16 bit m/c)

But I was not ready to accept the way my teacher said. I ask myself – How come “A” will be represented as some 1, 2, 3, 4 – in digit format. So if I write “Hello World” — does it mean ?- Is this something like 4324421625834  or 0010 1000 0011 1100…..

After 4 years of my engineering and 6 years of IT experience, I was going through the “Primitive Data Types” in the book “Java Certification by Khalid A. Mughal & Rolf W. Rasmussen”.

 The diagram in the book has given me a big shock!!!!!!!!!!!!!!!!!  
               
Primitive Data Types in Java
 How come the char data type is grouped under “Integral types”?

 After reading carefully I came to know — Oh it’s an unsigned number.

So what — still it’s a number!

After rehearsing the same line again and again –> “Characters are represented by the data type char. Their values are unsigned integers that denote all the 65536 (2 pow 16) characters in the 16-bit Unicode character set. This includes letters, digits, and special characters.

Ok, so got it finally. What does the line say … even if it is a character that will be stored as some integer values ( of course unsigned).

 So to say further ‘A’ is infact \u0041 [\u for Unicode, followed by for digit Hexadecimal number  – equivalent to 65 in Decimal ]

 This is where A is interpreted as \u0041 or 65 in decimal or 0000 0000 0110 0001 in binary.

Once A is a digit, your computer will understand it as

‘A’ – 65 – 0000 0000 0110 0001

‘a’ – 97 – 0000 0000 0100 0001

This explanation also proves that why char has been grouped under Integral types in the diagram.

And also my master was right -“Computer understands only binary digits”.

 Now look at the below Java code now:

public class CharTypeDemo {
	public static void main(String[] args) {
		char char1 = 'a';  // I will be stored in computes as 61 only, but you will be able to see me as it is
		char char2 = '\u0061'; // because of "\\u" plz put my converted charter value
		
		int int1 = 'a';  // should be a type conversion here from char to int
		int int2 = 97;
		
		System.out.println("char1   char2   int1  int2");
		System.out.println(char1 + "\t" + char2 + "\t"  + int1 + "\t" + int2);	
	}
}

Output:

char1 char2 int1 int2
a a 97 97

Advertisements

One comment on “Chars are digits?

  1. forex fonds says:

    forex millon�r Good day, I absolutely enjoy your net page. Very much helpfull information. Thanks a bunch for sharing this with us. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s