• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion about program output!!!

 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I came across the following code snippet.


The output of program is
15 0
20

Now my confusion is,

At Line 3 v is pointing to the object having Hascode Value@123ab
and after that(at Line 5) the v is pointing to the new object having Hascode Value@456cd
and hense the output at Line 6 is 15 0

So my question is that why ouptput is not
15 0
15
As we are assining the new object to the reference at Line 5 after returnig from second()
the value reference must points to the object created at Line 4(Hashcode Value@456cd) and not to the object created at Line 1( Hascode Value@123ab)

 
Torsten Oppermann
Ranch Hand
Posts: 62
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in Line 3 you set the value of v.i to 20. Then you use the reference value to point to a completely different object, created in Line 4 with the value of int i = 15
When reassigning v in the method second, the reference variable v in method first is unaffected since its method local.

Key word : Pass by Value ! not by Reference

what i mean is: the Value (an Object in this case) is passed to the second method, so changes affect the actual object. However after the reassigning to the new Object this isnt the case anymore

Hope i could help
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thanks

Got the point...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic