File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes  Maximum digits allowed for each literal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Maximum digits allowed for each literal" Watch " Maximum digits allowed for each literal" New topic
Author

Maximum digits allowed for each literal

vijaya vinayagam
Greenhorn

Joined: Dec 09, 2005
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

thanks in advance
bye
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
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?


There is no emoticon for what I am feeling!
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
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.


ASCII silly question, Get a silly ANSI.
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
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.


With Best Regards,
Shyam Prasad Murarka
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Don't forget the octal and hex notations!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 570
    
    7

As i read the question, here is how i would answer....

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

Joined: Dec 20, 2005
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

Joined: Aug 31, 2004
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.


"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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Maximum digits allowed for each literal