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

Assignment 1a

Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
A nitpick on my assignment 1a had it that I was using 100 string concatenations and 100 array dereferences. The string concatenations, no doubt, came from my not creating a separate string containing the concatenation of the name with a space. (I used a FOR loop to print a concatenation of argument[ 0 ] with a space. It seemed the most straightforward thing to do, but I see it's not the most efficient thing to do.) But I'm a bit flummoxed by the 100 array dereferences nitpick. If I've created a separate string, and used a FOR loop to print the string 100 times, don't I have 100 array dereferences anyway? I think this comes from using a String object, but as I've just started learning Java and have scant background in programming, I'm not sure. I've seen references in the Cattle Drive Java College discussion to a thing called StringBuffer, and sense this might help, but I don't see anything in Just Java 2 about it, so I can't check there.
Any help with my ignorance and confusion would be greatly appreciated.
Adam
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Adam,
If you do this:

You are only accessing the array once.
If you do this:

You are accessing the array 1000 times. Accessing an array is more overhead than just accessing a varialbe.
Hope that helps
Bill
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Let me see if I understand fully. The problem was that in writing
for ( int i = 0; i < 100; i++ )
{
System.out.println( args[ 0 ] + " " )
}
I was accessing the array *args* 100 times, hence had to dereference 100 times. When I create a separate object to which args[ 0 ] is assigned, I am only accessing the args array once. So I was confused about FOR loops and arrays.
Is this right?
Thanks for the lucid reply.
Adam
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
bill nitpicked my assignment 1a and i used the style he lists above (the first one). mine passed on the first try.
you are right with your evaluation. the code you have has to access the array on each iteration of the loop. it would be better to create the value outside the loop like the above example...
i don't know the rules here, so i am not going to give anymore away.


what?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Adam, your statements are correct.

Think of it this way, all arrays in java are actually objects internal to java. So if you ask for args[0], that is going to activate a method in the args object to return a reference to the first element of the array.

Whereas if you store the contents of args[0] in a String outside of the loop, you only need to reference that String each time you go through the loop. So you've saved a significant number of method calls (the number of times through the loop minus one).

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment 1a