Sharing Thoughts

This weblog will have interesting topics in it.

  • Blog Stats

    • 93,155 hits
  • Archives

  • Top Clicks

  • Advertisements
  • Categories

  • Top Posts

  • CopyRight

    © Sriram and sriramchewsthefat, 2009. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Sriram and sriramchewsthefat with appropriate and specific direction to the original content.

How the Range and Values of Primitive Data Types in java(or c or c++) have been calculated?

Posted by sriramchewsthefat on June 25, 2009

Please note: The original url for this post is:
If you are reading this on any other web site, this content is stolen.

Hi, im just going to explain that how the Range of the primitive data types have been calculated by giving one simple example.
Primitive data types in java programming languages are: boolean,byte,short,int,long,float,double,char.

Here im going to explain  that how the Range has been calculated for this data types.The calculation of Range and Values is same in Java,C,etc

Let us get into the topic,im just going to explain how many values that byte data type is having,
Bit consists of 0’s and 1’s.byte normally consists of 8 the values can be calculated using the general formula which is given below,
no of values data type can have=2^n(2 power n),where n represents no of bits.
so the value of byte data type=2^8(i.e 1 byte=8 bits),here n=8
byte value=256
so byte is having 256 values in it.this calculation is same for finding the values of all integer data types.

Now we came to know that how the values of the integer data types have been calculated,before going into the explanation about the range of data types we will see one small java program based on byte data type which will be useful for your understanding,

Java coding:
/*example for byte data type*/

public class Test {
public static void main(String args[])
byte b=128;
System.out.println(“the o/p is” +b);

So can u guess the o/p of the above program,most of them who are new to programming language will thought that the above coding works fine!!
But its not the case, java while compiling will throw the following error:
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to byte
Question Arises

Now the real question arises,how can the assigned value will be int because we have 256 values in byte data type,why java is showing type mismatch?

The answer to the above question is the Range of the byte data types is not having this intialised value.
Range of the data types is the one which will show both the negative values as well as positive values of the data types.
so the range of byte is -128 to overall it is having 256 values.Now you are getting the point that , java throws type mismatch exception because the value 128 will come under short and int data type.

In the Range,the number of negative values of the data types is same as that of positive values.In byte data type 0(zero) will come under +ve value,so there are 128 -ve values(from -1 to -128) and 128 +ve values (from 0 to 127).

General calculation of the Range for integer Data types:
Take simple integer data type for our example. We know byte consists of 8 bits,so in general n is going to represent the number of bits

The Range calculation is in general -2^(n-1) to (2^(n-1))-1
Am i confusing?

its easy to calculate,put n=8 and see whether we got the same Range i mentioned for byte data type earlier
when n=8,the general equation of the Range will be

=>-2^(8-1) to (2^(8-1))-1
=>-2^(7) to (2^(7))-1
=>-128   to 127

<=>Range of byte
so now we came to know that how we are calculating the Range of the  integer data types.This logic is applicable for all the integer data types.

The full details of all the data types are given below,

S.NO           Data Type        Bits               Ranges                                           values

1                   boolean             1                —                                            true or false(1 or 0)

2                   byte                    8             -128 to 127.                                    256(2^8)

3                   short                 16         -32,768 to 32,767                       65,536(2^16)

4                   int                      32           -2^31 to (2^31)-1                           2^32

5                   long                   64              Refer NOTE                                     2^64

6                   float                   32               Refer NOTE                                   ———

7                  double               64               Refer NOTE                                  ———-

8                   char                16              0 to 65,535                                     65,536(2^16)


1. 1 byte= 8 bits

2.Values general calculation formula:

2^n ( 2 power n),where n represents no of bits.

3.Range calculation:

-2^(n-1) to (2^(n-1))-1,where n represents no of bits.

4.Range of:

a.long=-9,223,372,036,854,775,808     to       +9,223,372,036,854,775,807

b.  float=     1.40129846432481707e-45   to


c.  double =  4.94065645841246544e-324d to

1.79769313486231570e+308d^31 to (2^31)-1=-2,147,483,648 to 2,147,483,647

I hope this post is interesting ,feel free to comment your thoughts.i will come up with different post next time,thanks for reading this post.


5 Responses to “How the Range and Values of Primitive Data Types in java(or c or c++) have been calculated?”

  1. bigendian said

    I posted an answer to your question on my blog. You can complain about your post being stolen to the host company.
    Big Endian

  2. flash said


  3. kiran said

    Was really Informative.. thanx

  4. IRFAN KHAN said

    nice code and calculations

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: