Friday, September 23, 2011

2. JAVA BASICS..JAVA PRIMITIVE DATA TYPES.. A CORRECT WAY TO START JAVA


There are 8 primitive java data type

We will write the java data types in increasing order of their size (in bytes).

boolean<byte<short ,char <int,float<long,double
    (1)        (1)        (2)            (4)            (8)

 Please notice, all data type start with a lowercase letter.
We will derive the possible values of these data types,according to the size of data type.

byte: memory =1byte=8 bits
 1(signed bit)   1    1    1    1    1    1    1  
                                           7       6    5     4     3     2    1   0
Our computer store numbers in 2's complement format.

Calculating the max value.
Signed bit=0 for positive number.Below is the format shown


 0(signed bit)   1    1    1    1    1    1    1  
                                           7       6    5     4     3     2    1   0

max value :
 (0*27)+26+25+24+23+22+21+20
       => 127
       => 27-1

Min value:
Taking the signed bit as 1, i.e negative number. For getting lowest -ve we make all other bits to 0
Below is the format shown

 1(signed bit)   0    0    0    0    0    0    0  
                                           7       6    5     4     3     2    1   0

Calculated value is     (-27)
    => -128

The range of byte  as derived is: -27 to (27-1)      OR  -128 to 127

Analyse the above in powers of 2. You will find that for 8 bits it comes to power 7.

short:
  size=2 bytes= 16 bits

 1(signed bit)   1    1    1    1    1    1    1    1    1    1    1    1    1    1    1  
       15             14  13   12  11   10   9    8     7    6     5    4     3    2     1     0

max value:

 0(signed bit)   1    1    1    1    1    1    1    1    1    1    1    1    1    1    1  
      15             14  13   12  11   10   9    8     7    6     5    4     3    2     1     0

Calculated value:
      =>        20+21+22+.............+214 {Geometric progression formulla a(rn+1-1)/(r-1)}
here a=1,r=2,n=14
     => 1+21+22+......214
     => (215-1)

Min Value:
 1(signed bit)   0    0    0    0    0    0    0    0    0    0    0    0    0    0    0  
       15             14  13   12  11   10   9    8     7    6     5    4     3    2     1     0

calculated value:   -215

Range: -215 to (215-1)      OR -32768 to 32767

Here 2 bytes=16bits -----> Range in power of 15

char:
Don't consider the signed bit here as characters cannot be -ve.
Java follows unicode characters,and cannot be placed in 1 byte as 1 byte can contain a max of 255 chars.

max value:

 1   1    1    1    1    1    1    1    1    1    1    1    1    1    1    1  
           15   14  13   12   11  10   9    8     7    6     5    4     3    2     1     0
All 1's
   => 20 +21+......+215
   => (216-1)
   => 65535


Min Value:  all 0's
    => 0
Range: 0 to 65535

We can say,char supports a max of 65535 characters.

int:
 4 bytes=32 bits
See the above derivation for short,byte and you will be able to write the range of integers.
The range comes  -231 to (231-1)

long:
8 bytes=64 bits
Range:   -263  to (263-1)

From the above discussion,it is clear,we don't have to learn these ranges,just calculate the number of bits and you will be able to write the range directly without any calculation.

float(Calculation for float range)
It follows IEEE 754 standard.
According to which 1 bit is fixed for sign. 23 for Mantissa and 8 bits for Exponent


 1(signed bit){s}  <------23 bits(Mantissa)--------> {M}   <----8 bits(Exponent)--->{E}  

Formula for Calculation of Value is:
         =>  (-1)s * M* BE
where base B is fix by designer.

Mantissa is assumed to be in decimal i.e its value start from 1.{caculated value}
M=1.{calculated Mantissa value}

Assume,all mantissa bits to be 1:
Calculated value is:   2-1+2-2+.....+2-23  = 1-2-23=    0.999999880790071044921
Mantissa is: 1.999999880790071044921
Exponent is stored in excess 127 format. E is 8 bits. No signed bit in exponent,calculated range is 0 to 255.
Actual range is calculated by subtracting 127 from stored exponent value. Values 0,255 are fixed for special purpose. As M=1.{some value}, it can never be 0, and also the number cannot be 0. For this reason 0,255 were fixed for special purpose, which otherwise are not possible. Following are the special cases.
a) If M=0,E=0    => Number is 0
b) If  M=0, E=255   => Number is +ve Infinity
c) If M={any value} ,E=0 => Number is -ve Infinity
d) If M={any value}, E=255  => NaN(Not a Number)
Actual range of Exponent(subtracting 127 and not taking special case value): -126 to 127

Max Value(positive)= 1.999999880790071044921 * 2 127
=>3.40282346638528860e+38  (calculated  from calculator)

Min Value  : -ve of Max Value

Float range : (-1.999999880790071044921 * 2 127)    to 1.999999880790071044921 * 2 127
OR  -3.40282346638528860e+38    to     3.40282346638528860e+38

Minimum positive value= 1* M * 2 -126
 => 1.75494e-38 (M=1 for making it the smallest possible positive number,which means all the mantissa bits are 0)

The positive values range from 1.75494e-38 to 3.40282346638528860e+38
Range Magnitude Minimum(Denormalized) 
  { Here,they take min value as 2E(min)-no. of mantissa digits}
=1* 2-126-23
=>  2-149
=> 1.4012 e -45


We can say that min positive number is 1.4012 e -45.
Positive number range is 1.4012 e -45  to   3.40282346638528860e+38     -->(1)
Similarly maximum -ve number possible is (-1.4012 e -45)
Similarly,-ve number range is (-3.40282346638528860e+38) to (-1.4012 e -45)      --->(2)

The derivations (1) and (2) are the ranges which you will find in text books.This denotes the minimum possible positive number and also the minimum possible difference between 2 numbers. Similarly we can calculate the maximum possible difference by calculating the difference between the largest possible number and the 2nd highest possible number.

double:
8bytes=64 bits
Mantissa - 52 bits
Signed -1 bit
Exponent- 11 bits

Same as the above method,we can calculate the positive and negative range for double.
I have not calculated,i am writing the book range
 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).
But the method is similar.
Please calculate once.


TYPE CONVERSION
Now think type conversion in simple terms.
If we try to save information from more number of bits space to a smaller number of bits space. 
Is this possible?? Ask the question to yourself.
Not satisfied.
Try to save 111111111111111 in 8 bits. It is obvious that some bits will be lost.
Think the other way around try to save 8 bits in 16 bits. It is easily possible without loss of information.

To avoid this problem the java designers thought,the loss of information should not happen. They fixed ,you cannot convert from higher to lower but lower to higher is allowed.
But in explicit it is possible to convert from higher to lower,which means that you agree for loss of information.

Below you find the lowest to highest (one way)
byte<short,char<int<long<float<double.

You will find that  long(8 bytes) and float(4 bytes).
Now you will ask how is it possible.
See the highest value possible for long which is (263-1) while the highest possible value for float which is 1.999999880790071044921 * 2 127 
which is much more than max possible value of long. There will be loss of precision but the value can be stored in that.
This is how the type conversion works.

EXAMPLES
Now we will see few examples and try to learn the type casting.
=> In java,default decimal values are treated as double.

1)int a=10.1;

The above code will give Compile time error.. Why??
RHS is of type double and LHS is of type int. So we are trying to save big in a small one.Is it possible?
No Bigger data type to  smaller data type is not possible.

2) float b=7.2

The above code will give Compile time error.. Why??
RHS is double and LHS is float. Bigger data type to  smaller data type is not possible.

3) byte b=10,c=20;
byte d=b+c;

The above code will give Compile time error.. Why??
The reason behind this is that the arithmetic calculations are treated as integers in java.
So b+c is an integer and this integer value we are saving in byte which is not possible
so correct way is byte d=(byte)(b+c)


3 comments:

  1. vinkal vishnoi ...u r great :):)

    ReplyDelete
  2. It's Really very good & Helpful information. Here also a data types description about java data types represents with diagram.

    ReplyDelete