• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

help with reference variable

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all! I'm new in the forum and I'm studying for the SCJP and plan to take the exam in December. Hopefully we can help each other with any problems that we have.

So far I have this problem: I have 2 ref vars that each refer to 2 different objects, and then I set both variables to refer to the same object. My understanding is that if I make a change in one object's value, the change should be done in the other variable as well, but that is not working in my code.

The code that I have is below. Any suggestions or help is greatly appreciated. Thanks.

------ code below --------

class Test
{
public static void main (String [] args) {

String first = new String("first");
String last = new String ("last");

// display original vals
System.out.println("first: " + first + "\tlast: " + last);

// chg references
last = first;
// this assignment should modify the reference pointed to by "first"
last ="new value";

// test: display first to see if its value has been changed.
System.out.println(first);

}
}
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry buddy. But i would recommend that you start afresh with a book that gives ground level basics of Java.
Because, in your code, you are nowhere changing the state of the Objects(strings) but just changing the References(String variables) to refer to different Objects.
Hence, No Object changed, only Variables(reference).

Thanks.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
leandro, please check your private messages. You can see them by clicking My Profile.
 
today's feeble attempt to support the empire
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic