Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

modify 2 local int variables by a called method without returning

 
Himanshu Sapra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across one question :

"Is it possible to have two local variables, say 2 int values x, y in a method A, Call a method B from this method A, pass these two X and Y and without returning any thing swap the values of X and Y in the method A?"

Thanks
Java Knowledge Seeker
 
J. Kevin Robbins
Bartender
Pie
Posts: 1800
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Smells like homework. What happened when you tried it?
 
Himanshu Sapra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not homework...it came up in an interview...I don't think it is possible even if i wrap the int into Integer and pass.

If i pass the reference of Integers to the method B and try use temp local variable there...the local references start pointing to swapped values but the original local variables of method A don't get affected.

Please give valuable help.

Thanks
Java Knowledge Seeker..
 
J. Kevin Robbins
Bartender
Pie
Posts: 1800
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this page might help you understand.
 
Himanshu Sapra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mr. Robbins for giving link to a really good article...

I knew that Java is strict pass by value but since interviewer was trying to confuse me while i was sticking to an answer "it is not possible"...so i thought of checking with experts in JavaRanch....

i though there could be some trick which i might not be aware of....

So my answer was right then???
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way to pass 2 ints to a function and have the function modify the ints. So, your answer of "It is not possible" is technically correct.

However, you could wrap those 2 ints in a bean, pass the bean, and have the function modify the ints inside the bean. Since the bean is passed by reference, any changes made by the called function would be visible to the calling function. If I were the interviewer, I would be looking for this answer because it shows an understanding of how pass by value vs pass by reference works, and also willingness to modify the parameters of the problem statement to solve the core problem
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:Since the bean is passed by reference, ...

No. Java always passes by value - pass-by-reference does not exist in Java.

However, remember that variables of non-primitive types are references. If you pass such a variable to a method, you are passing a reference by value - the reference is copied. That's not the same thing as passing by reference.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:There is no way to pass 2 ints to a function and have the function modify the ints. So, your answer of "It is not possible" is technically correct.

However, you could wrap those 2 ints in a bean, pass the bean, and have the function modify the ints inside the bean. Since the bean is passed by reference, any changes made by the called function would be visible to the calling function. If I were the interviewer, I would be looking for this answer because it shows an understanding of how pass by value vs pass by reference works, and also willingness to modify the parameters of the problem statement to solve the core problem


Apart from the thing about the reference to the bean being passed by value, you would still have to assign the two local int variables to the bean fields, after running 'method b' -- which is an exceedingly roundabout way of just swapping the values by either of the standard programming idioms (with / without a temp variable) in 'method a' itself.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it is possible even if i wrap the int into Integer and pass.


sure it is, why don't you try it out?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saifuddin Merchant wrote:
I don't think it is possible even if i wrap the int into Integer and pass.

sure it is, why don't you try it out?

Integer is immutable, so that won't work.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Saifuddin Merchant wrote:
I don't think it is possible even if i wrap the int into Integer and pass.

sure it is, why don't you try it out?

Integer is immutable, so that won't work.


Ouch forgot that! Guess you would have to write you own wrapper over int!
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AtomicInteger is mutable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic