3
class Effect 
{
public static void main(String [] args) 
{
    Effect e=new Effect();
    e.method();
}
void method()
{
    long [] a1 = {3,4,5};
    long [] a2 = doArray(a1);
    //expected output
    System.out.println("After Calling doArray "+a1[1] +" "+ a2[1]);

    String s1 = "Hello";
    String s2 = doString(s1);
    //expected output s1=HelloJava s2=World  like earlier arrays
    System.out.println("After Calling doString "+s1 + " " + s2);
}

long [] doArray(long [] a3) 
{
    a3[1] = 7;
    System.out.println("In doArray "+ a3[0]+" "+a3[1]+" "+a3[2]);
    return a3;
}
 String doString(String s1) 
{
    s1 = s1 + "Java";
    System.out.println("In doString "+ s1);
    return "World";
}

}

OUTPUT

In doArray 3 7 5
After Calling doArray 7 7
In doString HelloJava 
After Calling doString Hello World

My Expected OUTPUT :

After Calling doString HelloJava World
  • all array references a1,a2,a3 pointing to same. So, modified data applicable to all
  • but in case of String it gives old value even if we modify data

please give some explanation?

CHAITHANYA PADALA
  • 147
  • 1
  • 1
  • 8

6 Answers6

1

In java String objects are immutable.

The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.

Documentation

Code clarification -

String doString(String s1) {
    s1 = s1 + "Java";
    ...
}

Here s1+"java" will create a new object into the String pool. and reference s1 from method fucntion will still refer the old String object.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

You need to understand that there's a difference between an object and a reference (ie, pointer). An object is real (as close as anything is "real" in a program) "thing". A reference is simply the address of the thing.

If, in a phone book, I change the address of your house from 123 1st Ave to 456 2nd St, that doesn't change your house. It will still (presumably) be located at 123 1st Ave. But if someone follows the address in the phone book they will end up at a different house.

And if you move from one house to the other you don't (usually) take the house, you load the furniture into a truck (lorry) and haul it to the other house. Then, if you don't change the phone book, someone looking for you will end up at the wrong (old) house.

(This has nothing to do with the fact that a String is immutable, BTW. The same is true of an array of long. But what you're doing in the above code with the array is moving in new furniture, not changing the address. There is only ever one array.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
1

when you call doString(s1);
here s1 is a local String reference variable that referring "Hello" String obj.
And after this statment s1 = s1 + "Java";
this Statement create a new String object and s1 is now referring "hello java" string obj.

but in the case of array
doArray() not create any new array.

gifpif
  • 4,507
  • 4
  • 31
  • 45
0

Thats because string is immutable any modification you do to the string variable , it creates a new object. It doesnt change the object you are operating on

when you do s1 = s1 + "Java"; The actual parameter is still Hello

and not Hello Java. Only your formal parameter has changed

you may refer the below link

Click the link

You may find a more detailed explanation Click the link

Community
  • 1
  • 1
Freaky Thommi
  • 756
  • 7
  • 18
  • In case of `doArray()` a3 acts as formal parameter even a3 modifies data it reflects all other. Similarly in case of `doString()' s1 acts as formal parameter. So, here i got **confused**. Please give some more **explanation**. – CHAITHANYA PADALA Mar 14 '14 at 12:15
  • http://www.cs.toronto.edu/~dianeh/tutorials/params/ This link expains it with figures...Search for 'Passing Strings' in the link.. The difference here is arrays are mutable (means the content can be changed and strings are immutable means the content/state cannot be changed) – Freaky Thommi Mar 14 '14 at 12:21
0

1) `String s1 = "Hello"; String s2 = doString(s1); //expected output s1=HelloJava s2=World like earlier arrays System.out.println("After Calling doArray "+s1 + " " + s2);

2) String doString(String s1) { s1 = s1 + "Java"; System.out.println(s1 + " "); return "World"; }

In second case doString(String s1), you have a new s1 var that is local to doString() method only, so wat ever changes you are doing in this method will be visible to this method only. It will not reflect in the s1 var declared inside method().

So in second case, you will get output as "Hello Java". But in first case, your will get "Hello" as output. the s1 var declared still points to 'Hello' string in the string constant pool. Its because, String class in an immutable class.

Arjit
  • 3,290
  • 1
  • 17
  • 18
0
s1 = s1 + "Java";

What you are modifying above is local for method.

String s1 = "Hello";

And what you are defining here, your method don't know declared s1 is a stranger for your method.

System.out.println("After Calling doString " + s1 + " " + s2); <--- s1 is declared string in this line, not the local string in method.

For more reference :

  1. Introduction and Basics of String
  2. Immutability of String
Community
  • 1
  • 1
Not a bug
  • 4,286
  • 2
  • 40
  • 80