Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7899019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:35:35+00:00 2026-06-03T08:35:35+00:00

Possible Duplicate: Is it possible to write swap method in Java? Given two values

  • 0

Possible Duplicate:
Is it possible to write swap method in Java?

Given two values x and y, I want to pass them into another function, swap their value and view the result. Is this possible in Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:35:36+00:00Added an answer on June 3, 2026 at 8:35 am

    Not with primitive types (int, long, char, etc). Java passes stuff by value, which means the variable your function gets passed is a copy of the original, and any changes you make to the copy won’t affect the original.

    void swap(int a, int b)
    {
        int temp = a;
        a = b;
        b = temp;
        // a and b are copies of the original values.
        // The changes we made here won't be visible to the caller.
    }
    

    Now, objects are a bit different, in that the “value” of an object variable is actually a reference to an object — and copying the reference makes it point at the exact same object.

    class IntHolder { public int value = 0; }
    
    void swap(IntHolder a, IntHolder b)
    {
        // Although a and b are copies, they are copies *of a reference*.
        // That means they point at the same object as in the caller,
        // and changes made to the object will be visible in both places.
        int temp = a.value;
        a.value = b.value;
        b.value = temp;
    }
    

    Limitation being, you still can’t modify the values of a or b themselves (that is, you can’t point them at different objects) in any way that the caller can see. But you can swap the contents of the objects they refer to.

    BTW, the above is rather hideous from an OOP perspective. It’s just an example. Don’t do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to write a basic swap function in Java Hi. I don't
Possible Duplicate: Write a function that returns the longest palindrome in a given string
Possible Duplicate: How does XOR variable swapping work? Swap the values of two variables
Possible Duplicate: Write a function that returns the longest palindrome in a given string
Possible Duplicate: read/write to Windows Registry using Java I want to read the registry
Possible Duplicate: Searching through list I need to write a function 'once' which, given
Possible Duplicate: How to write a UTF-8 file with Java? I want to create
Possible Duplicate: Java \ Pattern - how to write a pattern that verifies the
Possible Duplicate: How do I write superscript word for checkbox text in java? So
Possible Duplicate: Detecting SMS incoming and outgoing I want to write a app that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.