• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Swapping of Object in Java

 
Greenhorn
Posts: 16
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the question is...?
 
Marshal
Posts: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



swapping to two object using call by refrence.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic