• Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm swapping two objects but it doesn't seem to be reflected in the output  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 3 classes:

Launcher.java
FlightMaker.java
Swapper.java

Launcher:




FlightMaker:



Swapper:



My Output is as follows:

 
Stefano Carniel
Greenhorn
Posts: 27
Android Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swapper is working on a copy of the 2 objects, and does not modify the original ones. In java parameters are always passed by value
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two things to understand:

1. Variables (of non-primitive types) in Java are references to objects (they are not objects themselves; they just point to objects)
2. Values are passed by value to methods

Because Java works according to the two points above, your swap(...) method doesn't really do anything.

The swap(...) method takes two arguments, which are put into two variables, firstFlight and secondFlight. Both these variables point to a Flight object. Then, you swap the variables - you make firstFlight point to the object that secondFlight was pointing to and vice versa. But then the method ends and the two variables go away - the swapping has no effect, because the two variables don't exist anymore after that.

Because the variables are passed by value, the swapping has no effect on the two variables that call the method with (in line 14 of your second code snippet). Only the value of the variables val1 and val2 is passed, not the variables themselves. So what happens when you call s.swap(val1, val2) in line 14 is this:

- The argument variable firstFlight is assigned to point to the object that val1 points to
- The argument variable secondFlight is assigned to point to the object that val2 points to
- Then the swap method switches around what firstFlight and secondFlight point to
- Then the method returns, but nothing has happened to the original variables val1 and val2; they still point to the same objects as before you called the method
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this code will swap two references. You may wish to create the Swapper object as a throwaway local variable. It isn't anything like as expensive in system resources as you think. I'm sure you can work out how the code works without needing any explanation.Jesper has explained why your swap method didn't work; we see people whom pass by value confuses quite frequently.
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:. . . I'm sure you can work out how the code works without needing any explanation. . . .


Yay! I'm learning Generics! Okay, I'm going to repeat my understanding of how you did it and you can tell me if I'm on track:

In your Swapper class, you have declared two variables of generic types. You create a constructor in your Swapper class which takes two parameters, also of generic types. We store the second passed parameter into the first field of "this" object. We store the first passed parameter into the second field of "this" object.

Then, we simply use the getters of the Swapper object to apply them wherever we'd like.

Did I get it?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:. . . Did I get it?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:. . . you have declared two variables of generic types. . . .
Actually, two variables of the same generic type. Maybe I ought to have called the class Swapper<T>. The custom is to use single capital letters: T for type, T, U or S, T or S, T, U or similar for multiple types. Things like functional interfaces often use S, T or similar for the input types and R for the result type.
Things like Lists which have elements usually use E for element and Maps which have keys and values use K, V.

These letters are a convention. Using a single capital letter means you can easily distinguish that (which is called a formal type parameter) from any variables. When you write Swapper<Flight> then Flight is its actual type parameter.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!