| Author |
Swapping of Object in Java
|
pratik gaurav
Greenhorn
Joined: Oct 04, 2012
Posts: 11
|
|
|
 |
Kemal Sokolovic
Bartender
Joined: Jun 19, 2010
Posts: 800
|
|
|
And the question is...?
|
The quieter you are, the more you are able to hear.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Welcome to the Ranch
Code tags are supposed to work when you put [code=java] before your code and and [/code] after it. Also keep your lines short, as it says here. don’t worry, I have sorted it out for you and you can see how much better it looks
Also please tell us what the problem is. If we don’t know, we can’t help you. In this case, I think the compiler is telling you off and you never get near running the code at all.
Your swapping class won’t do what you expect. You have two fields, which you never use.
You have some poor style, too. apart from using tabs for indenting, you have put the [] for an array in the wrong place. Put the [] before the name of the parameter and then you can see the type correctly. Not Swap obj[]. Then you will be able to see the type that the compiler thinks you are trying to pass. It doesn’t matter what you think you are trying to pass, it is whether you can convince the compiler of it.
Don’t write a swap method which swaps elements 0 and 1. Write a swap method which takes two ints, remembering you will suffer an Exception if the numbers are inappropriate
|
 |
pratik gaurav
Greenhorn
Joined: Oct 04, 2012
Posts: 11
|
|
Kemal Sokolovic wrote:And the question is...?
swapping to two object using call by refrence.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3865
|
|
pratik gaurav wrote:swapping to two object using call by reference.
If that's a question, the answer is "no". Java doesn't have pass by reference. It passes references by value, which is a small but important difference.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4905
|
|
pratik gaurav wrote:swapping to two object using call by refrence. 
Let's back up.
What are you trying to do Pratik? And explain it to us in English, not in Java. What is your Swap class intended to accomplish?
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
If you mean swapping two elements in an array, that is something different. You are not given the two objects, but one different object—the array. You are not so much swapping as altering the state of the array. Which you can do in a method which is passed a mutable object.
|
 |
pratik gaurav
Greenhorn
Joined: Oct 04, 2012
Posts: 11
|
|
i am trying to sawp the reference of two object.
i know i'm newbie.. but please look at it you will come to know what i am trying to explain
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
pratik gaurav wrote:i am trying to sawp the reference of two object.
i know i'm newbie.. but please look at it you will come to know what i am trying to explain 
It's already been fully explained. Please re-read the previous replies carefully.
There is no pass-by-reference in Java. If you want to swap two references, they need to be elements of an array or members of a class.
What specific question do you still have?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
pratik gaurav wrote:please look at it you will come to know what i am trying to explain 
If you want to get the most from this site, you should probably read our FAQ on HowToAskQuestionsOnJavaRanch (<---that is a link - click it). But in brief, you want to make it as easy as possible for people to help you. Saying "read the code I posted" is lazy, and turns a lot of people off from helping you.
Instead, if you said "well, I'm trying to swap two objects in memory. I wrote the above code, but I think it doesn't work because....", folks will know they are answering you question instead of guessing what that question is.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: Swapping of Object in Java
|
|
|