Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Getting error on equals method for objects

  1. #1

    Default Getting error on equals method for objects

    I'm taking an introduction to java class right now, so please don't advice me to something that I have not learned yet. My assignment is that I have to create a class that has two fields. One called length and the other width. I have to make a method that returns the tract area. Similarly, I also have to make a method that indicates whether two objects have the same fields. Here is the code that I have assembled...so far

    // create private fields to hold width and length
    private double width;
    private double length;
     
    //create a constructor that initialized the value
    public LandTract(double wid, double len)
    {
     
    width=wid;
    length=len;
    }
     
    // this method will check to see if two object have same data stored in or
    //not
    public Boolean objectEqualChecker(LandTract object)
    {
    boolean flag;
    if(length.equals(object.length) && width.equals(object.width))
    {
    flag=true;
    }
    }

    My problem is encountered when writing that equals method
    if(length.equals(object.length) && width.equals(object.width))
    , I get an error saying
    HTML Code:
    cannot invoke equals(double) on the primitive type double
    . Meanwhile, I do see, to realize that when I change my fields to capital "Double." The problem disappears; however, in my class I have never dealt with a situation where I have to use capital d in double. In fact, I don't even know what's the difference between Double and double. I do know what double is but not the other one...thanks in advance! for taking your time to read this


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Getting error on equals method for objects

    The equals() method (and all methods) only works with objects from classes. For comparing primitives use the arithmetic operators like == etc.
    See the tutorial: Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Double is a class. See the API doc for more info: http://docs.oracle.com/javase/7/docs/api/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. overloading an equals method
    By hannah87 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 15th, 2013, 02:54 PM
  2. create an equals() method that overrides the object equals() method
    By slinky29 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2013, 11:11 PM
  3. equals() method.
    By TP-Oreilly in forum Object Oriented Programming
    Replies: 2
    Last Post: February 11th, 2012, 11:59 AM
  4. Using the .equals method
    By TenaciousE in forum Java Theory & Questions
    Replies: 2
    Last Post: October 23rd, 2011, 05:24 PM
  5. Equals Method
    By Sninald in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2010, 03:06 AM