aspose file tools*
The moose likes Cattle Drive and the fly likes Gnawing on Grains Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Gnawing on Grains" Watch "Gnawing on Grains" New topic
Author

Gnawing on Grains

Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Waiting around for nitpicks on Say(a), I figured I'd gnaw on some Grains for a while. I'm worrying about int and long.
A long is (just!) big enough for the job, but I wonder whether I should use a long throughout the program. I got a strong vibe from the way the Grains assignment's purpose was worded that I should use an int for as long as I can. So: is it that using a long when an int will do at any point in a program is inherently wasteful? One can always switch to a long when an int will no longer do, but writing that into the program makes for more code to compile and run, and in some circumstances it has to be better to waste a little memory in the interests of leaner code.
Relatively speaking, it seems that 256 bytes isn't very much memory to waste. But maybe it's quite a lot for a program with only 30-odd lines of code. Or maybe there's some related rationale. In any event, I don't know which principles should guide me here, so any enlightenment would be welcome.

[This message has been edited by Adam Vinueza (edited May 26, 2001).]
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Adam the objective of Grains is to find a class in the Java standard library that can handle(accomodate) the such a large amount.
Another thing are you sure LONG is big enough for the total
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
>is it that using a long when an int will do at any point in a
>program is inherently wasteful? One can always switch to a long
>when an int will no longer do, but writing that into the program
>makes for more code to compile and run, and in some
>circumstances it has to be better to waste a little memory in
>the interests of leaner code.

Right, Adam. Readability is more important than saving a little memory in this day and age. My recommendation is: Make your code simple and easy to understand now. If you can gain efficiency without sacrificing readability, then take it.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Johannes: Dang and blast! Didn't notice that pesky digit, even though it and the right one don't look at all alike. That's what I get for rushing. Oh well, I can now see what I have to do. Thanks!
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Btw, I just noticed that I have been silently "promoted" to ranch hand. Didn't know I'd posted that much. How time flies!
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
How does one get promoted to ranch hand?
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Hey, when did that happen?
I didn't even notice! Cool! Now I feel like I've really accomplished something!
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
When you post more than 30 times .
If you post more than 100 the powers start watching you a bit more closely to see if you might be bartender material. So the next promotion is on invitation only. Some never make it, others like me get lucky because they keep a log
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
OK, so suppose I've found this class--call it theRightClass--and I'm happily writing my Grains program. I know from painful and embarrassing experience that I won't need it until very late in the loop; accordingly, it seems a waste to use it for every iteration. No doubt, as Marilyn pointed out, memory is pretty easy to come by nowadays; so for the sake of dead simple code I'd prefer to just use theRightClass throughout. But in thinking about what to do if I worked for a memory miser, I got to wondering about how to proceed.
It's easy enough to explain why one needs theRightClass; so there are now (at least--at most?) two ways to go at this point.
Way #1: use a conditional to check to see if the relevant condition is met, and use theRightClass precisely when that condition is met.
Way #2: use two loops (or, if a second loop is unnecessary, use a single statement).
The advantage of #1 is that there's only one loop, but it requires the program to check to see if the condition is met, and we already know it won't be met until very late. The advantage of #2 is that it doesn't require the program to run this check. But I don't know how significant it is that a program has to check for this condition: does it have to run this check at every iteration, or can it simply set up an alarm for the condition and do its thing until the alarm goes off? If it's the former, #2 is clearly the way to go; if it's the latter, I'd prefer #1.
Can anyone offer enlightment here?
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Adam, I didn't even think for more than about 5 seconds of treading where you are thinking of treading. I just used theRightClass for each iteration of the loop. That was complicated enough for me, as I ran into what seemed like simple math problems with theRightClass, and had to ask for assistance to solve my dilemma. And I haven't even submitted it for evaluation yet!
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Adam I cant help but think that you are sacrificin simplicity ( = maintainabilty ) for the sake of efficiancy.
And Carol I saw your attempt remember, stop knocking yourself, you have a very nice solution, you are doing great.
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Johannes,
Message received. I'll keep things simple.
Carol, there's stuff in the JCLs that really helps with doing the math for theRightClass, if you're having a problem with that. It's buried a bit, but it's there.

[This message has been edited by Adam Vinueza (edited June 05, 2001).]
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Btw Carol,
I've been out of town for a bit and away from computers, so I didn't notice that in the interim you'd passed 4b and are now up to Grains. Congratulations! Wooo! (Imagine the wave being done here.)
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi all.
Adam, does JCL mean Java Class Libraries, as in the book(s)? Would the same (or equally helpful) information also be found in the Java API?
Pauline
[This message has been edited by Pauline McNamara (edited June 07, 2001).]
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
I have the first volume of the Java Class Libraries book. But the same info, plus much more, is on-line as the 1.2.2 API Specification.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Thanks!
 
 
subject: Gnawing on Grains