• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Problem

 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Result


Why result is not a, ab ?

please explain to me.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in java don't have argument/paramete pass-by reference or not ??
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Understanding that parameters are passed by value and not by reference

Pass-by-value semantics in Java applications
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer content is changed because the pass-by-value is applied to the reference itself. You couldn't change the reference.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



I don't know why but they have it in C#.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
There is no "i" in denial. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic