• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

[immutable]...

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class A {
static String sName = "a";
public static void main(String argv[]){
A t = new A();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName){
sName = sName + "b";
}
}
My answer is "ab",but the output is "a",why??
-----------------------------
thanks for help,Liao
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, the result will be "ab" only if the

changed to

Hope it helps.
Guoqiao

Originally posted by chao-long liao:
public class A {
static String sName = "a";
public static void main(String argv[]){
A t = new A();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName){
sName = sName + "b";
}
}
My answer is "ab",but the output is "a",why??


 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,but why??
The reason is "reference"???
------------------------------
thanks for attention ,Liao
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is "a" because----
sName = sName + "b"
will modify the actual parameter of the method to "ab" (which is destroyed when method returns) and not the member var. In such situation u should always use this.sName to access the member var.
Rashmi
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello chao-long liao,
This is a tricky question designed to fool the user answering it.
Lets break up the code bit by bit. Have a look at these comments I added to your code:

*1 = here you are indicating that you want to execute the method called piggy and you are passing the variable sName to it.
*2 = here you are indicating that you want to print the variable sName (declared and initialized on the line marked // part of explanation from *2
*3 = here the method piggy says that its return type is void and its argument type is a String. Now java has a unique way of handling data inside methods/constructors; any data/variables initialized inside the method/constructor will only live while the method is alive i.e they will be automatically be removed after the method has finished executing. As for the:
public void piggy(String sName)
the String sName is just to confuse you, this variable has absolutely nothing to do with any other variables in other classes/methods etc. You can give these variables any name even if the same name already exists in another method or class.
What actually happens when you do:
sName = sName + "b";
is that you are saying that the variable sName (whose copy was passed to the method piggy and whose current value is "a") should be changed to "ab"
Then the method finished executing, meaning that the variable that you just changed just died. The actual variable sName declared at the beggining of the class remains untouched the whole time.
if you wanted to change the data inside the class level variable then you should have done:
this.sName = sName + "b";
meaning that this class's (class A) variable sName should to changed to the variable sName of the method, and have "b" added to it.
try this code to understand it better:

Hope this wasnt too long
and if im wrong, someone please correct me
Thank You,
Kamil.

[This message has been edited by Kamil Dada (edited August 14, 2001).]
 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody ^_^
But I still have a question,in Java,
there are two ways to pass argument.
1. pass by value
//used by primitive data,it dosen't change the original value
2. pass by reference
//used by reference,it will change the original value
the above statement is correct??
this question is a reference,why using "pass by value"??
--------------------------------
thanks for help,Liao
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chao-long liao:
Thanks everybody ^_^
But I still have a question,in Java,
there are two ways to pass argument.
1. pass by value
//used by primitive data,it dosen't change the original value
2. pass by reference
//used by reference,it will change the original value
the above statement is correct??
this question is a reference,why using "pass by value"??


Java doesnt have pass by reference
only pass by value
Even here pass by value is what happenin
Kamil's explanation is very detail and clear
Good job
 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chao-long liao:
Thanks everybody ^_^
But I still have a question,in Java,
there are two ways to pass argument.
1. pass by value
//used by primitive data,it dosen't change the original value
2. pass by reference
//used by reference,it will change the original value
the above statement is correct??
this question is a reference,why using "pass by value"??


Hello,
It is a common mistake which people make, by saying that java has two way to pass a value:
1. pass by reference
2. pass by value
but which is wrong.
Remember this: In java, when you pass a value to some method (or anything else) you ALWAYS pass a copy of the value
eg:
if you pass a variable "abc" to a method "def", then method def will recieve its own copy of a variable (which is why if we change a value in the method which exists in the class aswell, the class-level one does not get changed; thats why we need to use this.variable name if we want to change the class-level variable)
and any changes made to the variable will not affect the original value
if you pass a object to a method, you do not actually pass the object to the method, but a copy of the reference referring to the object, so even if you change anything in the method, the data of the object will get changed (this is why people think you have passed the object)
If the second part of passing a object is unclear
refer to the campfire stories of this site; I think they had a detailed explanation of pass by value
Kamil.
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO,
liao's statement is correct abt arguments in methods follow pass-by-value (copies the original varaible's value, so any modification in new var does not affect the original var) if the argument is a variable and follows pass-by-reference (copies the reference which still points to the original object and if any modification is done in the new refrence it affects the original object) if the argument is a refrence.

------------------
azaman
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic