Does the comparison operator also compares data types...
the following program gives the output Equals..Please advise...

class Check{
     public static void main(String[] args){
         int numA=100;
         double numA=100;
         if(numA==numB)  System.out.println("Equals");
         else System.out.println("Not Equal");
     }
}

Dani AI

Generated

The snippet in ’s first post has a real compile-time problem (two local declarations with the same name) — Java won’t compile that. Rename one variable and the intended comparison is between values, not “types” themselves. Example (fixed names):

class CompareDemo {
    public static void main(String[] args) {
        int i = 100;
        double d = 100.0;
        if (i == d) System.out.println("Equals");
        else System.out.println("Not Equal");
    }
}

Redeclaring a local name in the same scope is a compile-time error. (docs.oracle.com)

Behavior of == depends on the operand kinds. If both operands are numeric (or one can be converted to numeric), Java performs binary numeric promotion and then compares the numeric values; if both operands are reference types, == tests whether they refer to the same object (or are both null). In other words, for primitives == is value comparison (after promotion); for references == is identity. ’s comment describes the reference case, but it doesn’t apply to primitives. (docs.oracle.com)

Floating-point gotchas: IEEE rules mean NaN is not equal to anything (Double.NaN == Double.NaN is false) and +0.0 equals -0.0 for primitive comparison. The wrapper class behavior (Double.equals) is defined differently for hashing and can treat NaNs as equal and distinguish +0.0/-0.0; consult the Double javadoc for details. When comparing computed doubles use a tolerance (epsilon) or BigDecimal for exact decimals. Example epsilon check:

double a = 0.1 + 0.2;
double b = 0.3;
if (Math.abs(a - b) < 1e-9) System.out.println("Close enough");

See the language spec for the IEEE rules and the Double API for wrapper/equals details; use an epsilon or BigDecimal for numeric tolerance. (docs.oracle.com)

Practical checklist: fix the duplicate-name compile error first; use == for primitive value checks (aware of promotions and NaN), use equals() for object/content equality (e.g., String), and avoid direct == on floating results unless you accept IEEE edge cases. (docs.oracle.com)

Recommended Answers

All 2 Replies

no, it only compares memory locations.
So if 2 references point to the same object in memory it will return true (in which case they obviously point to the same type object as well), if they point to 2 different objects it will return false even if equals returns true for those 2 objects (and remember that equals may return true if objects are of different types depending on how it's implemented).

thanx 4 da explaination....i got the idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.