Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

passing data among methods  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help  me figure this code out, this topic on OCA is very confusing to me.

Can you tell me why if i was to change line 5 to number = number(number); I would see the incrementation to 2.
And if I was to change line 6 to letter(letters) I would only see abc. Thanks.


1: public class ReturningValues {
2: public static void main(String[] args) {
3: int number = 1; // 1
4: String letters = "abc"; // abc
5: number(number); // 1
6: letters = letters(letters); // abcd
7: System.out.println(number + letters); // 1abcd
8: }
9: public static int number(int number) {
10: number++;
11: return number;
12: }
13: public static String letters(String letters) {
14: letters += "d";
15: return letters;
16: }
17: }
 
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Moe Jackson wrote:Can someone help  me figure this code out, this topic on OCA is very confusing to me.

Can you tell me why if i was to change line 5 to number = number(number); I would see the incrementation to 2.
And if I was to change line 6 to letter(letters) I would only see abc. Thanks.




Hello, Moe.

Two important things you should mind:

1 - What is keeping you from indenting your code? Like so:



There is a tag in the menu bar titled "Code" that allows you to do so. Then, copy code from your IDE or text editor,  paste it inside the tags and voilá.

2 - Have you tried the changes you propose? What was the result?
 
Ricky Bee
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to your situation, a few hints:

What is happening on line two?



In the code you provided, a similar thing is happening in one case, and not happening in the other.


Also, and not least important, the names of the variables, methods and arguments are deliberately the same, so they might be confusing. Variable SCOPE matters in this situation...
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!