This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assignment 1a

 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic