Java 如何比较长值等于长值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4483734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:56:55  来源:igfitidea点击:

How to Compare a long value is equal to Long value

java

提问by Chitresh

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.

上面的代码在控制台中打印了一个“equals”,这是错误的答案。我的问题是如何比较 long 变量值是否等于 Long 变量值。请尽快重播我。

Thankh you

谢谢你

采纳答案by AlexR

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

首先你的代码没有被编译。线长 b = 1113;

is wrong. You have to say

是错的。你不得不说

Long b = 1113L;

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

其次,当我解决这个编译问题时,代码打印出“不等于”。

回答by Jigar Joshi

It works as expected,

它按预期工作,

Try checking IdeOneDemo

尝试检查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 equalsfor me

not equals为了我

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

使用compareTo()比较长,==西港岛线在所有情况下,没有工作就值缓存

回答by barti_ddu

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

System.out.println(b.equals(a) ? "equal" : "different");
System.out.println((long) b == a ? "equal" : "different");

回答by Mr_CRivera

On the one hand Long is an object, while on the other hand long is a primitive type. In order to compare them you could get the primitive type out of the Long type:

一方面 Long 是一个对象,另一方面 long 是一个原始类型。为了比较它们,您可以从 Long 类型中获取原始类型:

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

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

回答by Justas

Since Java 7 you can use java.util.Objects.equals(Object a, Object b):

从 Java 7 开始,您可以使用java.util.Objects.equals(Object a, Object b)

These utilities include null-safe or null-tolerant methods

这些实用程序包括空安全或空值容忍方法

Long id1 = null;
Long id2 = 0l;
Objects.equals(id1, id2));

回答by ArifMustafa

I will share that How do I do it since Java 7 -

我将分享自 Java 7 以来我该怎么做 -

Long first = 12345L, second = 123L;
System.out.println(first.equals(second));

output returned : false

输出返回:false

and second example of match is -

匹配的第二个例子是 -

Long first = 12345L, second = 12345L;
System.out.println(first.equals(second));

output returned : true

输出返回:true

So, I believe in equals method for comparing Object's value, Hope it helps you, thanks.

所以,我相信比较 Object 值的 equals 方法,希望对你有帮助,谢谢。

回答by Nabin Kumar Khatiwada

    public static void main(String[] args) {
        long a = 1111;
        Long b = 1113L;
        if(a == b.longValue())
    {
        System.out.println("Equals");
    }else{
        System.out.println("not equals");
    }
  }

or:

    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");
    }
  }