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

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?

Related

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.

How to swap two objects in Java? [duplicate]

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.

Is this pass by reference or pass by value [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 8 years ago.
so i was given this question in class.
consider the following method declaration:
public static int method(int[] x)
Is the int[] x parameter passed by reference or by value?
To my understanding only primitives can be passed by value. This therefore is passed by reference? I read from here (Are arrays passed by value or passed by reference in Java?) that 'java can only pass by value' and that the reference pointing to the object array x is being passed as a value.
so what is the answer?
Is the int[] x parameter passed by reference or by value?
Can someone also give me an example of a pass by reference since java can't do this?
many thanks
Java passes the reference by value, but the object itself is not passed by value, nor is copied.
Just change something inside the array passed to some function and then look into it outside. You will see that it is changed
Everything in Java is pass-by-value. Consider your function:
public static int method(int[] x) {
// If you change x here, it won't get reflected in the x. Because you pass a copy of the
// original x.
}

reason: why java is not pass by reference? [duplicate]

This question already has answers here:
Why doesn't java support pass by reference like C++
(2 answers)
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 9 years ago.
Java is strictly pass by value..I am still confused with this concept.
I have gone through many websites to get an answer for this but I am not able to find any good reason.
Is there any proper reason why Java is not pass by reference?
The value of a reference variable is an "address" in Java. When you pass a reference variable to a method, a new reference variable is placed on the stack and a copy of the passed reference variable's value is used to initialize the new local reference varaible's value, just like any primitive type.
That is the classic definition of pass by value.
NOTE: While you can think of the reference as a memory address, it's not. The underlying mechanism makes it act logically as if it were though.
SHORT VERSION: references are simple variables just like the other primitive types for purposes of passing arguments to methods. What you can do with them once passed is obviously different.

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