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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:18:09+00:00 2026-06-08T06:18:09+00:00

I know that in Java, everything is passed by value. But for objects, it

  • 0

I know that in Java, everything is passed by value. But for objects, it is the value of the reference to the object that is passed. This means that sometimes an object can get changed through a parameter, which is why, I guess, people say, Never modify parameters.

But in the following code, something different happens. s in changeIt() doesn’t change when you get back to main():

public class TestClass {

    static String str = "Hello World";

    public static void changeIt( String s ) {
        s = "Good bye world";
    }

    public static void main( String[] args ) {
        changeIt( str );
        System.out.println( str );
    }
}

I’m guessing — and I’d like confirmation — that when you say s = "something" it’s the same or equivalent to saying String s = new String("something"). Is this why s doesn’t change? Is it assigned a whole new object locally which gets thrown away once you exit changeIt()?

  • 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-08T06:18:10+00:00Added an answer on June 8, 2026 at 6:18 am

    that when you say s = “something” it’s the same or equivalent to saying String s = new String(“something”)

    Yes, pretty much. (though the JVM might do optimizations so that the same string literal used several times refers to the same String object).

    Is this why s doesn’t change? Is it assigned a whole new object locally which gets thrown away once you exit changeIt()

    Yes. As you say, everything is passed by value in Java, even references to object. So the variable s in changeIt( String s ) is a different value from str you use in main(), it’s just a local variable within the changeIt method.
    Setting that reference to reference another object does not affect the caller of changeIt.

    Note that the String object s refer to is still the same String as str refers to when entering the changeIt() method before you assign a different object to s

    There’s another thing you need to be aware of, and that is that Strings are immutable. That means that no method you invoke on a string object will change that string. e.g. calling s.toLowerCase() within your changeIt() method will not affect the caller either. That’s because the String.toLowerCase() does not alter the object, but rather returns a new String object.

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

Sidebar

Related Questions

Now I know that Java is purely passed-by-value, but are instance variables passed by
I know that Java have its own garbage collection, but sometimes I want to
We know that we can use a concept Java Package, but I just wanted
I know that Java's implementation does everything it can to hide that information from
OK - I know that Java generics can be a minefield for the unwary,
I know that in Java you can specify the return type of the DoInBackground
I know that in GWT the java reflection tools isn't emulated, but is there
As far as I know, everything is an object in Java. I am looking
I'm trying to use JAXB to unmarshal this file into Java objects. I know
I know that Java enforce the programmer to list all exceptions that will be

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.