-2

Here is the situation: I have two methods from two different class,

int getBoardPositionValue(int i, int j){ //from class Board
        return gameBoard[i][j];
    }

int getCoinNumber(){ //from class Coin
        return coinNumber;
    }

I am trying to compare those two values. However, I am not sure what is the correct way to compare them.

board.getBoardPositionValue(i, j)==coin.getCoinNumber() 

or

board.getBoardPositionValue(i, j).equals(coin.getCoinNumber())

or is there any other way?

Thanks!

Idos
  • 15,053
  • 14
  • 60
  • 75
Chasiwpaw
  • 23
  • 1
  • 5
  • 2
    For primitives use `==` and for references use `equals()`. For doubles and floats, check [this](http://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java) – TheLostMind Dec 19 '15 at 08:13
  • what is board..is it an object or class name?..If it is an object...of which class?...This code is incomplete...How do you expect us to understand? – Mathews Mathai Dec 19 '15 at 08:14
  • 2
    @MathewsMathai In this case it doesn't matter. The asker wants to know how to compare `int`s returned from 2 methods, regardless of how those are invoked. – user1803551 Dec 19 '15 at 08:16
  • Did you try it? Does the second one compile for you? – user1803551 Dec 19 '15 at 08:19
  • 1
    What happened when you *did* try it? Oh, you didn't? Is it really more efficient not to try it before you post? – user207421 Dec 19 '15 at 08:20
  • @user1803551...He mentions different methods.So I thought I'll clarify the origin of the objects and give him a detailed answer. – Mathews Mathai Dec 19 '15 at 08:23
  • Thanks for the fast reply guys. I am not complete with the code. I was at first using the .equals() to compare the value returned from both method, but it gave me error. That is why I wanted to know if == would be a solution to compare them. I actually thought that .equals() should be used in most cases because I had problems in compare two Strings in the past. – Chasiwpaw Dec 19 '15 at 08:25
  • @Andrew I just added an example of using equals() or == with Strings :) – Idos Dec 19 '15 at 08:27
  • Have you given some value to 'i' and 'j' before passing it to getBoardPositionValue?..If you simply pass 'i' and 'j' before intialising them as type integer then it will be considered as char and the function expects 2 integer arguments. Function `.equals()` is supposed to work fine! – Mathews Mathai Dec 19 '15 at 08:30
  • What was the error that you got? – Mathews Mathai Dec 19 '15 at 08:31
  • board.getBoardPositionValue(i, j).equals(coin.getCoinNumber()) gave me Cannot invoke equals(int) on the primitive type int – Chasiwpaw Dec 19 '15 at 08:34
  • So in what respect doesn't that answer your question? – user207421 Dec 19 '15 at 08:37
  • @EJP You mean aspect? Well I guess it is because it tells you that you cannot implement it this way, but it does not answer my "why". – Chasiwpaw Dec 19 '15 at 08:43

2 Answers2

3

If you compare references (Objects) then use .equals(), but if you compare primitive types (int, float, double etc...) you should use ==.
In your case it looks like you are comparing ints, so you can use == without a problem.
( board.getBoardPositionValue(i, j) == coin.getCoinNumber() )

Further: If you want to check if two Objects are the same then you may use ==, but if you wish to check their content you will want to use .equals(). For example, checking if two Strings are equal or not using the equality operator (==) and not with .equals() method is a mistake.
Check this out for a classic example of using .equals() or == in String:

        String a = "abc";
        String b = "abc";

        // returns true because a and b points to same string object
        if(a == b){
            System.out.println("Strings are equal by == because they are cached in the string pool");
        }

        b = new String("abc");

        // returns false as now b isn't a string literal and points to a different object
        if(a == b){
            System.out.println("String literal and String created with new() are equal using ==");
        }else{
            System.out.println("String literal and String created with new() are not equal using ==");
        }

        //both strings are equal because their content is the same
        if(a.equals(b)){
            System.out.println("Two Strings are equal in Java using the equals() method because their content is the same");
        }else{
            System.out.println("Two Strings are not equal in Java using the equals() method because their content is the same");
        }
Idos
  • 15,053
  • 14
  • 60
  • 75
1

Object A and Object B. A==B is used to check if the objects are same in terms of reference.If both the objects refer to the same address,it will return true.

A.equals(B) is used to compare the equality of the objects.If both the objects have same instance variables and the values of the instance variables are equal then,it will return true. Objects of the same class are equal if their instance variables have the same value.

Primitives: int a=1; and int b=3; a==b checks for equality.In this case,it returns false.

Mathews Mathai
  • 1,707
  • 13
  • 31