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
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.
posted 1 year ago
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?
posted 1 year ago
Mark Richardson wrote:. . . Did I get it?
posted 1 year ago
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.
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
global solutions you can do in your home or backyard