How to swap two objects in Java? [duplicate] - java

This question already has answers here:
swapping of objects in java [duplicate]
(8 answers)
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 7 years ago.
I have a class written for Fraction objects. I need to swap two Fraction objects, not the contents of the objects. Here is my code.
public void fswap(Fraction other){
Fraction temp = other.copy();
other = this;
this = temp;
The copy() method returns a Fraction object identical to the object it was called on. The last line of this code throws an error in my IDE "cannot assign a value to final variable this". Any help is greatly appreciated.

You cannot write a swap method in Java that swaps the objects, not their contents. It is literally impossible. This is a distinguishing feature of a pass-by-value language, though it's always important to remember that Java passes references by value.

Related

Java 8 method references for multiple statements in streams [duplicate]

This question already has answers here:
Multiple lambda method references
(3 answers)
Closed 5 years ago.
list.stream().forEach(e -> method(e)) can be converted to list.stream().forEach(this::method)
Similarly can we convert list.stream().forEach(e -> { method1(e); method2(e);}); using method references expressions. Big apologies if you don't understand question. I am using mobile app first time.
No you cannot.
The point of Method references in Java is to abstract (syntaxically) a lambda expression. Since forEach consumes a function that takes 1 element of type specified by the parent stream, there is no syntax sugar for double application using method references.
Even I'm not sure that this answer is wanted by you,
How about changing the method to static one in that class?

In Java sometimes method parameters act like passed by reference, even though they are not. what method are they passed by? [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 5 years ago.
In Java sometimes method parameters act like passed by reference, even though they are not.
what method are they passed by?
I know java is always passed by value. But is it possible that it could act like pass by reference
All parameters are passed by value.
Visit https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

What is the difference between passing arguments in Java and Python? [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
How do I pass a variable by reference?
(39 answers)
Python and Java parameter passing [duplicate]
(1 answer)
Closed 5 years ago.
I come from Java and C++, and now I'm learning Python with this tutorial.
To the best of my knowledge, in Java arguments are passed by value. It seems that they are passed by reference, because when the argument is an object, we pass the reference of the object. So, if inside the called function we change the object state, the object will result modified even after the function call. But, if inside the function we reassign the function parameter we are changing the referenced object, which means that from that point the function's argument will not be affected anymore.
A simple example:
void foo(List<Integer> l){
l.append(1); //affecting list_argument
l = new ArrayList<int>();
l.append(2); //not affecting list_argument
}
public static void main(String[] args){
List<Integer> list_argument = new ArrayList<Integer>();
foo(list_argument); //passing list_argument's reference by value
//list_argument contains 1 only
}
Now, I'm reading this article about passing arguments in Python. In the article, it refers to a fancy name strategy called "passing by object", but to me it seems exactly the mechanism that I described above.
So, my question is: is there any difference the passing argument strategy between Python and Java?

java code demonstrating primitive and reference types [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I am learning java and in my learning material I found the following question:
You are creating an application that includes a number of methods. These methods will change the values of both primitive and reference variables passed in as arguments. Which statement best describes the effect that changes made in the method will have on the original variables? Choose the best option(s) from those listed below.
The question is whether the value reference type can be changed, and the answer is no. But could anyone help with showing a code which demonstrates this behavior?
Thank you
reference type cant be changed.
Java is a pass by value language.
This means when you invoke a method ,such as
Person p = new Person();
foo(p);
You are pass a copy of reference p.
So you cant change reference type,but you could change the value indicated by the reference.

Passing params in java by reference [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
How to return multiple objects from a Java method?
(25 answers)
Closed 9 years ago.
I'm a new comer form C#, and I know clearly that "Java is always pass-by-value."
But pass-by-reference is useful when we want to get multiple outputs from one method.
How can we get multiple outputs from one method in java, as in C#.
I know one way to do this -- use a generic wrapper class, and get value from the field.
class Wrapper<T> {
public Wrapper(T value) {
Value = value;
}
public T Value;
}
Is there another way to realize this effect?
No, Java does not have out parameters. You can pass an object reference that the method is to modify to pretend that it has out parameters, but this isn't usually the best design and runs into other issues (multithreading and mutable state for one).
The best way to achieve a method that returns multiple values is to have the method return a type that contains multiple values.
Another way to simulate call by reference in Java is to pass a one-element array as a parameter.

Categories

Resources