• Post Reply Bookmark Topic Watch Topic
  • New Topic

What the code with arrays does?  RSS feed

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
many Thanks!
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Dear Winston,
Thank you for comments. Next time I will do better.
Regards,
Dana
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!