File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes Employing Primitives � Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Employing Primitives � " Watch "Employing Primitives � " New topic
Author

Employing Primitives �

John Abong
Ranch Hand

Joined: May 14, 2007
Posts: 79
I�m struggling with primitive data-type selection. I�ve researched numerous posts regarding optimization and, as a result, just purchased a copy of Practical Java by Peter Haggar. My immediate question is this, though:

While in this early learning stage, 4b(Say), should I focus solely on readability when choosing between using int, long, String, etc.?

I don�t know enough yet to make optimization-based choices so I would assume readability, but logic tells me otherwise.

John Abong
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi John,

While in this early learning stage, 4b(Say), should I focus solely on readability when choosing between using int, long, String, etc.?


For Cattle Drive purposes, you can be pretty sure that readability generally has a higher priority than optimization.

Could you maybe be a little more specific about the trouble with choosing types?
[ November 05, 2007: Message edited by: Pauline McNamara ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
My recommendation is: Make your code simple and easy to understand now. If you can gain efficiency without sacrificing readability, then take it. Readability and ease of use is far more important these days with the speed of processors and other hardware. If you get the readability right, the optimization you may do later will be much, much easier. And frequently you will find that by simplifying your code, it also runs faster -- a nice side effect.

I have found that primitives are one of the least hindrances to efficiency. Objects seem to be much more frequently involved in slowing things down.

Unless you're planning to go into writing computer games, chances are you'll rarely, if ever, get to the level where you have to worry about optimizing primitives.

Have you read these programming pearls?


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

Joined: May 14, 2007
Posts: 79
Thank you for you quick responses!!!

I�ve hit another wall, so even formulating this post is a struggle because I don�t know where my weakness lies; syntax, plain old inexperience, or both.

So, in answer to your question, Pauline,
Could you maybe be a little more specific about the trouble with choosing types?
, I�m afraid to say, no. Maybe explaining my personal approach (style) will help.

At first, I try to employ the concept of �build the four inch telescope, then six inch telescope�. In theory, I �get� the idea of sequence, decision, loop. In practice, my sequences, decisions and loops seem to be one incomprehensible unit. I want to keep it simple but it doesn�t always come out that way.

Then, as I try to refactor the incomprehensible unit, I lose my original focus and get lost in trying to regain control instead of writing simple, readable code to solve the problem. That�s when I tend to �fish� for the right token(s), primitives in this case, and the ever illusive perfect structure, to save myself.

At this point I get confused about whether to use string manipulation versus mathematical computation, loops versus hard-code, do it in main() versus do it in a method, return a value or don�t return a value, int versus long, etc. I know I�ll pull it all together at some point, but right now I�m struggling with how to untangle it all.

When I fix computers, my side gig, the symptoms guide my choice of tools. The computer was working, now it�s not, so what steps do I have to take to get it back to the known good state. I know what belongs and what does not belong when I look at a computer. I know in what order to do things.

Here, I feel like I�m doing this programming backwards; fixing it from an unknown point, to an unknown point, instead of writing it to a known good state in an orderly fashion. But I don�t know what belongs (yet) so fixing it is a struggle. It�s a catch-22.

I don�t know if I�m confused or just whining . I don�t want explicit answers. That won�t teach me anything. Hat�s off to the nitpickers!

John Abong
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by john abong:
At this point I get confused about whether to use string manipulation versus mathematical computation, loops versus hard-code, do it in main() versus do it in a method, return a value or don�t return a value, int versus long, etc.

In general, working with primitives (usually numbers) is much faster, and frequently easier, than working with Strings and substrings (objects).

If you are doing the same set of steps more than once, there is a good chance your nitpicker will smell the need for a method, especially in the Say assignment where we are concentrating on methods.

Whether or not to return a value depends on the situation.

This is slightly different than "fixing" your program. You already have a program that runs (hopefully giving the correct results). You are just trying to make it more simple and readable. There are many paths to this result and many acceptable solutions to this assignment (besides the instructor's solution) ... so it can be very challenging for the nitpicker to guide you just as it is for the student to understand how to get to an acceptable solution. Patience and perserverence all around.
John Abong
Ranch Hand

Joined: May 14, 2007
Posts: 79
You already have a program that runs (hopefully giving the correct results). You are just trying to make it more simple and readable.


Ahhh, yes...Now that clears things up, giving me back my focus. Let's see what I can do with it. My nitpicker is doing an outstanding job! I'm just having a block-head moment.

You (all) rock!

John Abong
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
Originally posted by john abong:
My nitpicker is doing an outstanding job!


Flattery isn't going to help you, John

Today I think I reached the pinnacle of my creativity, with a programming simile that involves a pasta making machine.

... Mmmm... pasta...
John Abong
Ranch Hand

Joined: May 14, 2007
Posts: 79
You'll never guess what was waiting for me for chow tonight. Yup, a heapin' bowl of pasta!
Dick Summerfield
Ranch Hand

Joined: Oct 04, 2007
Posts: 90
Originally posted by john abong:
Ahhh, yes...Now that clears things up, giving me back my focus. Let's see what I can do with it...
John Abong


I see what you did was obviously right John. Congrats on roping 4b!

Dick.
John Abong
Ranch Hand

Joined: May 14, 2007
Posts: 79
Thank you Dick!

I think the sharing of ideas in this forum is critical to our success and the success of Cattle Drive. It's why I put the books away (for now). I feel even more connected now that I'm a "Ranch Hand".

The other benefit that I see is the aspect of interpersonal networking and knowing that there are real people behind all of this, which you don't get with a book. It adds an emotional element, much like a support group.

I'll miss this place if I [ever] get out...

John Abong
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
John, congrats on becoming a ranch hand!

I really like that part of the Cattle Drive too, the fact that there are other people doing it too, all pulling together to get it.

Didn't they tell you when you signed up that the Cattle Drive is very much like the Hotel California... you can check out but you can never leave?
Dick Summerfield
Ranch Hand

Joined: Oct 04, 2007
Posts: 90
Originally posted by john abong:
...knowing that there are real people behind all of this, which you don't get with a book.
I'll miss this place if I [ever] get out...


Same reason I decided to join the Cattle Drive, John. The book I was using promised website support but in fact didn't .

I found this thread on Java primitives interesting, by the way, as I'd been taking a sneak peek at Java-4b and although I got some ideas, I still have enough questions. But that's for later - I'm still anxiuosly awaiting feedback on 4a!

Re. getting out: that bit about "Hotel California" is worrying, Pauline!
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
The reason I joined the cattle drive, was the <em>code review</em>... telling me how to make my code nicer, cleaner, better, and finding other ways to do the same thing, but better.

The forum was a nice plus that I hadn't really thought about before-hand.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Employing Primitives �
 
Similar Threads
Data Structures: LinkedList
Finally able to pay up and join!
Writing efficient Java
log4j vs JDK logging
Why does 1b seem so freakin tricky to me.