Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with code  RSS feed

 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont understand this how does orig turn into to arg can somebody please explain


 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here x is reference variable of an instance of class XCopy, which holds the reference of an object of class XCopy.
In below code your calling instance method go(int arg) and passing orig as int parameter whose value is 42 (int).
Below int go(int arg) is an instance method whose return type is int means method must return int value.
Here arg is local variable of method go, value of variable orig is copied in local variable arg of method go().
(Note: No operation on orig variable so value of orig will be same as it is i.e. 42)
Now arg = 42.
Here arg = arg * 2; means arg = 42 * 2 so arg becomes 84. and
returns arg variable's value i.e. 84 to the point where it was called from so control goes to here
and assigns 84 value in variable y so now y = 84. Then you printed it
prints value of orig which is 42 and value of y i.e. 84 which you got by
Output: 4284
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!