aspose file tools*
The moose likes Beginning Java and the fly likes Int size and double size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Int size and double size" Watch "Int size and double size" New topic
Author

Int size and double size

Conrad Kirby
Ranch Hand

Joined: Jun 17, 2001
Posts: 178
Programming languages always get me wondering why they are written how the are. You know how the size of a string is pretty much unbounded? Well how come they couldn't make a special type of int that is unbounded? Recently I've been dealing with huge integers that I can only deal with using strings, but strings are sized a byte per character. So if I had a string holding the value "123457890123567890" that would take up twenty bytes. An int that is sized to hold 20 bytes can hold a number 46 digits long! So what I'm saying is that if you could have int type that you could mess with the size, when you get up to really large numbers it would be much more efficient. THe same goes for doubles. Any reasons why this isn't so?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Originally posted by Conrad Kirby:
Any reasons why this isn't so?

Because, there are practical limits to what you can do with hardware. Until we have organic computers that have unlimited resources that they can tap at will, allowing them to handle any size of input, there will always be a limit to what these machines can do. Take heart though, if Moore's Law holds true, mankind should see the kind of machine you are talking about sooner or later (not that we'd still be around to see it happen ) In the meantime, you'll just have to make do with BigDecimal and BigInteger.

[This message has been edited by JUNILU LACAR (edited June 22, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Conrad,
You should have a look at java.math.BigInteger!
Regards,
Manfred.
Conrad Kirby
Ranch Hand

Joined: Jun 17, 2001
Posts: 178
I know there are limits to memory in a computer. I was only wondering why ints have a specific size and strings don't. A string can be anywhere from 1 byte to a pretty big number of bytes. However, ints are set to 32-bit or 4 bytes. If ints could have a varied size, then you could hold some pretty huge numbers. Do you get what I'm saying?
As for Moore's Law, he's the guy who came up with quantum theory right? Just for your info, there are quantum computers out right now, but they are very unpowerful (they work with about 3 atoms). However, I find it hard to believe that particles can have two states simultaneously. But on the same note, its pretty hard to comprehend 4-deminsions, yet people are starting to learn quite a bit about it. .
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Originally posted by Conrad Kirby:
I know there are limits to memory in a computer. I was only wondering why ints have a specific size and strings don't. A string can be anywhere from 1 byte to a pretty big number of bytes. However, ints are set to 32-bit or 4 bytes. If ints could have a varied size, then you could hold some pretty huge numbers. Do you get what I'm saying?

I know what you're saying, Conrad. And I'm saying that hardware ultimately limits what kind of number processing you can do. Strings and numbers are treated differently because they are processed differently. Adding (concatenating) a string to another involves different low-level processes than does adding two numbers.
I don't know how much Moore had to do with quantum theory, if at all, but Moore's law predicts that computing power will double every 18-24 months. That's why we have seen computer architectures go from 8-bit to 16-bit to 32-bit to 64-bit and so on. And the underlying computer architecture defines the practical limits for handling numbers.

[This message has been edited by JUNILU LACAR (edited June 22, 2001).]
Conrad Kirby
Ranch Hand

Joined: Jun 17, 2001
Posts: 178
I think pretty soon we'll be seeing analog computers that aren't limited to processing in binary.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The reason that strings are unlimited and numbers are not deals with the way they are stored. Strings are stored as an array ( or some other structure ) of characters with a delimiting character at the end( This is the null character... usually represented in C/C++ by \0... not sure if Java uses this same representation internally... ), which means that a string can take up as much room as it wants. Each character is read until it reaches the end.

Integers on the other hand, take up a pre-determined amount of space. This is also why we talk of integers or longs being 32-bit or 64-bit... this is the number of bits each number takes up. This is done due to the way computers do calculations on these values. A computer has to know an integer is 32 bits to do an addition like this :


-Nate

[This message has been edited by Nathan Pruett (edited June 22, 2001).]


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Int size and double size
 
Similar Threads
An object to hold several arrays
4081 bytes too big for weblogic and oracle?
Special character handling
maximum length of String = 32k ?
Maximum capacity of arrayList for String objects is one million?