Goldman Sachs Interview Question
Java DevelopersCountry: India
Interview Type: In-Person
I kind of disagree on the local copy and "no effect on the original reference" and stuff. Lets try to understand few basic things . 1) In java References are created on stack and Objects are created in System ( heap ) . 2) As there are no explicit pointers in java as in c or C++ , we don't differentiate value( primitive) or address in ( reference) . Yes a reference is an address which points to an object in the heap . But in java everything is pass by the value - -if we have to define it clearly "In java the reference is holding address and jut the value in that reference is passed " so if I declare String str = new String ("hello world"); then here str is a reference which is created on stack ( to be much clear "method local stack") and the object is created in heap. If we come to this program first thing when we see a declaration String iAmOfAnArgumentativeNature = "I am born new" then here "hello" string goes to STL ( string literal pool ) and Stings are immutable in java . when the method m1( iAmOfAnArgumentativeNature) is called the value in iAmOfAnArgumentativeNature reference ( **that is the address ) is passed to the method m1(String arg1) and it can be denoted as arg1 = iAmOfAnArgumentativeNature so now arg1 and iAmOfAnArgumentativeNature are pointing to the same string "I am born new" in the SLP . At this point there are two references to the string object in the SLP --"I am born new" .And in that method they do arg1 = "Am I going to disappear?" so here the ar1 which was initially pointing to string "I am born new" is now replace with the address of the new string in SLP "Am I going to disappear?" . So we see the out put as I am born new . If that String is changed with StingBuilder or StringBuffer then you see totally a different out put as StringBuilder/StringBuffer are mutalble .
@Mahanth Even if you change the code as below it would not result in any change
class Test
{
public void m1(StringBuffer arg1){
arg1 = "Am I going to disappear?";
}
public static void main (String[] args)
{
Test test = new Test();
StringBuffer iAmOfAnArgumentativeNature = new StringBuffer("I am born new");
m1(iAmOfAnArgumentativeNature);
System.out.print(iAmOfAnArgumentativeNature);
}
}// end class
only if you would have called append method on StringBuffer in m1() its value would have been changed in the main method.
Oops..Change main() as follows..
public static void main (String[] args)
{
Test test = new Test();
String iAmOfAnArgumentativeNature = "I am born new";
test.m1(iAmOfAnArgumentativeNature);
System.out.print(iAmOfAnArgumentativeNature);
}
import java.io.*;
public class Test
{
public void m1(String arg1){
arg1 = "Am I going to disappear?";
}
public static void main (String[] args)
{
Test test = new Test();
String iAmOfAnArgumentativeNature = "I am born new";
m1(iAmOfAnArgumentativeNature);
System.out.print(iAmOfAnArgumentativeNature);
}
}// end class
goto browxy website and paste this ..surely u ll get output...correct me if i am wrong.....
Above Code would result in Compile time error. In Java you cannot call a non-static method from a static method.
Point taken but don't divert the discussion. Assuming the code compiles, what will happen? Look at the update . The point of the question is not about compilation. And - I already know the answer - Not asking for the answer. Just posted for community benefit.
I kind of disagree on the local copy and "no effect on the original reference" and stuff. Lets try to understand few basic things . 1) In java References are created on stack and Objects are created in System ( heap ) . 2) As there are no explicit pointers in java as in c or C++ , we don't differentiate value( primitive) or address in ( reference) . Yes a reference is an address which points to an object in the heap . But in java everything is pass by the value - -if we have to define it clearly "In java the reference is holding address and jut the value in that reference is passed " so if I declare String str = new String ("hello world"); then here str is a reference which is created on stack ( to be much clear "method local stack") and the object is created in heap. If we come to this program first thing when we see a declaration String iAmOfAnArgumentativeNature = "I am born new" then here "hello" string goes to STL ( string literal pool ) and Stings are immutable in java . when the method m1( iAmOfAnArgumentativeNature) is called the value in iAmOfAnArgumentativeNature reference ( **that is the address ) is passed to the method m1(String arg1) and it can be denoted as arg1 = iAmOfAnArgumentativeNature so now arg1 and iAmOfAnArgumentativeNature are pointing to the same string "I am born new" in the SLP . At this point there are two references to the string object in the SLP --"I am born new" .And in that method they do arg1 = "Am I going to disappear?" so here the ar1 which was initially pointing to string "I am born new" is now replace with the address of the new string in SLP "Am I going to disappear?" . So we see the out put as I am born new . If that String is changed with StingBuilder or StringBuffer then you see totally a different out put as StringBuilder/StringBuffer are mutalble .
Class credits {
public static void main(string[] arguments {
// set up film information
String title = "The Piano ";
int year= 1993;
String director = " Jane Campion";
String role1 = "Ada";
String actor1 = "Holly hunter";
String role2 = "Baines";
String actor2 = "Harvey Keital";
String role2 = "Stewart";
String actor3 ="Sam Neil";
String role4 = "Flora";
String actor4 = "Anna Paquin";
// display information
System.out.print (title + " ( ", + year + " ) \n"+
"A" +director + " film.\n\n"+
role1+ "\t" + actor1 + "\n"+
role2+ "\ t" + actor2+ "\n"+
role3+ "\t" + actor3+ "\n"+
role4+ "\t" + actor4+ "\n"+
}
}
what is the output?
class credits {
public static void main(String[] arguments){
// set up film information
String title = "The Piano ";
int year= 1993;
String director = " Jane Campion";
String role1 = "Ada";
String actor1 = "Holly hunter";
String role2 = "Baines";
String actor2 = "Harvey Keital";
String role3 = "Stewart";
String actor3 ="Sam Neil";
String role4 = "Flora";
String actor4 = "Anna Paquin";
// display information
System.out.print(title + " ( " + year + " ) \n"+
"A" +director + " film.\n\n"+
role1+ "\t" + actor1 + "\n"+
role2+ "\t" + actor2+ "\n"+
role3+ "\t" + actor3+ "\n"+
role4+ "\t" + actor4+ "\n");
}
}
String is mutable and blab la concepts wont be applied here
"I am born new"; is one string object and "Am I going to disappear?"; is another.
iAmOfAnArgumentativeNature is the refrence and arg1 another refrence.
iAmOfAnArgumentativeNature is pointed to "I am born new" string object
and arg1 is also pointed to "I am born new" string object
but than arg1 is pointed to "Am I going to disappear?";..
so even if you use string buffer, linked list or whatever it wont change.
It will print "I am born new";
Since java passes object references rather than objects, and it passes these references by value rather than by reference, changing the copied object reference in the method will have no effect on the original reference, hence the value will be unchanged.
- Anonymous July 11, 2013The same would happen if you had a method assigning any other new object to an object reference inside a method.