File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java program to swap two different type of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java program to swap two different type of objects " Watch "java program to swap two different type of objects " New topic
Author

java program to swap two different type of objects

bala kan
Ranch Hand

Joined: Aug 20, 2012
Posts: 44


Hi Friends,


Good morning to everyone,i attended one interview regarding java technologies.. there they asked us to write a program to swap two different types of objects.. for instance, two variables int and char variables are to be interchanged.. after interchanging, we should print their values..


as a beginner in Java,I wrote this program..

..


the result was

before swapping i and c is 98 a

after swapping i and c a 98

if I made any mistakes.. please tell me..your help will be highly appreciated

Regards,
Bala



Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi Bala

There is only and only pass-by-value in Java. In case of primitives, you pass a copy of the bits that hold the primitive value. So, you cannot swap primitives. That is for one. As for the reference variables passing, object references in Java are also passed by value. What that means is that a copy of the bits that point to the object on the heap is passed. So, you can change the state of the object inside a method and it will be reflected at both the places. There is no pass by reference in Java. So burn it in. You cannot write a swap program in Java language. . In fact, that is a test to check if a language passes arguments by value or by reference. Read this carefully.


~ Mansukh
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8010
    
  22

bala kan wrote:there they asked us to write a program to swap two different types of objects.. for instance, two variables int and char variables are to be interchanged.. after interchanging, we should print their values..

Well right off the top that seems to be a badly worded question - if indeed they did use the term "object" - because neither int nor char is an object in the Java language.

