This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Collection code test  RSS feed

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will the program print when it is compiled and run?
ithis snippet of code of SCJP book :

select the one correct answer
Select the one correct answer.
(a) A collection coming up.
60
(b) A collection coming up.
A collection coming up.
A collection coming up.
60
(c) The program does not compile.
(d) None of the above.
i compile and execute this code the correct answer is (a) but i don't understand why it print once  A collection coming up.
 
Ranch Foreman
Posts: 3243
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, when I compile and run that, I get:

A collection coming up.
60

which seems to make a lot more sense.  Is it possible something has been copied incorrectly?
 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i get:
A collection coming up.
60
but i don't undrestand why 'Acollection coming up' is printed once
 
Mike Simmons
Ranch Foreman
Posts: 3243
19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see - I misread your question.  Well, it's printed once because the method makeCollection() is called once, and that method has a print statement.

Perhaps you wonder why it's only printed once?  That's because this:



is equivalent to



Although it's in a for statement, the makeCollection() call is only executed once, to get the value of the thing we're looping over.  We don't need to keep calling that method every time we get a new value of i.  We've already got the whole list, in the first call.  Afterwards we're just looping through the different values in the one list, that was created once at the beginning of the loop.
 
Saloon Keeper
Posts: 3250
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that in a classical for-loop the continuation condition is re-evaluated with every new iteration, including variables that may appear in such a continuation condition. For instance, can you explain the outcome of the following:


Edit: oops, forgot to say that you need to change the return type of makeCollection to List<Integer>, otherwise this code snippet will not compile! (why?)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!