• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Returning multiple values in Java

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:[...]or do it inline, or use an inline xor swap, or...

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, but that wasn't really the point.
 
I love a good mentalist. And so does this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic