~ Mansukh
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..
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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.
Mansukhdeep Thind wrote:You cannot write a swap program in Java language.
Joanne
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.
Of course you can - as the OP has just demonstrated. Writing a swap method is more difficult but certainly not impossible.
~ Mansukh
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.
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.
![]()
Joanne
Your intentions are good but a bit misdirected and potentially misleading/confusing for the OP.
~ Mansukh
Precision is very important in programming.
~ Mansukh
Mansukhdeep Thind wrote:You cannot write a swap program in Java language.
Junilu Lacar wrote:Hint: you can use an array to hold the objects to be "swapped"...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
~ Mansukh
Mansukhdeep Thind wrote:... share some literature on how these shuffling/sorting algorithms
Campbell Ritchie wrote:Any further suggestions, Junilu
?
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?
![]()
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:Just have a look at the sort methods in java.util.Arrays
~ Mansukh
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.![]()
Campbell Ritchie wrote:In B you can writeBut Java does not support multiple assignment, nor multiple return values.
~ Mansukh
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
|