File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes modify 2 local int variables by a called method without returning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "modify 2 local int variables by a called method without returning" Watch "modify 2 local int variables by a called method without returning" New topic
Author

modify 2 local int variables by a called method without returning

Himanshu Sapra
Greenhorn

Joined: Jan 22, 2013
Posts: 3
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

Joined: Dec 16, 2010
Posts: 953
    
  13

Smells like homework. What happened when you tried it?


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Himanshu Sapra
Greenhorn

Joined: Jan 22, 2013
Posts: 3
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

Joined: Dec 16, 2010
Posts: 953
    
  13

I think this page might help you understand.
Himanshu Sapra
Greenhorn

Joined: Jan 22, 2013
Posts: 3
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
Bartender

Joined: Jan 17, 2008
Posts: 2374
    
  28

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

Joined: Aug 16, 2005
Posts: 14146
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4542
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

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?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4387
    
    8

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

Joined: Feb 08, 2009
Posts: 605

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

Joined: May 03, 2008
Posts: 4542
    
    5

AtomicInteger is mutable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: modify 2 local int variables by a called method without returning