• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

What will be the output.

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is looking very simple but i am not able to give it right ans.
can any one explain?. it`s taken from javablackbelt.com

File: MyObject.java

01: public class MyObject {
02:
03: private String name;
04:
05: public MyObject(String anyName) {
06: this.name = anyName;
07: }
08:
09: public String getName() {
10: return this.name;
11: }
12: }


File: Worker.java

1: public class Worker {
2:
3: public void compute(MyObject myObject) {
4: myObject = null;
5: }
6: }


File: Main.java

1: public class Main {
2:
3: public static void main(String[] args) {
4: MyObject myObject = new MyObject("Foo");
5: Worker worker = new Worker();
6: worker.compute(myObject);
7: System.out.println("Name: " + myObject.getName());
8: }
9: }


What is the Output?


1) Name: null

Because the reference to myObject in Main is set to null in worker.compute(MyObject myObject);

2) NullPointerException caused by a method call on a reference which is set to null in worker.compute(MyObject myObject);

3) Name: Foo
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its the third option, name-Foo
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kaushik,
i'm thinking the answer to that question is it throws a NullPointerException, but why don't you just paste that code into your java editor and find out the output?

cheers.
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the call at line 6 in Main.java will not effect myObject in any way... So there will be no null pointer exception...
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ankit, Can you explain it out..i still not able to understand..Your answer is right i am getting same answer. but not able to understand it.

Any one can please explain it to me??
[ October 05, 2008: Message edited by: kaushik vira ]
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK here's what's happening

in Main.java line 4 does this



now myObject refers to an object of MyObject class and the name field of that object has a value Foo.

When you call compute at line 6, this is what happens



Now myObject in main method and compute method both refer to the same object. But objects in java are passed as pointers. So when you assign null to myObject in compute method this happens



when you assign null to myObject in compute method, the myObject in compute method no longer points to the object which it did earlier. but the myObject in main method still points to the object same object as it did earlier....

I hope this will clear your doubt...
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

File: Worker.java

1: public class Worker {
2:
3: public void compute(MyObject myObject) {
4: myObject = null;
5: }
6: }



The new Object myObject that is created is only a reference to the actual my object that is passed. So with

myObject = null;



Only this object is referenced to null and the actual object that is passed, is still present.

So, we do get the output as Foo..
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now it`s pretty clear.. There is concept on heap and local stack.. what method call it doing changing stack value.. but it`s not effect actual heap.

Thank you ankit and all Ranchers .
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, when you pass an object reference to a method, that method gets it's own copy of the same reference variable....but it IS a COPY of it. If the method nulls it's OWN copy it does nothing to the original reference in the calling method. In other words all it did was to null it's own pointer to the object. The one owned by the calling method is still good.

Notice though, that when the copy is passed in it DOES point to the same object. So if the called method uses that reference to reach in and change state inside the object, then the object itself IS changed even when return is made back to the calling method.

So as long as the called method holds on to and uses the object reference passed it it can access members of the original object. If it nulls or modifies it's copy (the parameter) then it loses that connection to the object and will have no more effect on it.
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Bob.. You are absolutely right... In this case we have to check that the state of object is changed then it will direct effect to heap.. but we are playing with reference variable.. then only that variable get change and it will not change heap data.
 
Efesa Origbo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks kaushik,
i guess i got thrown off by the shadowing of the myObject variable in the compute method. As there is no return statement in that method, nothing really changes in the Object.

nice explanation and illustration by the way...
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Bob said,

Notice though, that when the copy is passed in it DOES point to the same object. So if the called method uses that reference to reach in and change state inside the object, then the object itself IS changed even when return is made back to the calling method.


Can you please give an example ??
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello abhi,

see code below


File: MyObject.java

01: public class MyObject {
02:
03: private String name;
04:
05: public MyObject(String anyName) {
06: this.name = anyName;
07: }
08:
09: public String getName() {
10: return this.name;
11: }
12: public void setName(String name) {
13: this.name = name;
14: }
12: }


File: Worker.java

1: public class Worker {
2:
3: public void compute(MyObject myObject) {
4: myObject.setName("abhi");
5: myObject = null;
6: }
7: }

At line 4 in worker class is changing state of object. this will be reflect to Main. if i will print there

File: Main.java

1: public class Main {
2:
3: public static void main(String[] args) {
4: MyObject myObject = new MyObject("Foo");
5: Worker worker = new Worker();
6: worker.compute(myObject);
7: System.out.println("Name: " + myObject.getName());
8: }
9: }

Output will be abhi.

so now you can understand it. state of object (name) get changed.
[ October 06, 2008: Message edited by: kaushik vira ]
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello kaushik,

here call by reference concept happing in
1.which we can change the value of object but we can not change address of object so if we try to make null it's null only reference object not realy object.
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello vipin,
I thing you want to say right thing, but one word is might mislead others.

Correction required:
--------------------------------------------------------------------------
1.which we can change the value of object but we can not change address of object so if we try to make null it's null only reference object not really object.
--------------------------------------------------------------------------

I think it`s need correction. sentence should be only Reference variable.

Corrected:
--------------------------------------------------------------------------
1.which we can change the value of object but we can not change address of object so if we try to make null it's null only reference variable not really object.
--------------------------------------------------------------------------
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, i understood!
 
vipin jain
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi kaushik,

yes you are right.
Thanks for correction.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

Thanks for your excellent explanation. BTW...Could you please guide me how to answer the question with the help of diagrams, as you explained for the above mentioned question?

Have a wonderful day ahead!

Regards,
Gopal
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic