Results 1 to 4 of 4

Thread: swap values

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

    Default swap values

    hi

    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?

    thanks

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    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
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    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 online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,307
    Blog Entries
    7
    Rep Power
    20

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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
  •