Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Maximum digits allowed for each literal

vijaya vinayagam
Greenhorn
Posts: 21
Hi,
is there any limit for the number of digits to be used while assiging values to the variables ??

Eg.

int a = 564646464646535336667447774477

let me know that for each literal and for each way of representing that like (octal,hexa)

and give an examplefor that tooooo

bye

Jeff Albertson
Ranch Hand
Posts: 1780
Originally posted by vijaya vinayagam:
Hi,
is there any limit for the number of digits to be used while assiging values to the variables ??

Eg.

int a = 564646464646535336667447774477

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?

Stuart Ash
Ranch Hand
Posts: 637
Originally posted by Jeff Albrechtsen:

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.

Ranch Hand
Posts: 209
This is a table of Data Types with their range of values:

Type Value

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.

Jeff Albertson
Ranch Hand
Posts: 1780
Don't forget the octal and hex notations!

Ilja Preuss
author
Sheriff
Posts: 14112
Why does this feel like a homework question to me?

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Try google for something like "java primitive data ranges" ... with the quotes.

Steve Fahlbusch
Bartender
Posts: 605
7

byte 3
short 5
int 10
long 19
float 7
double 15
char 1
boolean N/A

conversion to binary, octal and hex are left as a further assignment.

Tom Sullivan
Ranch Hand
Posts: 72
This kind of reminds me of Dr. Evil asking the president of the United States for more money than had been printed at the time.

marc weber
Sheriff
Posts: 11343
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.

NUMERIC INTEGRALS...
byte: 8 bits
short: 16 bits
int: 32 bits
long: 64 bits

NUMERIC FLOATING POINT...
float: 32 bits
double: 64 bits

SPECIAL CASES...
char: 16 bits
boolean: 1 bit

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

0 => 00000000
1 => 00000001
2 => 00000010
3 => 00000011
...
126 => 01111110
127 => 01111111

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

-128 => 10000000
-127 => 10000001
-126 => 10000010
...
-2 => 11111110
-1 => 11111111

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.