This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Swapping of Object in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Swapping of Object in Java" Watch "Swapping of Object in Java" New topic
Author

Swapping of Object in Java

pratik gaurav
Greenhorn

Joined: Oct 04, 2012
Posts: 16

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

And the question is...?


The quieter you are, the more you are able to hear.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
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: 16

Kemal Sokolovic wrote:And the question is...?


swapping to two object using call by refrence.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

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: 7503
    
  18

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?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
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: 16

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
    
    6

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: 11153
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swapping of Object in Java
 
Similar Threads
Swaping Primitive Data types in java
small swapping problem please please reply!!!!!
Swaping Numbers
Passing objects by reference or value???
Fill the code