Java is pass-by-value indeed, but actually the one that hold this value is little bit of different with what you might think of. java is pass by value?
the word value
, which in basic data type is the data itself, for instance
int numberA=100 ;
int numberB=numberA;
the value that passed from numberA
to numberB
is the data 100
.
things never change in Reference type
:
Person p = new Person("Lost", "Boy", 24);
Person person=p;
person=new Person("Rugal", "Bernstein", 23);
the value that is passed from p
to person
is the reference address that point to the Person("Lost", "Boy", 24)
one, for any details please execute program above and debug it yourself to clarify them.
Here you might think, after person=new Person("Rugal", "Bernstein", 23);
the value of person now is disparate with its original one. yes of course, now if you println(person)
and that will turn out to be a new Person as you might think of.
But what about p
, does that changed because Java is pass-by-value?
Actually, after test with that, I found it is not, even if you changed the person
, it will not bother p
. what's wrong with java, is there any problem here ?
Yes, java demonstrated there is no pointer in it and all the things that passed in parameter is called reference
. lets go back to C/C++ style of programming:
int data=100,temp=50;
int* pdata=&data;
int& rdata=data;
pdata=&temp;
println("%d %d",*pdata,rdata);
rdata=temp;
println("%d %d",*pdata,rdata);
anything special can you find from above?
yes, actually in C/C++ , there is two type of so call pass-by-value
method, the first one declared by *
is called pointer
, second one start with &
named reference
, here please do not consider this reference with java, because you will find they are different later.
in C/C++ , the reference
is a kind of alias
, in other word, these two variables are totally identical thing, whatever changes you made on one will thus effect the other one. because they are just one thing.
But Pointer
is a little different with reference
. yes pointer can also point to a variable and you can also change the value of that variable by using *pdata=temp
. But here is a problem, when you using pdata=&temp
to change the pointer itself, will the data
change according to pdata
?
the answer is NO!
you can test it yourself ! because what value store in pointer is the address of data
variable, when you want to change the value of data
it can surely change it by refer *
, but when you try to change the address that stored in pdata
it would not bother with data
because they are totally different two variable.
Now do you have a familiar sense that you have ever feel this before?
yes, in java the so called reference
is what pointer
behave like in C/C++.
and here we have the deal, in java that demonstrated itself with pass-by-value
is correct and no more pointer
is a mendacity.
now let me answer questions:
1.the word value is the data that stored in memory. in java primitive type hold real data as value and address as value in reference type.
2.as I have already explained above, you can test it yourself!
3.and I tested some code, the hashcode()
will not change so long as the value of your object do not change.
for instance:
Person p = new Person("Lost", "Boy", 24);//Hash Code 38443066
p.setAge(100); //Hash Code 38443066
p=new Person("Rugal", "Bernstein", 23);//Hash Code 38444526
because in default situation, hashcode()
method only measure the address itself hence so long as your address unchanged, the hashcode()
will not change.
you can override this method by yourself to measure its member fields as well.
the method changePersonObject()
only changed the member field in it thus will have no difference with outer hashcode()
. you can achieve it by person=new Person("Rugal", "Bernstein", 23);
in this method, its hashcode()
will surely changed.
please try it and you will get more comprehension with Java