Wednesday, October 9, 2013

All about premitive and characters...conversions...etc ************************

The Low level  discussion of primitive data types and Representations :


byte notation   bit notation                                                        combinations            range

1byte    = =      8bit    = =    pow(2,8)                                            256                -128 to +127
2byte    = =      16bit  = =    pow(2,8)*pow(2,8)                           65536          -32,768 to +32,767
4byte    = =      32bit  = =    pow(2,8)*pow(2,8)*pow(2,8)             xxxxxxx           xxxxxxxxxxxxxx
8byte    = =      64bit  = =    pow(2,8)*pow(2,8)*pow(2,8)*pow(2,8)  xxx              xxxxxxxxxxxxx


In java primitive

byte  - The byte data type is an 8-bit signed two's complement integer.
short - The short data type is a 16-bit signed two's complement integer.
int    -  The int data type is a 32-bit signed two's complement integer.
long - The long data type is a 64-bit signed two's complement integer.

float - The float data type is a single-precision 32-bit IEEE 754 floating point.
double - The double data type is a double-precision 64-bit IEEE 754 floating point.

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).

here char is of 16 bit unicode so it's data representation will be in the form of hexadecimal like /uffff


here hexadecimal start with '0' and ends with 'f'

so numberin will be like below

0    - 0
1    - 1
2    - 2
3    - 3
4    - 4
5    - 5
6    - 6
7    - 7
8    - 8
9    - 9
10  -A
11  -B
12  -C
13  -D
14  -E
15  -F

TOTAL numbers from 0 to 15 are 16, means base 16.

char clarification :
    Literals of types char and String may contain any Unicode (UTF-16) characters. If your editor and file system allow it, you can use such characters directly in your code. If not, you can use a "Unicode escape" such as ' \u0061' ( Latin small letter A)

below code will give you clarity

code snippet:
char i= '\u0061';
char j= 97;
char k= 'a';

System.out.println(i);
System.out.println((int)i);
System.out.println((char)i);
System.out.println(j);
System.out.println((int)j);
System.out.println((char)j);
System.out.println(k);
System.out.println((int)k);
System.out.println((char)k);

output:
a
97
a
a
97
a
a
97
a



Value conversions: 

From the above code, let us discuss on char 'a'

\u0061  -  it is in hexa decimal notation  -- 16bit -- 2byte -- pow(2,8)*pow(2,8) -- combinations of 16 bits in binary representation.

0000000001100001   is the Binary representation of character 'a' ( total 16bits)  use this link1 or link2 to calculate the value of this binary code.

convert it into decimal like this

0*pow(16,3)+0*pow(16,2)+6*pow(16,1)+1*pow(16,0) =  0+0+6*16+1*1 = 0+0+96+1 =  97 

So, 97 is the decimal notation of character 'a'.


..
..
..
..
To be continued.......


sources:
http://www.binaryhexconverter.com/decimal-to-binary-converter
http://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html
http://en.wikipedia.org/wiki/List_of_Unicode_characters
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
http://docs.oracle.com/javase/tutorial/i18n/text/string.html***