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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:10:34+00:00 2026-05-11T21:10:34+00:00

//C++ Example #include <iostream> using namespace std; int doHello (std::string&); int main() { std::string

  • 0

//C++ Example

#include <iostream>
using namespace std;

int doHello (std::string&);
int main() {
    std::string str1 = "perry";
    cout << "String=" << str1 << endl;
    doHello(str1);
    cout << "String=" << str1 << endl; // prints pieterson
    return 0;
}

int doHello(std::string& str){
    str = "pieterson";
    cout << "String=" << str << endl;
    return 0;
}

In the above case, as expected the string ‘str1’ reference is modified when str reference is modified

//Java Example

public class hello {

    public static void main(String args[]){
        String str1 = "perry";
        System.out.println("String=" + str1);
        doHello(str1);
        System.out.println("String=" + str1); // does not prints pieterson
    }

    public static void doHello(String str){
        str = "pieterson";
        System.out.println("String = " + str);
    }
}

In Java, String str and String str1 are two different objects initially referencing the same String, so when we change str reference in doHello(), str1 reference does not get changed.

How can we achieve C++ style functionality in Java with Strings, Collections such as List, Vectors, other Objects.

Update:

Thanks Jon for a wonderful explaination, I believe any Java beginner will surely come across this question.
Let me explain what problem i ran into while using lists.

//bad doHello() 

void doHello(List inputList) { 
    inputList = getListFromAnyFunction(); // wrong, didnt work 
} 

// good doHello 

void doHello(List inputList) { 
    inputList.addAll(getListFromAnyFunction()); // worked 
}

Thanks Powell and Harshath for your explainations and code samples.

  • 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-11T21:10:34+00:00Added an answer on May 11, 2026 at 9:10 pm

    Java does not have pass by reference (which you were using in the C++ code) at all. The references are passed by value. (The values of str and str1 aren’t objects at all, they’re references – it really helps to keep the two concepts very separate.)

    Typically you would use a return value to return a new reference if you need to:

    str1 = doHello(str1);
    

    Note that String is slightly different to List etc, because strings are immutable. To modify a collection (well, any mutable collection) you don’t need to create a new one, you just modify the object via the original reference:

    public static void addHello(List<String> items)
    {
        items.add("Hello");
    }
    

    You could then call this like so:

    List<String> list = new ArrayList<String>();
    addHello(list);
    System.out.println(list.get(0)); // "Hello"
    

    The difference between mutating an existing object and changing the value of a variable to refer to a different object is crucial. If you want to leave the existing collection alone and create a new one, you’d have to do that explicitly:

    public static List<String> withHello(List<String> items)
    {
        List<String> newList = new ArrayList<String>(items);
        newList.add("Hello");
        return newList;
    }
    

    You’d then call it like this:

    List<String> empty = new ArrayList<String>();
    List<String> newList = withHello(empty);
    System.out.println(empty.size()); // Prints 0
    System.out.println(newList.size()); // Prints 1
    

    Does this answer everything you needed?

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Stats are not automatically updated until the stats are needed… May 12, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer No, there can be multiple requests in the same thread… May 12, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer There are two ways to handle this. The first is… May 12, 2026 at 10:50 pm

Related Questions

Looking on the internet for C++ brainteasers, I found this example: #include <iostream> using
I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast
For school, we use C++ as the language of choice. I am currently using
Static metaprogramming (aka template metaprogramming) is a great C++ technique that allows the execution

Trending Tags

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

Top Members

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.