Results 1 to 6 of 6
  1. #1
    goose87 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Simplest code won't work, help needed

    Hello,
    I'm scratching my head as I can't get any meaningful output from this very simple piece of code. (I'm following SCJP study guide and am on p90 for anyone who has the book).

    class Test {
    public static void main (String [] args) {
    Test t1 = new Test();
    Test t2 = new Test();
    if(!t1.equals(t2))
    System.out.println("they're not equal");
    if (t1 instanceof Object)
    System.out.println("t1's an object");
    }
    }

    It compiles fine but won't give me any output apart from 'exited with status 0'. Can anyone see where I've gone wrong?

    I'm using xcode to run it. I'm a beginner programmer so I suspect I'm subject to some very simple errors.

    Any help appreciated.

    Thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Your class Test doesn't override the equals( ... ) method so different objects are certainly not equal. Class Test extends (implicitly) class Object so a Test object most certainly is an Object. Check your xcode runtime settings.

    kind regards,

    Jos
    Last edited by JosAH; 10-24-2010 at 04:38 PM. Reason: little typo ...

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It compiles and runs just fine for me, so I see no problem at all with the code. Perhaps it's how you're trying to run it (just a SWAG).

    Also a recommendation: place all loops and blocks in curly braces. Doing this will save your tail in the future, trust me. Oh, and use code tags when posting code in the forum as it makes it easier to read. For e.g.,

    Java Code:
    class Test {
       public static void main(String[] args) {
          Test t1 = new Test();
          Test t2 = new Test();
          if (!t1.equals(t2)) {
             System.out.println("they're not equal");
          }
          if (t1 instanceof Object) {
             System.out.println("t1's an object");
          }
       }
    }
    Good luck and welcome to the forum!

  4. #4
    goose87 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Cheers for that.

    I thought it might be an xcode issue. I will keep looking and re-post if I get any further. It's good to know the code's ok though.

    Thanks for the tip Furarable, that's neat.

  5. #5
    goose87 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Just thought I'd update those interested, have now compiled code using different IDE (Eclipse), it works and I'm away, might move back to x-code once I know what I'm doing.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 0
    Last Post: 10-20-2010, 07:26 PM
  2. Will this code work
    By rajat16 in forum XML
    Replies: 3
    Last Post: 09-24-2010, 11:52 AM
  3. This code doesn't work. Why? thanks.
    By seanzhang in forum New To Java
    Replies: 10
    Last Post: 07-09-2010, 05:53 AM
  4. Code does not work
    By scotts in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 09:37 AM
  5. How does the '*' work in the code?
    By DrMath in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 09:26 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
  •