• 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

Interesting Anomaly with toString() vs. Memory Address

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems I heard about this whole object and toString() business when I learned Java, but this is over my head.

I am testing this code:



Seems straight forward enough. When I first found this, I compiled and ran from my desktop. As expected (I guess) the output was

Date = Thu Aug 17 12:23:46 EDT 2006


Great! A way to get the date! So I added this to my jump drive with other archived java programs. It is embedded in several folders.

I tried to test a bit more. Keep in mind the file was a DIRECT copy of the one on the desktop. I recompiled (because I'm OCD about that) and here's what I got:
Date = Date@108786b

A memory address as opposed to the actual date. Now....why??

My first thought was removable media. I copied the file back to the desktop and got the true date again. I copied THAT file to the root of the jump drive: TRUE DATE, which kills the removable media theory.

My next test was to see if the difference occured at compile or run time. I copied a java AND class file from the desktop (compiled there) to my embedded directory: TRUE DATE. which says to me, it seems to matter WHERE i'm compiling.

Again, it works on the desktop and root of jump drive, but not inside some directories on the jump drive. Also, tests indicate that the difference is occuring at compile time.
(For some reason, I can't try the test backwards: NoClassDef Found if I copy the class and java TO the desktop, so I have to recompile which defeats the purpose)


Any ideas? I just am wondering how many times I got so frustrated at a program that wasn't working the way it was supposed to -- all because it was in some folder that Java didn't like especially well!

Thanks
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way to get this result would be to define your own Date class that doesn't define a nice toString(). I'm not sure why you'd pick it up some times and not others.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw, it's not a memory address at all, it's a hashcode
 
I like tacos! And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic