Setting an object to equal another object
Page 1 of 110 Replies - 303 Views - Last Post: 05 October 2012 - 06:23 AM
#1
Setting an object to equal another object
Posted 04 October 2012 - 05:58 PM
Replies To: Setting an object to equal another object
#2
Re: Setting an object to equal another object
Posted 04 October 2012 - 06:03 PM
#3
Re: Setting an object to equal another object
Posted 04 October 2012 - 06:16 PM
#4
Re: Setting an object to equal another object
Posted 04 October 2012 - 06:20 PM
a == b;
#5
Re: Setting an object to equal another object
Posted 04 October 2012 - 08:18 PM
ILoveJava, on 04 October 2012 - 09:03 PM, said:
equals() when using ANY object not only String
ILoveJava, on 04 October 2012 - 09:20 PM, said:
a == b;
Depends if you want to compare String pointer or String contain
d.koen, on 04 October 2012 - 09:16 PM, said:
this would perform a boolean comparison instead of setting a to the value of b?
No that will make String a to point to String b
it won't compare anything
#6
Re: Setting an object to equal another object
Posted 04 October 2012 - 08:19 PM
ILoveJava, on 04 October 2012 - 08:20 PM, said:
a == b;
Other way around.
== compares, = assigns.
Examples. Let's all sing along now!
int a = 1;
int b = 2;
if (a == 17) // a == 17 evaluates to false
{
a = b; // result would be a == 2 and b == 2
}
else // but since a != 17, this happens:
{
b = a; // now b == 1 and a == 1
}
if (a == B)/>
System.out.println("I understand comparisons now!");
This post has been edited by jon.kiparsky: 04 October 2012 - 08:22 PM
#7
Re: Setting an object to equal another object
Posted 04 October 2012 - 08:55 PM
jon.kiparsky, on 04 October 2012 - 11:19 PM, said:
ILoveJava, on 04 October 2012 - 08:20 PM, said:
a == b;
Other way around.
== compares, = assigns.
Examples. Let's all sing along now!
int a = 1;
int b = 2;
if (a == 17) // a == 17 evaluates to false
{
a = b; // result would be a == 2 and b == 2
}
else // but since a != 17, this happens:
{
b = a; // now b == 1 and a == 1
}
if (a == B)/>
System.out.println("I understand comparisons now!");
That's what I thought, I read a==b would be needed for assignment and I was like what happened to my brain? Been spending the last 20 minutes split between finishing the assignment (which I finally got to work correctly) and researching online to make sure I wasn't completely losing my mind. Thanks for clearing it up so cleanly, sadly I must be the only one this confused because a search on Google ONLY pulled up things about .equals() and == but nothing on = vs ==. Glad I haven't lost my marbles yet.
#8
Re: Setting an object to equal another object
Posted 04 October 2012 - 09:02 PM
d.koen, on 04 October 2012 - 08:16 PM, said:
No, this would perform an assignment. Now the value of the variable a is equal to the value of the variable b.
What does that mean? It's not exactly what you think it means. This is neat, and it's alsom important, so pay attention.
We think of a variable as an association of a name to a value, but that's not exactly correct. A variable is an association of a name to a piece of memory. What we think of as the "value" is stored at that location.
The value of a primitive, like and int or a char or a double, is just the literal value that you give it.
double pi = 3.14; // that's close enough...
means "name a memory location pi, set it aside for doubles, and store the literal value 3.14 in that location".
So if I have
int x = 31; int y = x;
then what I mean is "evaluate the variable x and put that value in the spot marked y". First we evaluate the right hand side of the assignment, and get a value, and then we write that value to the location on the left hand side of the assignment.
Okay, so far so good. You can now do things like
int z = 1+ (2/3) - 4 * 5 + x;
and you know what's happening: you evaluate the right and put it in the location referred to on the left.
So what about
String a = "string a"; String b = "string b"; a = b;
Well, exactly the same thing happens, except now the values in a and b are not primitives, so it looks different, and you'll have to act as though it's different. (but it's not really different!)
What do a and b contain in this case? They don't contain Strings. Strings are much too big to fit on the stack, which is where a and b are pointing. They contain memory addresses, which point to the String objects somewhere else in the machine's memory. So a is a name which means a location, and when you look at what's stored at that location, you get an address, and when you go to that location, you get your object. Schematically:
double pi --> 3.14
String a --> some address, like maybe 27ce2dd4 --> "string a"
Okay, so what happens when you do your a = b?
Well, the value of a becomes equal to the value of b. Since the value of b is an address in memory (not a String!) now a and b contain the same address. This means that a and b point to the same object. (and the String that was in a before is now lost, to be collected by the garbage collector)
In other words, a and b are not equal, they're identical - they are different names for the same thing. As it turns out, this doesn't mean a lot for Strings because of the way Java handles Strings (see "string interning") but if you have objects whose state can change, this can produce weird or useful effects.
For example, if you have a Dog object which has methods setName and getName, you might see something like this:
Dog fido = new Dog();
fido.setName("Fido");
Dog rover = fido;
fido.getName(); // returns "Fido"
rover.getName(); // returns "Fido"
rover.setName("Rover");
rover.getName(); // returns "Rover"
fido.getName(); // returns "Rover" (do you understand why?)
Long answer to a question you didn't ask. Life's like that some times. I promise you this will be useful to you.
#9
Re: Setting an object to equal another object
Posted 04 October 2012 - 10:47 PM
jon.kiparsky, on 05 October 2012 - 12:02 AM, said:
d.koen, on 04 October 2012 - 08:16 PM, said:
No, this would perform an assignment. Now the value of the variable a is equal to the value of the variable b.
What does that mean? It's not exactly what you think it means. This is neat, and it's alsom important, so pay attention.
We think of a variable as an association of a name to a value, but that's not exactly correct. A variable is an association of a name to a piece of memory. What we think of as the "value" is stored at that location.
The value of a primitive, like and int or a char or a double, is just the literal value that you give it.
double pi = 3.14; // that's close enough...
means "name a memory location pi, set it aside for doubles, and store the literal value 3.14 in that location".
So if I have
int x = 31; int y = x;
then what I mean is "evaluate the variable x and put that value in the spot marked y". First we evaluate the right hand side of the assignment, and get a value, and then we write that value to the location on the left hand side of the assignment.
Okay, so far so good. You can now do things like
int z = 1+ (2/3) - 4 * 5 + x;
and you know what's happening: you evaluate the right and put it in the location referred to on the left.
So what about
String a = "string a"; String b = "string b"; a = b;
Well, exactly the same thing happens, except now the values in a and b are not primitives, so it looks different, and you'll have to act as though it's different. (but it's not really different!)
What do a and b contain in this case? They don't contain Strings. Strings are much too big to fit on the stack, which is where a and b are pointing. They contain memory addresses, which point to the String objects somewhere else in the machine's memory. So a is a name which means a location, and when you look at what's stored at that location, you get an address, and when you go to that location, you get your object. Schematically:
double pi --> 3.14
String a --> some address, like maybe 27ce2dd4 --> "string a"
Okay, so what happens when you do your a = b?
Well, the value of a becomes equal to the value of b. Since the value of b is an address in memory (not a String!) now a and b contain the same address. This means that a and b point to the same object. (and the String that was in a before is now lost, to be collected by the garbage collector)
In other words, a and b are not equal, they're identical - they are different names for the same thing. As it turns out, this doesn't mean a lot for Strings because of the way Java handles Strings (see "string interning") but if you have objects whose state can change, this can produce weird or useful effects.
For example, if you have a Dog object which has methods setName and getName, you might see something like this:
Dog fido = new Dog();
fido.setName("Fido");
Dog rover = fido;
fido.getName(); // returns "Fido"
rover.getName(); // returns "Fido"
rover.setName("Rover");
rover.getName(); // returns "Rover"
fido.getName(); // returns "Rover" (do you understand why?)
Long answer to a question you didn't ask. Life's like that some times. I promise you this will be useful to you.
Oh I don't doubt it's usefulness one bit. Although I've been using C++ now for about 9 years so I understand variables pointing to memory and memory containing the "values" of the variable. It's the Java component that drives me insane. The reason fido's name changes when you set rovers name to Rover is because they're the same object in both directions, you change fido you change rover, you change rover you change fido. Is there any way you would be able to set rover to fido as far as value or is the only option address? I know when you say Dog fido = new Dog() it creates a new Dog object with it's own memory address. I guess it wouldn't make any difference to make the same statement for rover before assignment would it? It would still change the address it points to only.
#10
Re: Setting an object to equal another object
Posted 05 October 2012 - 03:53 AM
The == is exclusively a value compare, period. If the values are objects, then it compares if they are the identical instance. This never changes. So, for object comparison, using .equals is the only safe way to go.
You want to make copies of an object, do it. You don't get to overload copy and assignment operators, but that doesn't stop you from making a copy constructor. You want to copy a Dog, no worries.
Dog fido = new Dog("Fido");
Dog rover = fido; // obviously, same instance
// this is true: rover == fido
// new instance
// this should be true: rover.equals(fido);
// this should be false: rover == fido
rover = new Dog(fido);
// in your class
class Dog {
public Dog(String name) { //...
public Dog(Dog other) { //...
// also
public boolean equals(Object other) { //...
#11
Re: Setting an object to equal another object
Posted 05 October 2012 - 06:23 AM
d.koen, on 05 October 2012 - 12:47 AM, said:
Here's where thinking about the memory like a C programmer will help you. If you change fido, you change only fido! That is, if you say
fido = new Dog();
rover is not touched. But if you ask fido to change - then rover changes as well.
fido.sit();
Now Rover expects a treat.
In some ways the problems are exactly the same as in C, but Java takes care of a lot of the details for you. This makes it easier for the beginner to get along without knowing the details, which is a problem, but it means there's a lot of mistakes that you don't have a chance to make, which is nice.
Quote
Access to an object doesn't guarantee access to its fields, and you don't have memory-level access to the heap, so the only way to make a deep copy of an object, as Baavgai says, is if the class provides a mechanism for this. The copy constructor seems to me like the best semantics for this.
|
|

New Topic/Question
Reply




MultiQuote




|