0

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.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
AAB
  • 1,594
  • 2
  • 25
  • 40
  • 1
    > Both refer to same object so why does "A " not get printed and "B" > gets printed??? The formal parameter `s` in the method `show(String s)` is different than what is in `main` method. So changing what `s` in show points doesn't make difference in what `s` in main method points. `A="S"` and `B="S"`, you are correct that they refer to same objects. But that **doesn't** **mean** that changing `A="M"` would change `B="M"`. Think of this as two pupil who go to the same school. When when changes school, it **doesn't mean** that other does. – pinkpanther Jun 07 '13 at 19:24
  • @pinkpanther You beat it to me by few seconds. Was trying to say same thing but question got closed and could not post answer. – Smit Jun 07 '13 at 19:26
  • @PinkPanther whats the difference here?? then For eg the Sample class has a String variable s. and in the "main Method" I create an object Sample obj=new Sample(); Sample obj1=obj; Now If I invoke obj1.show(); a change will reflect in obj as well so why is it not the same for a String?? – AAB Jun 07 '13 at 19:29
  • @Smit mine is too tragedic... I started to prepare a comprehensive answer typed the text(that's in comment) and clicked post answer to later edit it to full answer...but that's disabled by the time.... – pinkpanther Jun 07 '13 at 19:30
  • @Smit you are confused between objects and references... you invoke methods on objects...since, obj1 and obj refer to the same objects they reflect the change on that object.... it's like s="A" but here `s` is variable and "A" is objects.... I want to explain it....but how...these comments are not enough...have to find an alternative – pinkpanther Jun 07 '13 at 19:32
  • @pinkpanther We both are in same boat. I was done through `3/4` of my answer. I was trying to give very minimal info as I could not post the whole answer in comment. – Smit Jun 07 '13 at 19:32
  • Anyway to Enable answers?? hmmm I did mention similar Question were asked what need is there to close my Question – AAB Jun 07 '13 at 19:35
  • @AAB You can edit the POST and request for reopening the question. I am not sure how this can be done as I never got any question closed. but yes question can be reopened. @ Pinkpanther. its not about getting upvotes. Its about clearing the confusion. – Smit Jun 07 '13 at 19:36
  • @AAB you are new to SO, it happens this many times here....duplicate questions are not accpeted.... I don't have enough rep here to vote to reopen...but may be you can contact me via facebook..see my SO profile... – pinkpanther Jun 07 '13 at 19:36
  • @Smit "is java a pass by reference".. it's closed as that question exists... but I think that's hard for a newbie in java...or newbie in CS in particular... – pinkpanther Jun 07 '13 at 19:39
  • @pinkpanther _java is strictly pass by value_. when we see thing happening in `C and C++` and move to `java`, Yes this concept gets really confusing. I admit I was also the victim of this concept. – Smit Jun 07 '13 at 19:42
  • @Smit no you are getting wrong.... I don't how to explain it here..it's pass by value only for primitive types such as int... – pinkpanther Jun 07 '13 at 19:44

1 Answers1

1

It is passed by reference, but the reference itself is passed by value, and in show you override the reference. And that's why you should teach pointers :/

Tomer Arazy
  • 1,833
  • 10
  • 15
  • Hi Lets say For eg the Sample class has a String variable s. and in the "main Method" I create an object Sample obj=new Sample(); Sample obj1=obj; Now If I invoke obj1.show(); a change will reflect in obj as well so why is it not the same for a String?? – AAB Jun 07 '13 at 19:18