java Java不是说两个Strings相等的时候吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7410635/
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 not saying that two Strings equal when they do?
提问by bzupnick
Possible Duplicate:
String comparison in Java
可能的重复:
Java 中的字符串比较
heres my script:
继承人我的脚本:
public class euler4 {
/**
* a palindromatic number reads the same both ways. the largest palindrome
* made from the product of 2-digit numbers is 9009 = 91 * 99.
*
* find the largest palindrome made from the product of two 3-digit numbers.
*/
public static void main(String[] args) {
/*for(int t = 100; t < 200; t++) {
for(int l = 100; l < 100; l++) {
String milah = "" + t * l;
String result = palin(milah);
if(result != "no_pali") {
System.out.println(milah);
}
}
} */
String result = palin("abba");
System.out.println(result);
}
public static String palin(String the_num) {
int orech = the_num.length();
String back_word = "";
/**
* the for loop has the counter starting at the orech of the number, then
* decrementing till 0 (or the 0'th index)
*/
for(int i = orech - 1; i >= 0; i--) {
back_word = back_word + the_num.charAt(i);
}
System.out.println(the_num);
System.out.println(back_word);
System.out.println(back_word == "abba");
if(back_word == the_num) {
return back_word;
} else {
return "no_pali";
}
}
}
at line 34 it prints "abba" like it should
在第 34 行,它像它应该的那样打印“abba”
at line 35 it prints an exact copy of "abba" like it should
在第 35 行,它会像它应该的那样打印“abba”的精确副本
it gets weird at line 36 which is:
它在第 36 行变得很奇怪,即:
System.out.println(back_word == "abba");
and that prints false.....??
并且打印错误..... ??
it printed exact copy of the words, but when i compare them, it gives false!?
它打印了单词的精确副本,但是当我比较它们时,它给出了错误!?
and since they dont equal (when they really do) its returning the wrong thing
并且由于它们不相等(当它们真的相等时)它返回了错误的东西
回答by Drahakar
In java the == operator compares the address of the object.
在 java 中 == 运算符比较对象的地址。
To compare if two strings are equals you need to use the .equals()
function.
要比较两个字符串是否相等,您需要使用该.equals()
函数。
back_word.equals("abba");
回答by soldier.moth
Use back_word.equals("abba");
instead.
使用back_word.equals("abba");
来代替。
==
in Java tests whethere they are the same object so back_word == back_word
would return true
.
==
在 Java 中测试它们是否是同一个对象,所以back_word == back_word
会返回true
.
The String Class's .equals
method however checks
然而,字符串类的.equals
方法检查
if the given object represents a String equivalent to this string, false otherwise
如果给定的对象表示与此字符串等效的字符串,则为 false 否则
Taken from: String's Javadoc
回答by corsiKa
Because ==
compares that they're the same object in memory, which they aren't. They represent the same data, but they aren't the same object.
因为==
比较它们是内存中的同一个对象,而它们不是。它们代表相同的数据,但它们不是同一个对象。
You need back_word.equals("abba")
instead.
你需要back_word.equals("abba")
代替。
回答by Jon Lin
You need to use String.equals() instead of comparing pointers.
您需要使用 String.equals() 而不是比较指针。
back_word.equals("abba");
(See also String.equalsIgnoreCase()
if that's applicable)
(另请参阅String.equalsIgnoreCase()
是否适用)
回答by Chris Dail
The == operator in Java performs object equality not value equality. back_word and "abba" are two different objects but they may have the same value.
Java 中的 == 运算符执行对象相等而不是值相等。back_word 和 "abba" 是两个不同的对象,但它们可能具有相同的值。
What you are looking for is the equals method:
您正在寻找的是 equals 方法:
System.out.println(back_word.equals("abba"));
回答by jdawg
In the context of the String type, using == compares by reference (memory location).
在 String 类型的上下文中,使用 == 按引用(内存位置)进行比较。
Unless you compare a string against its exact self, it will return false.
除非您将字符串与其确切的自身进行比较,否则它将返回 false。
Try using the method equals(), as it compares by value, and you should get what you need.
尝试使用方法 equals(),因为它按值进行比较,您应该得到您需要的。