Java用等于或==比较2个整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28953805/
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
Java Compare 2 integers with equals or ==?
提问by Konstantinos
i am very very new to Java and i would like to know how can i compare 2 integers? I know == gets the job done.. but what about equals? Can this compare 2 integers? (when i say integers i mean "int" not "Integer"). My code is:
我对 Java 非常陌生,我想知道如何比较 2 个整数?我知道 == 完成了工作.. 但是 equals 呢?这可以比较2个整数吗?(当我说整数时,我的意思是“int”而不是“Integer”)。我的代码是:
import java.lang.*;
import java.util.Scanner;
//i read 2 integers the first_int and second_int
//Code above
if(first_int.equals(second_int)){
//do smth
}
//Other Code
but for some reason this does not work.. i mean the Netbeans gives me an error: "int cannot be dereferenced" Why?
但由于某种原因,这不起作用..我的意思是 Netbeans 给了我一个错误:“int 不能被取消引用”为什么?
采纳答案by Elliott Frisch
int
is a primitive. You can use the wrapper Integer
like
int
是一个原语。您可以使用包装器,Integer
例如
Integer first_int = 1;
Integer second_int = 1;
if(first_int.equals(second_int)){ // <-- Integer is a wrapper.
or you can compare by value (since it is a primitive type) like
或者您可以按值进行比较(因为它是原始类型),例如
int first_int = 1;
int second_int = 1;
if(first_int == second_int){ // <-- int is a primitive.
JLS-4.1. The Kinds of Types and Valuessays (in part)
JLS-4.1。类型和值的种类说(部分)
There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).
有两种类型的Java编程语言:基本类型(4.2节)和引用类型(第4.3节)。相应地,有两种数据值可以存储在变量中、作为参数传递、方法返回和操作:原始值(§4.2)和引用值(§4.3)。
回答by Modhy
If you want to compare between
如果你想比较
1-two integer
If(5==5)
2- char
If('m'=='M')
3 string
String word="word"
word.equals("word")
回答by Md Asiful Haque
As int is primitive you can not use equals. What you can do Use Interger as wrapper
由于 int 是原始的,您不能使用 equals。您可以做什么 使用 Interger 作为包装器
void IntEquals(Integer original, Integer reverse) {
Integer origianlNumber = original;
Integer reverseNumber = reverse;
if (origianlNumber.equals(reverse)) {
System.out.println("Equals ");
} else {
System.out.println("Not Equal");
}
回答by Fazil
int is primitive type.This itself having value but Integer is object and it is having primitive int type inside to hold the value. You can do more operations like compare,longValue,..more by Using wrapper Integer.
int 是原始类型。这本身具有值,但 Integer 是对象,它内部具有原始 int 类型来保存值。您可以通过使用包装器整数来执行更多操作,例如 compare、longValue、..more。
== for Integer will not work the rang above -128 and 127
. Integer hold cache value upto this range only in memory. More than this range you have to equals() method only to check Integer wrapper class.
== 代表整数will not work the rang above -128 and 127
。整数保持缓存值仅在内存中达到此范围。超过这个范围,您必须使用 equals() 方法来检查 Integer 包装器类。
equals()method will check the value stored in the reference location.
equals()方法将检查存储在引用位置的值。