This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between Reference Data Types & Own Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Difference between Reference Data Types & Own Objects" Watch "Difference between Reference Data Types & Own Objects" New topic
Author

Difference between Reference Data Types & Own Objects

craig morgan
Greenhorn

Joined: Aug 18, 2006
Posts: 3
I am trying to understand the difference between a reference data type and an object in terms of passing values. What I am struggling with is the fact that reference data types, such as a String are actually Objects. Why is it then that if I do this...



...then String a becomes "Dog" and String B remains "Cat".
However, if I create a Cat Class with a private String speak with getter and setter methods and do this...


...then B effectively references A which means and both return the same value ("Meaow").

If I then set both A & B hold the value = "Meaow Meaow".

So the question is - Why does my own Object operate differently to a String Object (or other reference type)?

Thanks!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

This has nothing to do with the objects, but how you are using them.

In the string case, you are changing one of the references to point to a different object. In the second case, you are changing the object itself -- and since the two references point to the same object, they should both change.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dan Patterson
Greenhorn

Joined: Jan 27, 2005
Posts: 24
Cat A = new Cat();
Cat B = A; <= This line is not creating a new object, it is making "B" a refernece to the already created object that Variable A is pointing to.

Cat A and B are reference variables that are referencing the same object which is created in the new Cat() line.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 244
First example

changes the variable a (reference) not the String object itself (which cannot be changed). b will still reference to the old String.

Second example

changes the Cat object not the variable A.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Your String example would be equivalent to



If you use that code, you get exactly the same behaviour as in the String example.

You can't change the String example to make it work like your first Cat example, simply because String is immutable - it doesn't have any getters. In contrast, StringBuffer has, and could be used to show the same behaviour.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
craig morgan
Greenhorn

Joined: Aug 18, 2006
Posts: 3
Thanks Ilja - finally a reply which makes sense!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between Reference Data Types & Own Objects
 
Similar Threads
Another examLab casting question
about == and equals()
Inheritance, overriding methods
Help with assigning references
Collection Example in K&B