• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What the code with arrays does?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! I have following code, please check if my comments are correct, as I have a heavy doubts:



Thank you!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your comments on line 4 are correct; that's the first line of the definition of the method makeRange, which takes two ints and returns an array of ints.

Your comments on line 5 are also correct. There, a local variable 'range' is created, which is initialized with a new array of ints, with (upper - lower) + 1 elements (nitpick: not "number of arrays", but "number of elements" - there's only one array).

Line 14: Here, a local variable 'range' in the main method is declared. It's not initialized. Nothing is copied here. Note that this local variable is completely separate from the local variable in the method makeRange (line 4).

Line 17: The method makeRange is called, and the return value is assigned to the variable 'range' that was declared in line 14. Nothing is being defined on that line.

One thing that's important to learn to see is that { and } delimit the scope of variables. Variables declared inside a pair of { and } exist only from the point that they are declared until the closing }. So, the variable 'range' in line 5 exists only until line 11 (the makeRange method), and the variable 'range' in line 14 exists until line 24 (the main method).
 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
many Thanks!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dana Horst wrote:Hello! I have following code...


Dana,

Please DontWriteLongLines. It makes your thread very hard to read. I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.

Thanks.

Winston
 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Winston,
Thank you for comments. Next time I will do better.
Regards,
Dana
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dana Horst wrote:Thank you for comments. Next time I will do better.


No probs. Thanks for taking note.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic