• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why the value of the same variable changed?

 
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the test class:



The output in console look like this:


I expected the output of sent1 should be the same as sent2. But why they differ? Could anyone please kindly enlighten me? Thank you very much in advance!


Regards,
Ellen
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. Do you have the source code for this CodeWordVector class?
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Both of the above classes passed their unit tests. Now this class has been frustrating me...Let me give out the code now, I'll post a picture of the class diagram right away...



The input of this class is a received code word, which is an array of 15 GFElements. The output of this class is the sent code word, which is supposed to be the same as the received if error free. For now there is no falsification added, so the received code word, after the decoding, should equal the sent code word...
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can find the class diagram here. It's a pdf file.
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I forgot to comment out a line in the decoder class. Now the sent1 and sent2 problem has been fixed. But assertion still failed...well there must still be bugs in the implementation of the decoding algorithm...Junit worked just fine, many thanks!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I see two problems in your code, which seem to be based on a basic misunderstanding.

Take, for example, the lines

[code]
CodeWordVector sent = new CodeWordVector();
sent = this.received;


I don't think they are doing what you think they do.

You need to remember that both sent and received are *references* to objects, not the actual objects themself. That is, the assignment in the second line has both references point to the same object. Now when you do something to sent or received, it will be seen by using the other reference, too. In the same vein, the first line doesn't have any effect at all - the object that is created via new will no longer be referenced after the second line is executed, and will therefore be garbage collected.

Hope this helps...
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes good sense. I think the references of objects in the decoder class is somehow messed up, not working like the algorithm supposed to do. Wait a second, help me to clear up something:

Say, in the ...DecoderTest class, firstly the CodeWordVector received was created, let's assume at address 0x0004. Then this object was then made to store a bunch of references to EFElement objects like the setUp() method did. But it still lies at 0x0004. Then a new object "sent" was created at 0x0008 (egal wo). Then the address 0x0004 was passed to "sent" so now both sent and receive point to the same address. The address 0x0008 is available to other objects (egal was). Then this happened:

another object called result was created at 0x000c. But then after the bmd.decode(received), the "result" may well be then pointing to another address say 0x0010. (The object which lies at 0x0010 was created during the decoder class was executed.). Now, both sent and received are still pointing to 0x0004, result is pointing to 0x0010...

Am I thinking right? And is there any way in the java language to verify the address of an object? In another word, how to get the physical address of an object in java? :roll: Thank you very much in advance!


Regards,
Ellen
[ February 20, 2005: Message edited by: Ellen Zhao ]
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bang~~! I see what's really stupid now... What I really want to do is to set the values of the newly created object and update the values during the computation but not to assign reference to it! I should have used get() and set() method everywhere in the decoder class!!!

Well, I'm just working on rewriting a program which was written in a procedure language, was a little bit affected by its different grammar and semantic...now I get it... Ilja YOU ROCK!
[ February 21, 2005: Message edited by: Ellen Zhao ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome! Nice to see that you found it using the little hint I gave...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic