I've gone through a few answers regarding Java always passing by value for example in the below code.
public class Sample {
public void show(String s){
s="A";
}
public static void main(String []args) {
String s="B";
new Sample().show(s);
System.out.println(s);
}
}
String s;
Means just a reference is created as my teacher put it no object is created
so, in the above code s=B
; means s
holds the reference of B
i.e. the place in memory where it is stored is my understanding of this correct?
When do show(s);
In the definition of the method show no new String
object is created, just the reference is passed.
The String s
created in main method and the one in show method both refer to the same object in memory ,if both refer to the same object should "A" not be printed to screen?
Both refer to same object so why does "A " not get printed and "B" gets printed?
I'm a newbie in Java I have read a few previous answers regarding the issue and an answer with a diagram with 2 reference arrows pointing to same object confuses me even more I may have misunderstood the answer so please don`t close my question as its been repeated I need some help with this.