Addman
Addman

Reputation: 341

Java - comparison of long values through templates

Can someone explain me this result?

Well this is my code:

public class foo <T> {

    public static void main(String[] args) {

        foo Foo = new foo();
        System.out.println(Foo.compare(100L, 100));
        System.out.println(Foo.compare(100L, 100L));
        System.out.println(Foo.compare(127L, 127L));
        System.out.println(Foo.compare(128L, 128L));

        System.out.println();
        System.out.println(System.getProperty("java.vendor"));
        System.out.println(System.getProperty("java.version"));

    }

    public boolean compare( T val1, T val2) {
        return ( val1 == val2 ) ? true : false;
    }

}

and the result was:

false
true
true
false


Sun Microsystems Inc.
1.6.0_26

First of all:

It is strange because i have declared only one template class "T". The first comparison end up false because val1 is long and val2 is int (i had checked it through debuger). But it should not be, cos i have declared only one template class and same for both.

and

Second of all:

why every "long" larger numbers greater then 127 are not equal ??

Thanks!

Upvotes: 3

Views: 225

Answers (3)

Vishal K
Vishal K

Reputation: 13066

The first comparition end up false because val1 is long and val2 is int (i had checked it through debuger). But it should not be, cos i have declared only one templete class and same for both.

Because, You are using foo Foo = new foo(); instead of foo<Long> Foo = new foo<Long>(); so, foo is considering any object as parameter ( because after compilation, the compiler erases all type parameters and replaces each with its first bound if the type parameter is bounded, or Object if the type parameter is unbounded.)

why every "long" larger numbers greater then 127 are not equal ??

Because, you are comparing the reference of variables in line val1 == val2 , not the values contained in the object pointed by those variables.

Now, A question that I want to Incorporate:

Why every long smaller than number 127 and greater than -128 are equal?

Because of JLS5.1.7

If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

Upvotes: 3

sasha.sochka
sasha.sochka

Reputation: 14715

In your compare function you are comparing references instead of values (== operator always compares references, unless it's a built-in type. see this post What is the difference between == vs equals() in Java?).

As you are using generics, your built-in types was packed to java classes (e. g. Integer).

So in general this code would give you all false's but in this case you had true in 2nd and 3rd example because java compiler caches numbers in range [-128, 127], because they are often used, so they have the same address in memory.

Upvotes: 3

keelar
keelar

Reputation: 6026

You need to use equals instead of == to perform the equality check since T is an object instead of a primitive:

public static void main(String[] args) {

    foo Foo = new foo();
    System.out.println(Foo.compare(100L, 100));
    System.out.println(Foo.compare(100L, 100L));
    System.out.println(Foo.compare(127L, 127L));
    System.out.println(Foo.compare(128L, 128L));

    System.out.println();
    System.out.println(System.getProperty("java.vendor"));
    System.out.println(System.getProperty("java.version"));

}

public boolean compare( T val1, T val2) {
    return ( val1.equals(val2) ) ? true : false; // use equal instead
}

This gives you:

false
true
true
true

The first false is caused by the fact that the types of its two inputs are not the same, therefore there is a underlying casting which causes their difference.

Upvotes: 2

Related Questions