• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swapping of Object in Java

 
pratik gaurav
Greenhorn
Posts: 16
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the question is...?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:And the question is...?


swapping to two object using call by refrence.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10273
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic