• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Assignment 1a

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic