• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Values of variables are not changed when placed in methods

 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys, Rafael here. I'm not a native speaker so i'll do my best to comunicate what i'm going through.

I do no understand why the last System.out.print shows this:

2 5 2 4 6 8 10



Instead of this:

5 2 5 10 15 20 25



The first method(changeB) shouldn't change the references between A and B?

 
Rancher
Posts: 99
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is pass by value
 
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch, Rafael. In the changeB(...) method you are indeed swopping the values assigned to the variables 'a' and 'b', but these local variables, being the method's parameters, only have scope within that method. The same-name 'a' and 'b' local variables in the main method are not changed. I think this overlaps with with Adrian's answer (which came up after I saw the Q and before I posted), but I should remind myself of the the whole pass-by-referencs vs. pass-by-value thing in case I explain it incorrectly   . (I don't want to suggest a work-around as I don't know what the practical goal would be.)
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

You have written some confusing code, with all the code squashed up. Make your indents deeper and put empty lines between successive methods. Spaces after every comma (line 16). Don't declare two variables on the same line. You will then end up with something like this:-Let's simplify that code, because there are easier ways to print those values:-Let's give the methods names that reflect what they are doing, and the class a better name, too.Now you have some code you can actually read. Also, the array will print as [1, 2, 3, 4, 5]; the [] will make it clear which of the output is array and which the two Strings.
[edit]Corrected spelling error (off→of)
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry; I pushed the wrong button and posted an incomplete post.
Both people who posted last night were correct. If you look here, you will find it says this:-

. . . in Java primitives are passed by value and objects are passed by reference.

...followed immediately by

It is not correct. (My emphasis)

If you read the whole of that link, you will see it tells you quite clearly that Java® only supports pass‑by‑value, even quoting from the Java® Language Specification (=JLS) where it says that. This is probably the crucial part of the quote:-

newly created parameter variables

As EB told you, the a and b in the swap method are now new variables, completely separate from the originals, and changing the identity of the variables in the swap method does not make any changes to the originals. That means the two Strings in lines 19‑20 are exactly the same as they were before.

In the second method, you are passing an array, whose original state is [1, 2, 3, 4, 5]. It is possible to manipulate that array because it is a mutable reference type and change its state to [2, 4, 6, 8, 10]. That does not mean you have changed the identity of the array. Its memory location will be exactly the same, but it is possible to change its state, i.e. the values of its elements. That can be done for any array of any type (with one special exception) or for any object with methods available to change its state.If you display that account, it is the same account as before. All its details will be unchanged, except that I now have the amount of money I deserve
 
You showed up just in time for the waffles! And this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!