pankajt pankajt - 3 years ago 62
C++ Question

C++ References and Java References

//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.

Answer Source

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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download