AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Starting a counting program from a high number

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Discussions
View previous topic :: View next topic  
Author Message
Peter
Guest





PostPosted: Sun Apr 15, 2007 7:09 pm    Post subject: Starting a counting program from a high number Reply with quote



I wanted to find out how high my computer will count (by increments of
'1'). Since counting by '1' takes an extended amount of time, I thought
I'd start my program at a high number.

The program is capable of counting higher than 2^33, however, if I tell
the program to start at a number of 2^33 it gives me an error.

A long can count up to 2^64 but the compiler wont allow me to start
counting at that high of a number.


Does anyone know why?


Here is a copy of my program:

/*
*
*This program will count
*
*/


class count {

public static void main (String args[]){

long count; //specifies count size

for (count=2147597261; ; count++){ // increments counter by 1

System.out.println("\t\t The count is: " + count); // outputs count


}

}

}
Back to top
Lew
Guest





PostPosted: Sun Apr 15, 2007 7:16 pm    Post subject: Re: Starting a counting program from a high number Reply with quote



Peter wrote:
Quote:
I wanted to find out how high my computer will count (by increments of

It'll count ints all the way up to
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALUE>.

It'll count longs all the way up to
<http://java.sun.com/javase/6/docs/api/java/lang/Long.html#MAX_VALUE>.

Quote:
'1'). Since counting by '1' takes an extended amount of time, I thought
I'd start my program at a high number.

The program is capable of counting higher than 2^33, however, if I tell

2 to the 33rd is too large for an int. It needs to be stored in a long.

Quote:
the program to start at a number of 2^33 it gives me an error.

A long can count up to 2^64 but the compiler wont allow me to start
counting at that high of a number.

2 to the 64th is too large for a long. It needs to be stored in a BigInteger.

Quote:
class count {

Should be named "Count".

Quote:
public static void main (String args[]){

long count; //specifies count size

for (count=2147597261; ; count++){ // increments counter by 1

That last character of the count initializer is the letter "L", is it not?
You should use the upper-case "L" as recommened by the JLS, since "1" and "l"
can be hard to tell apart.

You could declare the long count in the for() block, and the comment that the
loop increments the counter is superfluous.

--
Lew
Back to top
Peter
Guest





PostPosted: Sun Apr 15, 2007 8:03 pm    Post subject: Re: Starting a counting program from a high number Reply with quote



Lew <lew (AT) nospam (DOT) lewscanon.com> wrote in news:Us2dnY3-
WdtJrr_bnZ2dnUVZ_s2vnZ2d (AT) comcast (DOT) com:

Quote:
http://java.sun.com/javase/6/docs/api/java/lang/Long.html#MAX_VALUE

I made a slight error. I am aware a 'long' is 2^63-1.

I'm sure Java is capable of counting to its specified number of 2^63-1,
however, the compiler gives me an error if I start my count at a high
number.

Not sure where you're seeing a 'L'.

Did you try compiling the program I listed and seeing if you get a reported
error for that number being too long?


Thanks in advance!
Back to top
Lew
Guest





PostPosted: Sun Apr 15, 2007 8:58 pm    Post subject: Re: Starting a counting program from a high number Reply with quote

Peter wrote:
Quote:
Not sure where you're seeing a 'L'.

That is my whole point. I wasn't.

I didn't say I was seeing an 'L', I asked if I was seeing an 'L':
Quote:
That last character of the count initializer is the letter "L", is it not?

referencing your sample code:
Quote:
for (count=2147597261

since I am unable to distinguish the lower-case 'l' from the digit '1' in my
screen font.

I gather from your non-answer to my question that the last character of that
constant is the digit '1'. That is the problem. That is why I drew your
attention to the 'L' suffix. You didn't put in the 'L'.

I asked you the question about the 'L' to get you to pay attention to whether
there was a missing 'L', and to compare the number to
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALUE>.

No 'L'. Merry Christmas.

I already told you
Quote:
It'll count ints all the way up to
http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALUE>.

and that is in reference to 2147597261. (No 'L'. Merry Christmas.) Which is
larger?

Quote:
Did you try compiling the program I listed and seeing if you get a reported
error for that number being too long?

Of course not. Did you? What was the error? Why don't you just tell us what
it was instead of making us do all the work? Anyway, I already knew what the
error is, and I already told you where to find the answer in my first response.

If you look at the error message you will see that it tells you precisely the
problem.

Just to repeat:
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALUE>.

And to explain:
<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.1>

--
Lew
Back to top
Peter
Guest





PostPosted: Sun Apr 15, 2007 9:36 pm    Post subject: Re: Starting a counting program from a high number Reply with quote

Lew <lew (AT) nospam (DOT) lewscanon.com> wrote in
news:B4ydnbaYps4k1r_bnZ2dnUVZ_hCdnZ2d (AT) comcast (DOT) com:

Quote:
Peter wrote:
Not sure where you're seeing a 'L'.

That is my whole point. I wasn't.

I didn't say I was seeing an 'L', I asked if I was seeing an 'L':
That last character of the count initializer is the letter "L", is it
not?

referencing your sample code:
for (count=2147597261

since I am unable to distinguish the lower-case 'l' from the digit '1'
in my screen font.

I gather from your non-answer to my question that the last character
of that constant is the digit '1'. That is the problem. That is why
I drew your attention to the 'L' suffix. You didn't put in the 'L'.

I asked you the question about the 'L' to get you to pay attention to
whether there was a missing 'L', and to compare the number to
http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALU
E
.

No 'L'. Merry Christmas.

I already told you
It'll count ints all the way up to

http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALU
E>.

and that is in reference to 2147597261. (No 'L'. Merry Christmas.)
Which is larger?

Did you try compiling the program I listed and seeing if you get a
reported error for that number being too long?

Of course not. Did you? What was the error? Why don't you just tell
us what it was instead of making us do all the work? Anyway, I
already knew what the error is, and I already told you where to find
the answer in my first response.

If you look at the error message you will see that it tells you
precisely the problem.

Just to repeat:
http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALU
E
.

And to explain:
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#
3.1
0.1


I was not aware an 'L' needs to be inserted. I thought declaring it's a
'long' is all the compiler needs.

Maybe I'm not deep enough in my Java learning stages to have known about
the 'L'?

Thanks again!
Back to top
Lew
Guest





PostPosted: Sun Apr 15, 2007 9:41 pm    Post subject: Re: Starting a counting program from a high number Reply with quote

Peter wrote:
Quote:
I was not aware an 'L' needs to be inserted. I thought declaring it's a
'long' is all the compiler needs.

The declaration only applies to the variable, not the literal.

--
Lew
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Discussions All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.