This week's giveaway is in the Android forum. We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line! See this thread for details.

Here's a suggestion that will serve you well in Java, and life: try it out! What happens when you write that line in a program?

While you can't try out what happens if you build the top storeys of a building without building the foundation, you can certainly try out most things in software. Thus, there should be no reason why one should not try out such stuff and then come here to discuss the whys and wherefores.

Dear Readers, This is a table of Data Types with their range of values:

TypeValue

byte -128 to 127 short -32,768 to 32,767 int -2,147,483,648 to 2,147,483,647 long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float -3.402823e38 to 3.402823e38 double -1.79769313486232e308 to 1.79769313486232e308 char Symbols used in text eg. 'A' boolean True or False

I would appreciate it if my mistakes are corrected.

Why does this feel like a homework question to me?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

Try google for something like "java primitive data ranges" ... with the quotes.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

Well, here's a recap of what your textbook should be telling you.

As Shyam's post indicates, range limits are not simply a "number of digits." Instead, the first thing to consider is the amount of memory that each type is allotted.

Once you know this, the next step is to understand how values are represented in binary for each type.

In Java, all numeric primitive types are signed, meaning that they can represent both positive and negative values. The first bit is sometimes called the "sign bit." If this is 1, then the value is negative. If it's 0, then the value is non-negative.

Let's consider byte as an example. A byte is 8 bits, and 8 zeros represents a value of zero: 00000000. The positive values are straightforward binary...

For negative values, consider what -1 should look like in binary. When we add +1 to it, we should get zero. So -1 is represented by all ones: 11111111. (Thus, -1 + 1 is 11111111 + 00000001 = 00000000.)

In general, "increasing" the binary representation of a negative value moves that value towards zero. So the minimum negative value is a one followed by all zeros. In the case of byte, this is 10000000, representing -128...

Therefore, the range of an 8-bit byte is -128 to 127, which is -2^7 to (-2^7) - 1.

Notice that the range of positive values is one less than the range of negative values because zero is non-negative (having a leading bit of of 0).

Ranges for the other numeric integral types follow this same pattern.

Floating-point values are stored quite differently, according to a standard called "IEEE 754." This is a topic unto itself (best addressed by searching the web for "IEEE 754"), but some key points are as follows. As with the integral numerics, floating-point numerics use the leading bit to indicate the sign. The remaining bits store the value in a scientific notation format, with an "exponent" value and a "mantissa" value. This provides for tremendous range with a "sliding window" of precision. For this reason, floating-point values should be considered approximations. (See this thread for a more detailed illustration, this page for a nice explanation, and bookmark the essential, Some things you should know about floating-point arithmetic.)

A 16-bit char is much like an integral primitive, except that its values are all non-negative. Thus, the numeric range for a char is 0 to (2^16) - 1. How these values translate to symbols is another topic. (For most common characters, see this ACSII table.)

Finally, a single-bit boolean is restricted to values of true or false.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org