Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java program to swap two different type of objects

 
bala kan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 48424
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree you should use the library class’ algorithms whenever possible.
 
Winston Gutkowski
Bartender
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7319
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit of a digression -- the Go language has an interesting way of doing swapping:
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In B you can writeBut Java does not support multiple assignment, nor multiple return values.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic