How many objects in this code?
So lets say I made up a class called Sun, and now I want to create objects of Sun.
Sun happy = new Sun();
Sun sad = new Sun();
Sun neutral = happy();
So at first glance I thought that there was two Sun objects: happy and sun.
I think this because I used a constructor to create 2 new objects of Sun and store them onto the instance variables happy and sad.
I then create an instance variable called neutral of type Sun, which I believe copies the reference stored in happy onto itself.
But now I am starting to think there is 3 objects, because maybe Sun neutral makes a third object even though it stores the same reference as happy.
Basically I have looked at this code so much that I have really confused myself and getting mixed up with the difference between instance variables and objects.
An object is an instance of a class, and an instance variable is a reference to that object, or instance of a class in memory, i think?
So the answer is that there is two objects, being referenced by 3 instance variables?
Am i getting somewhere? lol thanks
Re: How many objects in this code?
Ok the more I look at it the more I believe neutral is just an instance variable of type sun (meaning it is compatible to store other sun objects) so there is only 2 sun objects in the end, that we created with new sun() in the first 2 statements.
Can someone tell me if I am correct?
Re: How many objects in this code?
The posted code uses happy two ways:
one as a variable
one as a method. What does the happy() method do? Where does it get a Sun object to return?
Re: How many objects in this code?
Quote:
Originally Posted by
Norm
The posted code uses happy two ways:
one as a variable
one as a method. What does the happy() method do? Where does it get a Sun object to return?
My bad there is no happy() method, it is supposed to be the existing variable:
Sun happy = new Sun();
Sun sad = new Sun();
Sun neutral = happy;
So we are copying the reference stored in the instance variable happy, into a new variable of type sun called neutral.
So this code has 2 objects, 1 object is referenced by two instance variables happy and neutral) and the other is referenced by sad, is that correct?
Re: How many objects in this code?
You could also test this yourself like I did.
Code:
public class Test {
public static void main(String[] args){
Person a = new Person("Bryan");
Person b = new Person("Thomas");
Person c = a;
System.out.println(a.getName());
System.out.println(b.getName());
System.out.println(c.getName());
c.setName("Derp");
System.out.println(a.getName());
System.out.println(c.getName());
}
}
class Person{
String name;
public Person(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
My answer was that it seemed to copy by reference rather than by value. I assumed that Java was only copy by value.
Re: How many objects in this code?
Quote:
Originally Posted by
Wnt2bsleepin
My answer was that it seemed to copy by reference rather than by value.
Wrong! Java copies the value of the reference. Two references that have the same value refer to the same object.
Objects can't be copied. If the class implements cloneable, its instances can be cloned, but that's a different story.
db
Re: How many objects in this code?
Quote:
Originally Posted by
DarrylBurke
Wrong! Java copies the value of the reference. Two references that have the same value refer to the same object.
Objects can't be copied. If the class implements cloneable, its instances can be cloned, but that's a different story.
db
So it is copying the value of the memory location (value) and assigning it to the new variable (reference). I seem to be getting myself confused between the two. Thanks for the clarification.
Re: How many objects in this code?
Quote:
Originally Posted by
DarrylBurke
Wrong! Java copies the value of the reference. Two references that have the same value refer to the same object.
Objects can't be copied. If the class implements cloneable, its instances can be cloned, but that's a different story.
db
So I was right in the end that there is 2 objects (being referenced by 3 instance variables in total?)
Thanks
Re: How many objects in this code?
Quote:
Originally Posted by
Wnt2bsleepin
You could also test this yourself like I did.
Code:
public class Test {
public static void main(String[] args){
Person a = new Person("Bryan");
Person b = new Person("Thomas");
Person c = a;
System.out.println(a.getName());
System.out.println(b.getName());
System.out.println(c.getName());
c.setName("Derp");
System.out.println(a.getName());
System.out.println(c.getName());
}
}
class Person{
String name;
public Person(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
My answer was that it seemed to copy by reference rather than by value. I assumed that Java was only copy by value.
I figured I would also include the output from the program.
Code:
Bryan
Thomas
Bryan
Derp
Derp
I looked into it more and this is what i seemed to have come up with. I am dealing with C++ as well, so I hope I am not confusing the two.
This is my understanding after reading this
Java apparently makes two copies when you call a method. The original reference and the reference the method is using. Since they both point to the same object, when you modify one you change the other. That would be why the original values are modified by my program below
Code:
public class Test {
public static void main(String[] args){
Person a = new Person("Bryan");
Person b = new Person("Thomas");
Person c = a;
System.out.println(a.getName());
System.out.println(b.getName());
System.out.println(c.getName());
c.setName("Derp");
System.out.println(a.getName());
System.out.println(c.getName());
System.out.println("______________________");
System.out.println(b.getName());
modPerson(b);
System.out.println(b.getName());
}
public static void modPerson(Person person){
person.setName("Modified By person");
System.out.println(person.getName());
}
}
class Person{
String name;
public Person(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
I still feel a little confused by it but I think that is the gist of it.
That means that yes, there are only 2 objects.
Re: How many objects in this code?
Quote:
Originally Posted by
Wnt2bsleepin
So it is copying the value of the memory location (value) and assigning it to the new variable (reference).
No, it is assigning the value of one reference variable to another reference variable. In Java, you don't have to concern yourself with memory locations.
db
Re: How many objects in this code?
So.. in a pseudo-code like version:
Code:
Person a;
Person b = new Person("Kevin");
Person c = b;
line 1: Create a reference variable a, but do not assign it to an object. If 'a' is declared as an instance variable, it is pointing to a 'null' space. (If you don't initialize it later but try to use it, you will get a NullPointerException). If you place that in a method, and try to use it later without initializing, the compiler will be upset and won't compile, saying that Person a may not have been initilaized. At this point, you have one reference variable, zero objects.
line 2: Create reference variable b, point it to object Person which was created with String "Kevin". At this point, we have two reference variables, one object.
line 3: Create reference variable c, assign it the same value as reference variable b has. Meaning, you do not modify the Person object, you just tell java that "Copy the value of ref variable a to ref variable b, I wan them to point to the same object". At this point, we have 3 reference variables, of which 2 are the same (copies of each other), and one object.
Exception: String object. When you do the following:
Code:
String a = "Test";
String b = a;
b = "modified";
System.out.println(a + " " + b);
The compiler will say: (line 1:) Reference variable a pointing to String object "Test". (line 2:) Reference variable b getting the value of Ref variable a (that is, pointing to the same object as a is!). (line 4:) modify the value of the object b is referencing to. BUT since it is String, The compiler is going to create a new String object for you and assign the value "modified" to it.
line 6: Test Modified
Re: How many objects in this code?
Quote:
Originally Posted by
huntack
(line 4:) modify the value of the object b is referencing to. BUT since it is String, The compiler is going to create a new String object for you and assign the value "modified" to it.
Nothing's modified, and the compiler doesn't create objects. Objects exist only at runtime.
Also, all String literals in your code are created as String objects and placed in the String pool when the class is loaded. So, in line 4, b is assigned the value of the reference to the String "modified" which already exists in the String pool.
The only new object created when those 6 lines of code are run is the String "Test modified" concatenated from 3 Strings already in the pool.
db
Re: How many objects in this code?
Quote:
Originally Posted by
DarrylBurke
Nothing's modified, and the compiler doesn't create objects. Objects exist only at runtime.
Also, all String literals in your code are created as String objects and placed in the String pool when the class is loaded. So, in line 4, b is assigned the value of the reference to the String "modified" which already exists in the String pool.
The only new object created when those 6 lines of code are run is the String "Test modified" concatenated from 3 Strings already in the pool.
db
Thanks for clearing that up :-)
Re: How many objects in this code?
Quote:
Originally Posted by
DarrylBurke
The only new object created when those 6 lines of code are run is the String "Test modified" concatenated from 3 Strings already in the pool.
Also, if the variables a and b are either final fields or method-local variables (and hence their value couldn't possibly be changed by another Thread), a modern compiler just might optimize a + " " + b to another String in the pool, and no object at all would be then created during the execution of those lines of code.
Overall, this is not something you need to worry your head about, though. Unless you have a large code that is creating and discarding a very large number of objects -- String or otherwise -- and frequent garbage collection impacts performance. And then, optimization should be carried out only after analyzing and profiling the running code -- not by guesswork.
db