10 Replies - 731 Views - Last Post: 05 October 2012 - 06:23 AM Rate Topic: -----

#1 d.koen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 08-November 10

Setting an object to equal another object

Posted 04 October 2012 - 05:58 PM

I am currently working on an inventory management program for my Java course in college. We are now modifying the program to sort the array by product name and I'm having trouble finding a method to switch the values in the array between objects such as array[1] and array[2]. I come from a C++ background so I'm used to overloading operator= to get this done but from what I understand Java doesn't support this and I hear that using clone() is a bad idea. I was thinking of using a buffer to store the initial value, setting the initial value to the second value and then setting the second value to the buffer. Thing is this would be quick if it were variables being swapped instead of whole objects. Instead I would have to perform the swap for each variable contained in the class. Is there a good API method to do this better or should I simply create my own?

Is This A Good Question/Topic? 0
  • +

Replies To: Setting an object to equal another object

#2 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Setting an object to equal another object

Posted 04 October 2012 - 06:03 PM

In Java, '=' is '==', or .equals() when using strings.
Was This Post Helpful? 0
  • +
  • -

#3 d.koen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 08-November 10

Re: Setting an object to equal another object

Posted 04 October 2012 - 06:16 PM

View PostILoveJava, on 04 October 2012 - 09:03 PM, said:

In Java, '=' is '==', or .equals() when using strings.


so if I have the following:

String a = "string a";
String b = "string b";

a = b;

this would perform a boolean comparison instead of setting a to the value of b?
Was This Post Helpful? 0
  • +
  • -

#4 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Setting an object to equal another object

Posted 04 October 2012 - 06:20 PM

That would just simply compare their contents. If you want to make String a = String b. You would need to do
a == b;
Was This Post Helpful? -3
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Setting an object to equal another object

Posted 04 October 2012 - 08:18 PM

View PostILoveJava, on 04 October 2012 - 09:03 PM, said:

In Java, '=' is '==', or .equals() when using strings.

equals() when using ANY object not only String

View PostILoveJava, on 04 October 2012 - 09:20 PM, said:

That would just simply compare their contents. If you want to make String a = String b. You would need to do
a == b;

Depends if you want to compare String pointer or String contain

View Postd.koen, on 04 October 2012 - 09:16 PM, said:

a = b;

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
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,084
  • Joined: 19-March 11

Re: Setting an object to equal another object

Posted 04 October 2012 - 08:19 PM

View PostILoveJava, on 04 October 2012 - 08:20 PM, said:

That would just simply compare their contents. If you want to make String a = String b. You would need to do
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

Was This Post Helpful? 0
  • +
  • -

#7 d.koen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 08-November 10

Re: Setting an object to equal another object

Posted 04 October 2012 - 08:55 PM

View Postjon.kiparsky, on 04 October 2012 - 11:19 PM, said:

View PostILoveJava, on 04 October 2012 - 08:20 PM, said:

That would just simply compare their contents. If you want to make String a = String b. You would need to do
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.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,084
  • Joined: 19-March 11

Re: Setting an object to equal another object

Posted 04 October 2012 - 09:02 PM

View Postd.koen, on 04 October 2012 - 08:16 PM, said:

View PostILoveJava, on 04 October 2012 - 09:03 PM, said:

In Java, '=' is '==', or .equals() when using strings.


so if I have the following:

String a = "string a";
String b = "string b";

a = b;

this would perform a boolean comparison instead of setting a to the value of b?



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.
Was This Post Helpful? 0
  • +
  • -

#9 d.koen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 08-November 10

Re: Setting an object to equal another object

Posted 04 October 2012 - 10:47 PM

View Postjon.kiparsky, on 05 October 2012 - 12:02 AM, said:

View Postd.koen, on 04 October 2012 - 08:16 PM, said:

View PostILoveJava, on 04 October 2012 - 09:03 PM, said:

In Java, '=' is '==', or .equals() when using strings.


so if I have the following:

String a = "string a";
String b = "string b";

a = b;

this would perform a boolean comparison instead of setting a to the value of b?


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.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5829
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Setting an object to equal another object

Posted 05 October 2012 - 03:53 AM

I think the best thing you can do is empty your glass. Java is not C++; new language, new rules. Forget C++ and make room for Java. Sure, there are little hidden pointer thingies going on behind the scenes, forget that. Java programmers don't think about pointers. Operator overloads? Forget that, no mystical operations in Java.

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) { //...


Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,084
  • Joined: 19-March 11

Re: Setting an object to equal another object

Posted 05 October 2012 - 06:23 AM

View Postd.koen, on 05 October 2012 - 12:47 AM, said:

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.


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

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.


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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1