• 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

Basic Strings question

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of the following class? Is the result because
strings are immutable or pass by value/reference?? Please explain (i always get confused with this question)

public class Bar {
private static String a;
private static void foo(String a){
a = "second";
}
public static void main(String[] argv){
a = "first";
System.out.println(a);
foo(a);
System.out.println(a);
}
}
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are re assigning the reference in the function to some other string. This will not affect what the original reference was pointing to the String "first". That is , in main(), a is still pointing to "first" regardless of what other methods do with that reference. For Strings , even if you change the value of that object in the method, a will still point to "first".
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Bar {

private static String a; // line (1)

private static void foo(String a){ // a here is local. say local:a(local:a = Bar.a)

//when you say a here compiler gives preference to local a
//a in line (1) remines unaltered
//You can access a in line (1) using Bar.a


//now we have two a's.
// 1)local a(belongs to method)
// 2)and a in line (1) (which belongs to class Bar)
// Both pointing to same String Object("first").

a = "second"; // after this line
// local a is made to point to different String Object.
// But a in line (1) still points to old String Object(ie., first)
}

public static void main(String[] argv){

// Now a is null.
a = "first"; // now a is first.
System.out.println(a); // first gets printed here.
foo(a); // pass "first" in to method foo.
System.out.println(a); // first gets printed here.
}
}
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this pgm you ill get
first
second
as output

class Bar {

private static String a;

private static void foo(String a){

Bar.a = "second";
}

public static void main(String[] argv){

a = "first";
System.out.println(a);
foo(a);
System.out.println(a);
}
}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by satya kiran:
What is the output of the following class? Is the result because
strings are immutable or pass by value/reference?? Please explain (i always get confused with this question)

public class Bar {
private static String a;
private static void foo(String a){
a = "second";
}
public static void main(String[] argv){
a = "first";
System.out.println(a);
foo(a);
System.out.println(a);
}
}



Remember that all parameter passing in Java is by value. When the formal parameter is an object reference, then a copy of the actual reference is sent to the method. You can access the original object through the formal parameter name as long as you don't change what the formal parameter points to.

Also in the case of String, you aren't able to change anything about the String because it is immutable so no matter what you do with the formal parameter it won't change the original object.
 
satya kiran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Very much Guys. Now i am clear on this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic