Results 1 to 4 of 4

Thread: swap values

  1. #1
    yanivdan98 is offline Member
    Join Date
    Feb 2013
    Rep Power

    Default swap values


    i want to create a function which get two Integers and swap between their values

    however, as i know in java the variables passed to function by value (meaning that the variable doesnt pass itself, only the copy of the variable is pass)

    so what is the method to do it?

    can i pass variables as reference and not by - value?


  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Rep Power

    Default Re: swap values

    Just try it! You are passing a reference by value.

    And there are no functions in Java.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Islamabad, Pakistan
    Rep Power

    Default Re: swap values

    May be i am wrong but i think you can do it by having two methods (returning one value after swapping) which will be easier for you at this level....but try to think those methods by yourself..

    Plus: Correct me if i am wrong.....

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: swap values

    You just can't swap the value of two variables by using the call by value mechanism; there are a couple of tricks that seem to do the trick but it hides that fact that it can't be done; there is no need to worry, because it can't be done either by using the call by name mechanism (Algol/60 comes to mind), you need the call by reference mechanism or equivalent to do it.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 12:01 AM
  2. Swap array value
    By irnie1994 in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 05:06 PM
  3. Cannot swap elements in ArrayList
    By glchau in forum New To Java
    Replies: 4
    Last Post: 05-30-2010, 08:03 AM
  4. swap method
    By leavearth in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 06:59 PM
  5. how to swap 2 numbers
    By mary in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts