• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning multiple values in Java

 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21153
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:[...]or do it inline, or use an inline xor swap, or...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, but that wasn't really the point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic