pass-by-value

Java - Object state does not change after method call [duplicate]

ε祈祈猫儿з 提交于 2019-11-26 17:05:15
问题 This question already has answers here : Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed 3 years ago . Beginner java question, but I cannot understand how call-by-Value ( or Reference ) is working in the example below - How come the String value is not modified after it exits the method while my custom String Object is. ? Same with other classes like Date.. public class StringMadness { public static void main(String[] args) { String s = "Native String"; CustomStringObject

Passing objects by reference vs value

北战南征 提交于 2019-11-26 16:36:19
问题 I just want to check my understanding of C#'s ways of handling things, before I delve too deeply into designing my classes. My current understanding is that: Struct is a value type, meaning it actually contains the data members defined within. Class is a reference type, meaning it contains references to the data members defined within. A method signature passes parameters by value , which means a copy of the value is passed to the inside of the method, making it expensive for large arrays and

pass by reference and value with pointers [duplicate]

二次信任 提交于 2019-11-26 16:09:39
问题 This question already has answers here : Is passing pointer argument, pass by value in C++? (5 answers) Closed 3 years ago . I don't understand why passing a pointer to a function doesn't change the data being passed in. If the function proto looked like this: void func( int *p ); and func allocated memory to p, why can't it be used outside of the function? I thought that pointers were addresses? 回答1: Whilst something like this does what you expect: void func(int *p) { *p = 1; } int a = 2;

Are structs 'pass-by-value'?

谁说我不能喝 提交于 2019-11-26 15:44:09
问题 I've recently tried to create a property for a Vector2 field, just to realize that it doesn't work as intended. public Vector2 Position { get; set; } this prevents me from changing the values of its members ( X & Y ) Looking up information on this, I read that creating a property to a Vector2 struct returns only a copy of the original object and not a reference. As a Java developer this confuses me. When are objects in C# passed by value and when are they passed by reference? Are all struct

What exactly is the difference between “pass by reference” in C and in C++?

蓝咒 提交于 2019-11-26 15:13:47
问题 The phrase "pass by reference" is used by C and C++ developers alike but they appear to be used to mean different things. What exactly is the difference between this equivocal phrase in each language? 回答1: There are questions that already deal with the difference between passing by reference and passing by value. In essence, passing an argument by value to a function means that the function will have its own copy of the argument - its value is copied. Modifying that copy will not modify the

Pass an array to a function by value

ε祈祈猫儿з 提交于 2019-11-26 15:13:38
Below is a snippet from the book C Programming Just the FAQs . Isn't this wrong as Arrays can never be passed by reference? VIII.6: How can you pass an array to a function by value? Answer: An array can be passed to a function by value by declaring in the called function the array name with square brackets ( [ and ] ) attached to the end. When calling the function, simply pass the address of the array (that is, the array’s name) to the called function. For instance, the following program passes the array x[] to the function named byval_func() by value: The int[] parameter tells the compiler

Passing a String by Reference in Java?

半城伤御伤魂 提交于 2019-11-26 15:02:38
I am used to doing the following in C : void main() { String zText = ""; fillString(zText); printf(zText); } void fillString(String zText) { zText += "foo"; } And the output is: foo However, in Java, this does not seem to work. I assume because the String object is copied instead of passed by referenced . I thought Strings were objects, which are always passed by reference. What is going on here? Aaron Digulla You have three options: Use a StringBuilder: StringBuilder zText = new StringBuilder (); void fillString(StringBuilder zText) { zText.append ("foo"); } Create a container class and pass

Pass by reference or pass by value? [closed]

纵饮孤独 提交于 2019-11-26 14:25:31
When learning a new programming language, one of the possible roadblocks you might encounter is the question whether the language is, by default, pass-by-value or pass-by-reference . So here is my question to all of you, in your favorite language, how is it actually done? And what are the possible pitfalls ? Your favorite language can, of course, be anything you have ever played with: popular , obscure , esoteric , new , old ... sven Here is my own contribution for the Java programming language . first some code: public void swap(int x, int y) { int tmp = x; x = y; y = tmp; } calling this

Is passing pointer argument, pass by value in C++?

一曲冷凌霜 提交于 2019-11-26 11:45:51
Is passing pointer argument, pass by value in C++? Since i see that any change to the pointer as such is not reflected outside the method. The changes i do by dereferencing the pointer is reflected though. In that case, is it acceptable/standard procedure to use pointer to pointer as argument to a function to modify the pointer value as such within a function? Yes to both. Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the object pointed to) is copied. That means that if you change the value of the pointer in the function body,

change pointer passed by value

对着背影说爱祢 提交于 2019-11-26 11:34:09
问题 I have given a function foo(struct node *n) where n is the head node in a linked list. Now foo should change n s.t. it points to the end of the list. But is this possible with this function signature? Assuming t is the pointer to the end of the list: n = t won\'t work because the pointer is passed by value. *n = *t won\'t work because I would overwrite the head of the list. Did I miss something? 回答1: You would need to use a pointer to a pointer: foo(struct node **n) To change what n points to