• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Array confusion (SCJP) Please help

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test1231{
public static void main(String argv[]){
Test1231 t1 = new Test1231();
t1.change();
}
private void change(){
String[] Sa = {"Sam Smith", "John Smith"};
getName(Sa);
System.out.print(Sa[0]);
}
public void getName(String s[]){
s[0]=null;
}
}
The above program prints "null". However the below program prints "Sam Smith". When i change the array element it is accepting. Then why it is not accepting when i am nullifying the array itself? How it is able to retain the values?
public class Test1231{
public static void main(String argv[]){
Test1231 t1 = new Test1231();
t1.change();
}
private void change(){
String[] Sa = {"Sam Smith", "John Smith"};
getName(Sa);
System.out.print(Sa[0]);
}
public void getName(String s[]){
s=null;
}
}
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With this

you're null-ing only the local variable, meanwhile the original object stays intact (reference Sa in the method change is still pointing to it so it can't be gc'ed).

HTH,
B.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am giving my certification this month and I am also new to this so pardon me if I am not able to explain this properly, I am sure some one will coreect me if I am wrong...
Array is an abject and you are passing a copy of the object reference in the method
in case 1: with the copy of object reference you are modifying the actual object and thus you can see those changes to the original object ( doesnt matter which reference u r using to check the object)
in case 2: where u are setting s=null, u are setting null to the copy of object reference you passed in the method and this does not change the original object which is out there.
Hope this helps.
Thanks
[ March 01, 2004: Message edited by: Jay Patel ]
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some doubts,

if he is passing a copy of object reference to the method, doesn't that still points to original object and make changes on that?


If s[0] is a local variable, does that means that the code 'public void getName(String s[]) ' effectively create another object?
Would appreciate if there are more explanations.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Now let's consider the problem step by step.
public class Test1231
{
public static void main(String argv[])
{
Test1231 t1 = new Test1231();
t1.change();
}
private void change()
{
String[] Sa = {"Sam Smith", "John Smith"};
getName(Sa);
System.out.print(Sa[0]);
}
public void getName(String s[])
{
s[0]=null;
}
}
Step 1: Creating the object of class Test1231
Step 2: Calling the method change().
Step 3: In method change creating Sa reference of array type suppose that reference contains address 100 and then you are intializing the local array with values like
Sa[0]=Sam Smith
Sa[1]=John Smith
Step 4: Now you are calling the getName() method and passing it the reference Sa.
Step 5: In method getName() you are creating a local reference variable of array type s and copying the value of reference variable Sa in that so now s is also having the address 100. Now both Sa and s points to the same array.
Step 6: Now you are setting the value at s[0] = null; this change is reflected in Sa array becuase here we have two refernce that is Sa and s pointing to the same array so anychange made will be visible so that's why we get null when we try to print out Sa[0].
Now let's consider the next part of your problem.
public class Test1231
{
public static void main(String argv[])
{
Test1231 t1 = new Test1231();
t1.change();
}
private void change()
{
String[] Sa = {"Sam Smith", "John Smith"};
getName(Sa);
System.out.print(Sa[0]);
}
public void getName(String s[])
{
s=null;
}
}
Till step 5 is same. So till now we have two reference variables Sa and s pointing to the same array having addresses 100.
Step 6: Now we are setting s = null means that now we changing the value of reference variable from 100 to null. now s is no more pointing to array so now when we try to print the value of Sa[0] we will get "Sam Smith" beacuse no change has been made to array.
 
Rashi Gulati
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Now let's consider the problem step by step.
public class Test1231
{
public static void main(String argv[])
{
Test1231 t1 = new Test1231();
t1.change();
}
private void change()
{
String[] Sa = {"Sam Smith", "John Smith"};
getName(Sa);
System.out.print(Sa[0]);
}
public void getName(String s[])
{
s[0]=null;
}
}
Step 1: Creating the object of class Test1231
Step 2: Calling the method change().
Step 3: In method change creating Sa reference of array type suppose that reference contains address 100 and then you are intializing the local array with values like
Sa[0]=Sam Smith
Sa[1]=John Smith
Step 4: Now you are calling the getName() method and passing it the reference Sa.
Step 5: In method getName() you are creating a local reference variable of array type s and copying the value of reference variable Sa in that so now s is also having the address 100. Now both Sa and s points to the same array.
Step 6: Now you are setting the value at s[0] = null; this change is reflected in Sa array becuase here we have two refernce that is Sa and s pointing to the same array so anychange made will be visible so that's why we get null when we try to print out Sa[0].
Now let's consider the next part of your problem.
public class Test1231
{
public static void main(String argv[])
{
Test1231 t1 = new Test1231();
t1.change();
}
private void change()
{
String[] Sa = {"Sam Smith", "John Smith"};
getName(Sa);
System.out.print(Sa[0]);
}
public void getName(String s[])
{
s=null;
}
}
Till step 5 is same. So till now we have two reference variables Sa and s pointing to the same array having addresses 100.
Step 6: Now we are setting s = null means that now we changing the value of reference variable from 100 to null. now s is no more pointing to array so now when we try to print the value of Sa[0] we will get "Sam Smith" beacuse no change has been made to array.
I hope it will clear your doubts
Regards
Rashi
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr.Patel is correct.
I suport his answer
 
Frankie Chee
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So when we set 's=null' that will make 'it' eligible for garbage collection where the reference variable 's' is just a copy of a reference. But if we say 's[0]' that will refer to the first member of the 'Sa' the original array object, through the copy.
hmmm, that leads me to some other questions.
I understand that in 'public void getName(]String s[])', 'String s[]' is a copy of the reference to the the object 'String[] sa'. Does that means that 'String s[]' is also an object that contains a reference to another object? I mean.. is reference variable equals to an object?
 
Rashi Gulati
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frankie
You are right that after setting s=null make it eligible for the garbage collection,
----------------------------------------------------------------------------
I understand that in 'public void getName(]String s[])', 'String s[]' is a copy of the reference to the the object 'String[] sa'. Does that means that 'String s[]' is also an object that contains a reference to another object? I mean.. is reference variable equals to an object?
----------------------------------------------------------------------------
Well String s[] is just the reference which holds the address of the array object which was created by Sa it is not an object itself it is reference variable containing the address of the array.
Regards
Rashi
 
Frankie Chee
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Crystal clear, thank you
 
Kadiyala Naga Sai
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ecellent Mr.Gulati
I feel very happy with such a great and clear explanation.
This Java Ranch site really deserves appreciation to bring intellectuals like you to a very nice platform.
Bye
 
Sekhar Kadiyala
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys. It was really good explanation. Now i can handle such questions in the exam. I am writing the exam on 11th!!
 
Frankie Chee
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Luck
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic