aspose file tools*
The moose likes Beginning Java and the fly likes Wrapper - Swap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper - Swap" Watch "Wrapper - Swap" New topic
Author

Wrapper - Swap

Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Hi,

I want to swap the values between two variables by using Wrapper. I know how to swap thru' arrays.

If anybody knows, pls give the example codings. I have tried on my own, but in the vain. Below is my coding:

Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
All your code is doing is swapping the x and y variables in your method, not your classes variables. Try this.



As a suggestion you might want to read a little about variable scope. That's what was causing your confusion. In essence, you were creating two new x and y variables inside your switch method, and never changing the original variables.

Also methods should be named with a lowercase letter.
[ July 14, 2005: Message edited by: Hentay Duke ]
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Thanks for your reply.

Things like swapping, great care should be taken for the scope of the variable. isn't!!
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Sorry ya.. The coding is not working.



Error
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
This code works perfectly for me.

Edit: Sorry my bad. I switched the case of method name in the post but not in my program. Switch is a terrible name for your own method (the compiler thinks you want to use a switch statement after it's changed to lowercase) change it to swap and you'll be all set. I've corrected it below


[ July 14, 2005: Message edited by: Hentay Duke ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

"switch" is a reserved word in Java, so it can't be a function name! Mr. Duke has gotten around this by using a capital "S", "Switch" -- a subtlety which "Mark Jim Tony" missed. A better solution would be to use the more traditional name "swap" for this function.

Now, "Mark Jim Tony" --

You may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. No fake-sounding names. You can change your display name here. Thanks!


[Jess in Action][AskingGoodQuestions]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824


The problem is that "switch" is a keyword in Java -- you can't create a function called "switch". Hentay's worked because he called it "Switch" with a capital "S". The problem here is that Java convention calls for methods to start with a lower-case letter. I'd call it "swap" or "switchIntegers" or some other name.

Finally, "Mark Jim Tony", please read our JavaRanch Naming Policy and
change your display name to comply. (We are looking for a display name that is not obviously fictitious, and I'm afraid that yours quilifies as suspicous-sounding.)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Yes in my original post when I went to copy my code into the post I noticed that he had named his method Switch and just changed it to lowercase in the post without going back and changing it in my IDE and re-running it. I totally missed the fact that switch would then cause errors(silly mistake on my part I just wasn't thinking). I apologize for any confusion.
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
No Problem...
Anyway you cleared by doubt.

Thank you very much
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I would like to suggest a few modifications to Hentay's solution. First of all, the tmp variable is unnecessary because you are using the parameters to swap the member variables. This means that you can do this instead:


However, this doesn't do a true swap because the new values of x and y can be totally unrelated to the old values. For example, what if we do something like this:

What is being "swapped" here? Nothing, really. The old values of x and y are completely replaced.

(As a side note, notice that you don't need to use the Integer wrapper class in Hentay's modified version of your code. You can just as easily use the primitive int type. However, the same issue still arises: this code does not actually swap the values of x and y unless you happen to call the method with the correct arguments.)

So how do you write a "real" swap() method in Java? Well, it's a little tricky becaus as you have probably already noticed, you can't use primitive types since their values are copied. Other problems occur when you try to use the wrapper classes. First of all, the wrapper classes are immutable. You cannot change the value stored inside of it. Second of all, references are passed-by-value, so just swapping the references doesn't do any good because, as you have already seen, it doesn't affect the original variables.

One possible solution is a slight variation of your original code. The main change I would make is completely remove the parameters altogether. This allows the method to access the class variables and swap them directly:

Once again, this doesn't require the use of the wrapper classes. You can do this just as easily with a primitive int.

Another solution is to write your own wrapper class that allows the caller to modify the internal data:

Now we can pass instances of this class to a swap method:

Notice that I didn't use the Integer wrapper class here because it isn't necessary, yet again.

In my opinion, none of these solutions is very elegant. However, I find that I rarely need to swap two values when I'm programming in Java. If I do, I will just write the code in-place rather than delegating it to another method.

I hope this helps, even though other people have already answered your original question.

Keep Coding!

Layne
[ July 14, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Mark Jim Tony:
Sorry ya.. The coding is not working.
(snip...)
[/CODE]


I think others have explained why you recieved these errors. I would like to point out that Hentay changed the name of your method because it is common convention to start method names with lower-case letters. We usually start class names with upper-case letters. If you adopt this convention, you will be able to easily recognize whether a certain name is a class or a method when you read your own code or another programmer's (assuming they also follow this convention).

Keep Coding!

Layne
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Thanks you very much for all of you.

Hentay Duke

Ernest Friedman-Hill (for naming policy too. Hope now it will be according to your policy)

Joel McNary

Layne Lund
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper - Swap