title: C++ References and Java References
body: <p>//C++ Example</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int doHello (std::string&amp;);
  int main() {
      std::string str1 = "perry";
      cout &lt;&lt; "String=" &lt;&lt; str1 &lt;&lt; endl;
      doHello(str1);
      cout &lt;&lt; "String=" &lt;&lt; str1 &lt;&lt; endl; // prints pieterson
      return 0;
  }
  
  int doHello(std::string&amp; str){
      str = "pieterson";
      cout &lt;&lt; "String=" &lt;&lt; str &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>In the above case, as expected the string 'str1' reference is modified when str reference is modified</p>
  
  <p>//Java Example</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>How can we achieve C++ style functionality in Java with Strings, Collections such as List, Vectors, other Objects.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>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. </p>
  
  <pre><code>//bad doHello() 
  
  void doHello(List inputList) { 
      inputList = getListFromAnyFunction(); // wrong, didnt work 
  } 
  
  // good doHello 
  
  void doHello(List inputList) { 
      inputList.addAll(getListFromAnyFunction()); // worked 
  }
  </code></pre>
  
  <p>Thanks Powell and Harshath for your explainations and code samples.</p>