Forums Register Login

String Problem

+Pie Number of slices to send: Send


Result


Why result is not a, ab ?

please explain to me.
+Pie Number of slices to send: Send
u have uesd String which is immutable.so the changes which r done in Append method doesn't reflect.if u want to print "a,ab",
place 'data = data + "b";' in main function and execute.
Bye
regards,
mahesh
+Pie Number of slices to send: Send
This is quite simple. Methods arguments are passed by value. When the method Append is invoked a copy of the reference data is made and passed to the method. In the method data is assigned a new value but the reference "data" passed in the main method still points to "a".
+Pie Number of slices to send: Send
The String type is passed as call-by-value...

You need to use StringBuffer in this case to apply the call-by-reference...

Have a look at the following program... After running it, you will receive 'ab' as you wish...



Hope it helps...
+Pie Number of slices to send: Send
in java don't have argument/paramete pass-by reference or not ??
+Pie Number of slices to send: Send
 

Originally posted by somkiat puisungnoen:
in java don't have argument/paramete pass-by reference or not ??



There is no pass-by-reference in java, C# has the concept.
+Pie Number of slices to send: Send
 

Originally posted by Pradeep Bhat:


There is no pass-by-reference in java, C# has the concept.



Pradeep, what about the application that I provided above... Is that not call-by-reference? You can try to run that and you will see that the content of the StringBuffer object changed...

Well, basically is call-by-reference the same as pass-by-reference?
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
 

Originally posted by Ko Ko Naing:


Pradeep, what about the application that I provided above... Is that not call-by-reference? You can try to run that and you will see that the content of the StringBuffer object changed...



It is call by value ko ko . When the method Append is invoked a copy of reference(Hence the name call by value) to data StringBuffer is made. Now , there are two references to the StringBuffer object. What you are doing in the test method is manipulating the same object.

But if you had created a new object in Append

this causes the copied reference to point to a new object (this object is different from the one in the caller), then the changes would not have reflected in the caller's string buffer .
+Pie Number of slices to send: Send
StringBuffer content is changed because the pass-by-value is applied to the reference itself. You couldn't change the reference.
+Pie Number of slices to send: Send
Hi Somkiat,

Objects are passed as references in function calls in java.
Somebody mentioned that variables are passed by value but it
is a bit misleading in this case. Primitive types are passed
my value. Variables that represent objects are not object but
references to objects. Therefore you can say that variables
are passed by value and objects are passed by reference and it
means the same.

To your program:
Reference is a pointer somewhere in memory. Let us start.

means that data now contains a reference to a String object
with value "a". Let's say the reference points to address 20.

is something like:

The same with passing to Append.

Now, what happens inside Append:

creates a new String object with value "ab" on address, let's say
50. Therefore the value of the local variable data becomes
50-which-is-a-ref-to-String. There is no return in Append, therefore
the local data (50-which...) is thrown away.
We are back in main, where our data is still reference to our
20-which-is-a-ref-to-String.

Well, that's it. It is the same as in C:


Enjoy,
Petr
+Pie Number of slices to send: Send
 

Somebody mentioned that variables are passed by value but it
is a bit misleading in this case.



I dont find it mentioned any where in this thread.
+Pie Number of slices to send: Send
in java don't have argument/paramete pass-by reference or not ??
It is an interesting topic that I want to know too....
As I know that in C++ and C, the object or pointer can be passed by reference, in Java, if I declare a String s and then point to another string which had been created. s is a kind of pointer. But why can not pass by reference as it exists the pointer concept. :roll:
One teacher told me that when I create a object, it will return a object reference.
Hybrid OOooooop
+Pie Number of slices to send: Send
 

But why can not pass by reference as it exists the pointer concept



I don't know why but they have it in C#.
+Pie Number of slices to send: Send
In java don't permit programmer to code program in java refer to memory directly (pointer) Because it's difficult to use and it's cause to occur error when use it's wrong.
+Pie Number of slices to send: Send
 

Originally posted by somkiat puisungnoen:
In java don't permit programmer to code program in java refer to memory directly (pointer) Because it's difficult to use and it's cause to occur error when use it's wrong.



AS java, C# does not allow programmer to access memory. To read about ref parameter passing use the link below

http://www.c-sharpcorner.com/Language/out_and_ref.asp
+Pie Number of slices to send: Send


Result


Why result is not a, ab ?

please explain to me.[/qb]<hr></blockquote>


Ok, let's walk through this code, but first notice I changed the String that Append takes in and call it something. First of all a new string reference is created 'data' which contains "a". This is printed. Then the method Append is called and the string reference 'data' is passed to it. It's is important to remember that strings are objects, and that the compiler changes the shorthand to the real method calls on the strings. It's also important to remember that Strings can't be mutated, everytime you change a string, you are actually creating a new string. With that in mind, Append looks at the String located by reference 'something' (which points to the same place that 'data' points to). It then takes this string and attaches a "b" to the end of it, however we must remember that the String can't be mutated, so it actually creates a new string, and then points the reference 'something' at the new string. The method exits and the string reference 'something' is deleted, and later the actuall string (which happens to be "ab") is garbage collected. The main then continues and looks where 'data' is pointing, which is still "a" because we never modified the String reference 'data' and the String "a" can't change, so it prints "a" again.

And just so everybody here knows, java passes objects by reference, not value.

Hope that helps,
Kevin

Edit: just noticed Petr answered this
[ September 29, 2004: Message edited by: Kevin Stock ]
+Pie Number of slices to send: Send
One other thing to keep in mind: the Append method declares a local variable named data in its parameter list. This "data" is not the same as the object's data. So, when the method returns, the local data is destroyed and therefore there is no reference to the String literal that was created and assigned to it within the method body.

Had you done:

Now you would have modified the original reference. But, since we have a static method in your code and therefore no object, you have no this pointer.

Think of your current code as:

This (hopefully) clarifies why the data variable in the main method does not get modified by the Append method.
+Pie Number of slices to send: Send
P.S. Having the parameter name the same as the calling argument name is called "data hiding". The confusion in this topic is an indicator as to why this is a bad practice.
+Pie Number of slices to send: Send
public static void main(String[] args) {
String data = "a";
System.out.println(data);
Append(data);
System.out.println(data);
}
public static void Append(String data) {
data = data + "b";
System.out.println(data);
}
--------------------- --------------------
here append is called as call by value , variable data is local to append only. variable data at main is locl to main, what happen here is a new variable is created at append and intialized with value "a" and concatenated with "b" which is local to append so not reflected in the variable at main because both are different
[ October 04, 2004: Message edited by: sajeer assiz ]
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1295 times.
Similar Threads
Inner Thread Classes
how and why to do it ?
Doubt regarding StringBuffer
Problems with java and mysql error: 1064
Can this be a case of deadlock ?And how to determine it output ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:11:31.