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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:39:30+00:00 2026-06-18T19:39:30+00:00

check this code public class StringbuilderDoubt { public static void main(String args[]){ new StringbuilderDoubt().methodTest();

  • 0

check this code

   public class StringbuilderDoubt {

    public static void main(String args[]){
           new StringbuilderDoubt().methodTest();
    }

    public void methodTest(){
        String str=new String("Default");
        StringBuilder sb=new StringBuilder();
        sB1(str,sb);
        sB2(str,sb);
        System.out.println(str+".........."+sb);
    }
    private void sB1(String str1, StringBuilder sb1){
                    str1+="str1";
                    sb1.append("sB1");
    }

    private void sB2(String str2, StringBuilder sb2){
        str2+="str2";
        sb2.append("sB2");
    }
}

Output is: Default..........sB1sB2
Why StringBuilder is working like pass by reference?
In methodTest I am passing two objects string and string builder. String is pass by value but StringBuilder looks like pass by refence and StringBuilder object from calling method getting changed.

  • 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-18T19:39:32+00:00Added an answer on June 18, 2026 at 7:39 pm

    When you pass a StringBuilder instance to a method, the instance value is passed by value. As you’ve seen, you can append characters to the StringBuilder instance inside of the method without returning the instance value.

    You can also return the StringBuilder instance value if you want, but it’s not necessary. It hasn’t changed.

    This “pass by reference” also works for most of the Collection classes (List, Map), and for your own classes that have getters and setters.

    If you don’t want this behavior, you have to make deep copies of the instances you pass. You would do this if you return a Collection class from a public API.

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

Sidebar

Related Questions

I wrote this code: public static void main(String args[]) throws Exception { String template
This is my code: public class MyCollection { internal static Dictionary<string, CheckBox> MyCheckBox =
This is my simple code. public class Permission { public string Name { get;
following is the code listed in MainClass.java. public class MainClass { public static void
I already have the following code public class Qn3 { static BigDecimal[] accbal= new
Okay, check this source code: public void checkSession() { SharedPreferences session = getSharedPreferences(PREFS_NAME, 1);
Hola check this code. var watch = new Stopwatch(); watch.Start(); var request = HttpWebRequest.Create(new
I check like this: enter code here var point = new g.LatLng(parseFloat(lat),parseFloat(lng)); var bounds
i have this code that will check the array contains the specific string and
I have this code that will check for if the class more-results are in

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.