How to Compare a long value is equal to Long value

long a = 1111;
Long b = 1113;

if(a == b)
{
    System.out.println("Equals");
}else{
    System.out.println("not equals");
}

the above code prints a "equals" in console, which is wrong answer. my qestions is how to compare a long variable value is equals to Long variable value. please replay me as soon as possible.

Thankh you


First your code is not compiled. Line Long b = 1113;

is wrong. You have to say

Long b = 1113L;

Second when I fixed this compilation problem the code printed "not equals".


It works as expected,

Try checking IdeOneDemo

 public static void main(String[] args) {
        long a = 1111;
        Long b = 1113l;

        if (a == b) {
            System.out.println("Equals");
        } else {
            System.out.println("not equals");
        }
    }

prints

not equals for me

Use compareTo() to compare Long, == wil not work in all case as far as the value is cached


long a = 1111;
Long b = new Long(1113);

System.out.println(b.equals(a) ? "equal" : "different");
System.out.println((long) b == a ? "equal" : "different");
链接地址: http://www.djcxy.com/p/58636.html

上一篇: ==运算符如果比较具有相同值的Double类型,则返回false

下一篇: 如何比较long值等于Long值