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 8095847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:16:11+00:00 2026-06-05T21:16:11+00:00

public void swap(Point var1, Point var2) { var1.x = 100; var1.y = 100; Point

  • 0
 public void swap(Point var1, Point var2)
 {
  var1.x = 100;
  var1.y = 100;
  Point temp = var1;
  var1 = var2;
  var2 = temp;
 }

public static void main(String [] args)
{
  Point p1 = new Point(0,0);
  Point p2 = new Point(0,0);
  System.out.println("A: " + p1.x + " B: " +p1.y); 
  System.out.println("A: " + p2.x + " B: " +p2.y);
  System.out.println(" ");
  swap(p1,p2);
  System.out.println("A: " + p1.x + " B:" + p1.y); 
  System.out.println("A: " + p2.x + " B: " +p2.y);  
}

Running the code produces:

A: 0 B: 0
A: 0 B: 0
A: 100 B: 100
A: 0 B: 0

I understand how the function changes the value of p1 as it is passed-by-value.
What i dont get is why the swap of p1 and p2 failed.

  • 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-05T21:16:13+00:00Added an answer on June 5, 2026 at 9:16 pm

    Java is pass by value at all times. That’s the only mechanism for both primitives and objects.

    The key is to know what’s passed for objects. It’s not the object itself; that lives out on the heap. It’s the reference to that object that’s passed by value.

    You cannot modify a passed reference and return the new value to the caller, but you can modify the state of the object it refers to – if it’s mutable and exposes appropriate methods for you to call.

    The class swap with pointers, similar to what’s possible with C, doesn’t work because you can’t change what the passed reference refers to and return the new values to the caller.

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

Sidebar

Related Questions

public class Main { public static void main (String args[]) { int nums[]= {2,
Here is my code: public class Main { public static void main(String[] args) {
I have the following code: public class Main { static void swap (Integer x,
I have forgotten the syntax for a generic method: public static void swap <T>
Does a method like this exist anywhere in the framework? public static void Swap<T>(ref
Check this method: public static void Swap(ref int i, ref int j) { int
public void Save() { XmlSerializer Serializer = new XmlSerializer(typeof(DatabaseInformation)); /* A first chance exception
public void EatDinner(string appetizer, string mainCourse, string dessert) { try { // Code }
public void DoPing(object state) { string host = state as string; m_lastPingResult = false;
public void openReport() { try { HashMap params = new HashMap(); params.put(aapor, 19); JasperReport

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.