Furthermore, it would be virtually impossible to write a truly universal swapper, since not all combinations of objects are swappable. How, for example, would you swap a String and a List? (although maybe that's part of the test - to get your ideas for designing a generic swapper )

And even with an int and a char, you wouldn't be able to correctly swap all values.

I think a little more information about the actual instructions you were given would be good.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Mansukhdeep Thind wrote:There is only and only pass-by-value in Java. In case of primitives, you pass a copy of the bits that hold the primitive value. So, you cannot swap primitives. That is for one. As for the reference variables passing, object references in Java are also passed by value. What that means is that a copy of the bits that point to the object on the heap is passed. So, you can change the state of the object inside a method and it will be reflected at both the places. There is no pass by reference in Java. So burn it in.

All very interesting but totally irrelevant to the question that was asked. The swapping is being done inside a single method so there is no method calling happening.

Mansukhdeep Thind wrote:You cannot write a swap program in Java language.

Of course you can - as the OP has just demonstrated. Writing a swap method is more difficult but certainly not impossible.


Joanne
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

All very interesting but totally irrelevant to the question that was asked. The swapping is being done inside a single method so there is no method calling happening.

Well, I wanted Bala to understand the whole concept clearly. A little knowledge is a dangerous thing. I don't think it is irrelevant. In fact, many seasoned Java programmers build a wrong concept that Java passes object references by reference and primitives by value. Many sites too wrongly say that. Like this one. Read point number 6. It is better that we teach Bala the concept correctly as it does not leave any scope for such websites to bring in misconceptions.

Of course you can - as the OP has just demonstrated. Writing a swap method is more difficult but certainly not impossible.


Well, if your claim is correct about a swap program being possible in Java language, then may be the guy who wrote this article is confused. But, I personally still believe that you cannot write a traditional swap program in Java language. Please teach me how to write a swap program to interchange 2 primitives or 2 objects in Java.


P.S-> Please correct me if I am wrong.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Mansukhdeep Thind wrote:
All very interesting but totally irrelevant to the question that was asked. The swapping is being done inside a single method so there is no method calling happening.

Well, I wanted Bala to understand the whole concept clearly. A little knowledge is a dangerous thing. I don't think it is irrelevant. In fact, many seasoned Java programmers build a wrong concept that Java passes object references by reference and primitives by value. Many sites too wrongly say that. Like this one. Read point number 6. It is better that we teach Bala the concept correctly as it does not leave any scope for such websites to bring in misconceptions.

So, following your logic, if someone were to ask you how to ride a bicycle, it would be completely relevant for you to teach him how to swim first because a lot of people drown in swimming pools.

Your intentions are good but a bit misdirected and potentially misleading/confusing for the OP.


Junilu - [How to Ask Questions] [How to Answer Questions]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Mansukhdeep Thind wrote:Well, if your claim is correct about a swap program being possible in Java language, then may be the guy who wrote this article is confused. But, I personally still believe that you cannot write a traditional swap program in Java language. Please teach me how to write a swap program to interchange 2 primitives or 2 objects in Java.

Precision is very important in programming. That article is about writing a swap method, not a swap program. More precisely it is about a swap method that takes two parameters and swaps what they are referring to (the traditional swap method as you have now started referring to it) - that is not possible in Java. It is possible to write a method in Java that can be used to swap what two references refer to, but it will have a different signature to the traditional swap method. And it will then be possible to call that method in a program, which I guess will give you a swap program if that is the only thing it does.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Your intentions are good but a bit misdirected and potentially misleading/confusing for the OP.


I am merely guiding a newbie so that he does not build wrong concepts about the language. If Bala is actually getting confused as you claim, I(in fact all of us) are here to clear those too. I still believe there was nothing wrong with what I did. Anyways, lets not digress away from the topic. We are all here to learn.

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Precision is very important in programming.


Correcto! It is. Not only in programming but any thing which is to be done perfectly without errors. I am referring to a swap method.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Mansukhdeep Thind wrote:You cannot write a swap program in Java language.


Never say never. Of course there is a way to write a general swap method. It won't be pretty or elegant and you'll still have the type compatibility problem that Winston pointed out but up to a point it is possible, even with the pass-by-value nature of method parameters. Do I have Sheriffs' permission to post code? Hint: you can use an array to hold the objects to be "swapped"; upon return from the swap method you'd need to reassign variable references. Hey, I said it's possible up to a point but it's not pretty or even that useful.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8010
    
  22

Junilu Lacar wrote:Hint: you can use an array to hold the objects to be "swapped"...

In fact, it's the perfect vehicle; especially if it's typed, because then you know that the two objects you are swapping are similar.

Indeed, it's one of the major components of many sorting and shuffling algorithms.

Winston
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157


Hint: you can use an array to hold the objects to be "swapped"


Junilu: Could you post the code you are hinting at? I would like to study it. Or do you want me to try it on my own?

In fact, it's the perfect vehicle; especially if it's typed, because then you know that the two objects you are swapping are similar.

Indeed, it's one of the major components of many sorting and shuffling algorithms



Winston : Could you perhaps share some literature on how these shuffling/sorting algorithms are related to what Junilu said about swapping objects in Java? Or am I asking for too much in the same thread?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I’ll let you work out how the IntSwapper class works; the ObjectSwapper class does exactly the same.

Note this is a utility class whose methods swap two elements in situ in the array, rather like an output parameter. The methods will throw Null Pointer and Index Out Of Bounds exceptions as appropriate.
Any further suggestions, Junilu ?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Mansukhdeep Thind wrote:... share some literature on how these shuffling/sorting algorithms

Just have a look at the sort methods in java.util.Arrays
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Campbell Ritchie wrote:Any further suggestions, Junilu ?

None except to note that this kind of thing should be classified as "Silly, stupid things you can do in Java just because" or "Don't try this at work unless you want to get fired."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Agree you should use the library class’ algorithms whenever possible.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8010
    
  22

Mansukhdeep Thind wrote:Winston : Could you perhaps share some literature on how these shuffling/sorting algorithms are related to what Junilu said about swapping objects in Java? Or am I asking for too much in the same thread?

My first port of call for stuff like this is usually Wikipedia. Try 'sorting' or 'shuffling' + 'algorithm'.

Winston
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Junilu Lacar wrote:Just have a look at the sort methods in java.util.Arrays


Looks like I will have to devote time to understanding different sort/search algorithms separately as they appear to demand undivided attention. Thank you Junilu.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote:
Precision is very important in programming.


Correcto! It is. Not only in programming but any thing which is to be done perfectly without errors. I am referring to a swap method.


The phrase "a swap method" is very imprecise. Just saying.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

A bit of a digression -- the Go language has an interesting way of doing swapping:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
In B you can writeBut Java does not support multiple assignment, nor multiple return values.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Campbell Ritchie wrote:In B you can writeBut Java does not support multiple assignment, nor multiple return values.


What is "B"? Some language?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
B
 
Consider Paul's rocket mass heater.
 
subject: java program to swap two different type of objects