• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String and ArrayList passed to a method...

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem 1:
public class A {
public void decorate(String s) {
s = new String("#" + s + "#");
}

public static void main(String[] args) {
A a = new A();
String greeting = "hello";
a.decorate(greeting);
System.out.println(greeting);
}
}

Here is the output is "hello".I can understand why.


Problem 2:
public class A {
public void decorate(List list) {
String s = new String("#");
list.add(s);
}

public static void main(String[] args) {
A a = new A();
List list = new ArrayList();
a.decorate(list);

// This prints the objects in the list
system.out.println(list);
}
}
The output is "#".Can somebody explain why??
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

ya, the decorate method takes a list as an argumet, adds a string "#" to it... And when we are the objects in the list, # is the only object present and its printed...

Where's it that you're not understanding? Anws, I'll compare the two codes...

In the first code, initally there are two references[ 'greeting' and 's'] both referring to the same object... and when a new string is created, a new object is created and 's' refers to that... greeting still refers to the old string object...

In the second code, there are two references... one is the list reference in main method and the other is the list reference in the decorate method, both pointing to the same object.. However in this case when an object is added to the list, theres no new list-object created.. its the same list-object to which the string is added... Both the list references refer to the same object.. so printing list using the list reference in main prints the #...

Hope that helps,
Vishwa

[ June 15, 2008: Message edited by: Vishwanath Murthi ]
[ June 15, 2008: Message edited by: Vishwanath Murthi ]
 
Rekha Kanakasabapathy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vishwa!! That Helps!
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic