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

  • Home
  • SEARCH
  • 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 1087211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:54:34+00:00 2026-05-16T22:54:34+00:00

I am trying to swap two strings in Java. I never really understood strings

  • 0

I am trying to swap two strings in Java. I never really understood “strings are immutable”. I understand it in theory, but I never came across it in practice.

Also, since String is an object in Java and not a primitive type, I don’t understand why the following code prints the same result twice, instead of interchanging the words!

public static void main(String[] args)
{
    String s1 = "Hello";
    String s2 = "World";

    System.out.println(s1 + " " + s2);

    Swap(s1, s2);

    System.out.println(s1 + " " + s2);
}

public static void Swap(String s1, String s2)
{
    String temp = s1;
    s1 = s2;
    s2 = temp;
}

I want it to print

Hello World
World Hello

But it is printing

Hello World
Hello World

I thought s1 and s2 are references and hence the references should be swapped and the new ones should point to the other one respectively. Where am I going wrong?

  • 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-05-16T22:54:35+00:00Added an answer on May 16, 2026 at 10:54 pm

    I thought s1 and s2 are references and hence the references should be swapped and the new ones should point to the other one respectively.

    Yes. Locally inside swap, this is exactly what happens.

    However, s1 and s2 are copies of the references passed into the function, so the effect remains local. Note that it’s not the strings that are copied (since String is a reference type). But the references are copied.

    … and since parameter references are always copied in Java, writing a swap function according to your specifications is, quite simply, not possible.

    If you have problems understanding the difference, consider this: you want to write a letter to a friend so you copy her postal address from your address book onto an envelope. By this process, you certainly didn’t copy her home (copying a whole house is a bit difficult in practice) – you only copied the address.

    Well, an address refers to her home so it’s exactly like a Java reference.

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

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 540k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is called the Peek-a-boo bug. Here is an explanation… May 17, 2026 at 2:30 am
  • Editorial Team
    Editorial Team added an answer Ok, so it turns out the server was running a… May 17, 2026 at 2:30 am
  • Editorial Team
    Editorial Team added an answer You can use +[NSEvent addGlobalMonitorForEventsMatchingMask:handler:], but it's not quite the… May 17, 2026 at 2:30 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to swap two strings but I'm not sure if what I am
I am trying to 'swap' two cells' contents, and their mappings. To do this,
I'm trying to swap two variables in ActionScript. I tried using: a = 42
I am trying to swap two LinearLayouts by setting their visibility properties to VISIBLE
I'm trying to swap two std::list< dontcare* >::iterators under Visual 2005. Iter it1 =
I'm trying to figure out how to use RJS to swap between two fields...
I am just curious if there is a way to swap the two stings
I'm trying to use lambda syntax to be able to swap out search rule
First and foremost, I do not know RegEx but am trying to piece something
I'm trying to swap objects for a homework problem that uses void pointers to

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.