Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2008
    Posts
    8
    Rep Power
    0

    Question [SOLVED] HELP! Is this BUG? System.out.println() always shows "@9304b1"

    Hi...I'm new to Java...

    I've done some little coding to test how Java works using System.out.println()
    But the result are rather unecpected weird, it always print same false result.
    Could anyone please help me, what am I doing wrong in the code. Thx in advance.

    I've tested it in Eclipse & intelliJ IDEA
    with jdk1.6.0_07

    intelliJ IDEA Result :
    Java Code:
    circleToy@9304b1
    Eclipse Result :
    Java Code:
    circleToy.circleToy@3e25a5
    Code :
    Java Code:
    public class circleToy {
        private double radius;
    
        public circleToy() {
            radius = 0.0;
    
        }
    
        public circleToy(double radius) {
            this.radius = radius;
        }
    
        public void setRadius(double radius) {
            this.radius = radius;
        }
    
        public double getRadius() {
            return radius;
        }
    
        public double getDiameter() {
            return calcDiameter();
        }
    
        private double calcDiameter() {
            return radius * 2;
        }
    
        static void test() {
    
            circleToy a = new circleToy(20);
            System.out.println(a);
    
    
        }
    
        public static void main(String[] args) {
            test();
        }
    }
    The correct result for this code should be "40" ...
    Am I wrong ?
    calcDiameter = (radius * 2)
    calcDiameter (20 * 2) = 40

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    This is not a bug. Its normal.
    You are seeing the default action of the toString() function in Object, which is the base class of all classes.

    If you want to see something else, your class must override toString() with whatever you want.

    Something like:

    Java Code:
    public String toString()  {
       String rval = "" + radius;
       return rval;
    }

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    You are trying to print the Object in the heap,so when you try to print the object ,the JVM's behaviour is printing the address in hexa of the Object

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by serjant View Post
    You are trying to print the Object in the heap,so when you try to print the object ,the JVM's behaviour is printing the address in hexa of the Object
    That's not quite right. Whether its on the heap or not is irrelevant. Its that any Object gets its address printed, so if you want something else, you need to override toString()

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Quote Originally Posted by fishtoprecords View Post
    That's not quite right. Whether its on the heap or not is irrelevant. Its that any Object gets its address printed, so if you want something else, you need to override toString()
    Yes ,but my answer was not aimed on whether the object is in the heap or not,my answer was pointed to the address Object printing in hexa.But in this case the address of the object which is in the heap is printed.

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by serjant View Post
    Yes ,but my answer was not aimed on whether the object is in the heap or not,my answer was pointed to the address Object printing in hexa.But in this case the address of the object which is in the heap is printed.
    I don't know what you are arguing about. The address is printed because the creator of the class did not override toString().

    Heap has nothing to do with it. It happens to be the address in hex in most JVM implementations, but it its (1) not guaranteed and (2) you can't use the address information for anything useful.

  7. #7
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    1.That's was my point ,that heap has no meaning here.
    But for you information,as i remember from my Java courses in the university,the JVM's heap stores all objects created by an executing Java program.,so i answered correclty on the asked question,that address of the object which was created and stored in the HEAP and not somewhere else is printed.
    I recommend you to read about that:
    JVM's Heap
    2.Of course in Java ,the address info is not useful at all.
    Last edited by serjant; 08-18-2008 at 11:53 PM.

  8. #8
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by serjant View Post
    so i answered correclty on the asked question
    No your answer is not correct and more importantly, its not useful.
    The problem that the OP has is that he's getting something that is not expected, and the solution is to override toString().

    Please go argue with someone interested in your vision of the world

  9. #9
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    I don't refuse your answer with overriding toString(),that is the solution and i only posted what his program outputs.
    I think now our argue is more clear.I am here to help and to know something new,and not to argue.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Hey man, be cool here. Argue is much better when we discuss interesting points on anything. But don't get angry. ;)

    I'd like to comment on this question too.

    The output what our thread starter get is when the toString() is not overridden of class object. Basically there is no meaning at all of the output. But it gives the class name, which is the only meaningful part. Then it follows @ sign. Then followed by the unsigned hexadecimal value of the object's hash code.

    Hope it's clear. If anyone wants I can comment this with an example. :)

  11. #11
    Join Date
    Aug 2008
    Posts
    8
    Rep Power
    0

    Default

    Thanks for your help guys...

    Those informations about heap, memory address, hexa and toString() really helps me understanding Java...

    Happy coding. :D

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 01:20 AM
  3. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 AM
  4. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •