File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Returning multiple values in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Returning multiple values in Java" Watch "Returning multiple values in Java" New topic
Author

Returning multiple values in Java

Nelson Sam
Ranch Hand

Joined: Jun 12, 2010
Posts: 30
I am doing this swap program to swap to integers.I want the result to be printed from the main method.i.e. the values after swapping.Is there any way out to print the value of a and b in main method

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Not the way you're going about it, no, and you can't return multiple values. You can return an array and destructure, or pass in an object and change its internal values, or use the object you already have, or do it inline, or use an inline xor swap, or...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

When you call a method with arguments, then the arguments are passed by value - in other words, not the variables themselves, but just a copy of the value of the variables is passed to the method.

So if you call:

then the values of x and y are passed to the swapNos method - not the variables themselves. Inside the method, a and b are two new variables. When you swap the contents of a and b, nothing happens to the original variables x and y in the method that you called swapNos from.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

David Newton wrote:Not the way you're going about it, no, and you can't return multiple values. You can return an array and destructure, or pass in an object and change its internal values, or use the object you already have, or do it inline, or use an inline xor swap, or...

Or... just put those three lines of code in the place where you want to swap the two integer variables.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

David Newton wrote:[...]or do it inline, or use an inline xor swap, or...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

David Newton wrote:... or use an inline xor swap, or...

According to me, swapping variables with the XOR trick is only a way to show off how clever you are It looks obscure, makes your code less readable, and does not have any real advantages (for example, it's not "faster" in general because you don't need a temp variable). Don't do the XOR trick when writing real software (unless there is a really, really special reason, for example when you're programming a microcontroller in assembly language, and you really need to spare a register).
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I agree, but that wasn't really the point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning multiple values in Java