可能重复:
Java String.equals与==
我认为这将是构造picker方法的一种巧妙方法,但输出不会进入前两个if语句,只输出最后一个
public int myPickerMethod(){
System.out.println("please select from the options ");
System.out.println("please select 1 for option 1 ");
System.out.println("please select 2 please select 2 for option 2");
String input = keyboard.readLine();
System.out.println("input = " + input);
if(input=="1"){
return 1;
}
else if(input=="2"){
return 2;
}
else{
return 42;
}
}
这是我从终端得到的结果:
please select from the options
please select 1 for option 1
please select 2 please select 2 for option 2
1
input = 1
response = 42
如果我放入2也是如此。"response"print语句是主类中print语句的方法输出。
我以前从未尝试过这种方法,但我认为它应该有效。我真的不明白为什么不是。有人能把这个清理干净吗?感谢
在Java中,您需要使用equals方法比较字符串:
if ( input.equals("1") ) {
// do something...
}
从Java7开始,您也可以在switch语句中使用字符串:
switch ( input ) {
case "1":
// do something...
break;
case "2":
// do something...
break;
}
Edit:作为对我答案的补充,这里有一个类的例子,它使用带字符串的switch和该类的反汇编代码(使用javap-c),以及它的工作原理(标签8和11)。
Foo.java
public class Foo {
public static void main( String[] args ) {
String str = "foo";
switch ( str ) {
case "foo":
System.out.println( "Foo!!!" );
break;
case "bar":
System.out.println( "Bar!!!" );
break;
default:
System.out.println( "Neither Foo nor Bar :(" );
break;
}
}
}
分解的Foo.class代码:
Compiled from "Foo.java"
public class Foo {
public Foo();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String foo
2: astore_1
3: aload_1
4: astore_2
5: iconst_m1
6: istore_3
7: aload_2
8: invokevirtual #3 // Method java/lang/String.hashCode:()I << hashCode here! (I wrote this!)
11: lookupswitch { // 2
97299: 50 // 97299: hashCode of "bar" (I wrote this!)
101574: 36 // 101574: hashCode of "foo" (I wrote this!) (yep, they where swaped. the lesser hashCode first)
default: 61
}
36: aload_2
37: ldc #2 // String foo
39: invokevirtual #4 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
42: ifeq 61
45: iconst_0
46: istore_3
47: goto 61
50: aload_2
51: ldc #5 // String bar
53: invokevirtual #4 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
56: ifeq 61
59: iconst_1
60: istore_3
61: iload_3
62: lookupswitch { // 2
0: 88
1: 99
default: 110
}
88: getstatic #6 // Field java/lang/System.out:Ljava/io/PrintStream;
91: ldc #7 // String Foo!!!
93: invokevirtual #8 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
96: goto 118
99: getstatic #6 // Field java/lang/System.out:Ljava/io/PrintStream;
102: ldc #9 // String Bar!!!
104: invokevirtual #8 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
107: goto 118
110: getstatic #6 // Field java/lang/System.out:Ljava/io/PrintStream;
113: ldc #10 // String Neither Foo nor Bar :(
115: invokevirtual #8 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
118: return
}
一件有趣的事情是,另一个开关是用整数创建的(标签62),它做"真正的工作"。
对于测试对象(非基元类型)相等性,请使用Object.equals()
。
if(input.equals("1")) {
return 1;
}
==
运算符检查对对象的引用是否相等。参考相等性的测试在String.equals() method
中进行,以及其他检查。以下是String.equals()
方法的Java源代码:
public boolean equals(Object anObject) {
if (this == anObject) { // Reference equality
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) { // Are the strings the same size?
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++]) // Compare each character
return false;
}
return true;
}
}
return false;
}
1.使用.equals
比较java中的Objects
,而String
是java中的对象。
例如:if(input.equals("1"))
2.这是可选,但如果您尝试使用Scanner类,它也会很好。
例如:
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
3.还建议使用switch
语句而不是if-else
梯形图,如果开关使用String
,则您必须具有jdk 1.7 or above
,否则,如果您使用的jdk版本低于1.7
,则可以将字符串转换为字符,然后在开关中使用
不要通过==
比较字符串(或任何对象)的值。使用类似if(input.equals("1"))
的equals
方法。
==
用于检查引用是否包含相同的对象,例如
Integer i1=new Integer(1);
Integer i2=new Integer(1);
Integer i3=i1;
//checking references
System.out.println(i1==i2);//false
System.out.println(i1==i3);//true
//checking values
System.out.println(i1.equals(i2));//true
System.out.println(i1.equals(i3));//true
对象比较应该是.equals
而不是==
,并且String
是对象。
input=="1"应输入。等于("1")
EDITFollowing始终有效,即使您使用==
,因为它们是文字,java分配相同的内存位置。
String s ="Hi";
String s1= "Hi";
if(s==s1)
{
System.out.println("Yes they are same");
}
但是,当我们读取两个单独的字符串(new String())
时,除非覆盖equals
和hashcode
,否则可能不满足相等条件。阅读以上链接了解更多详细信息。
使用equals
方法。。。。
public int myPickerMethod(){
System.out.println("please select from the options ");
System.out.println("please select 1 for option 1 ");
System.out.println("please select 2 please select 2 for option 2");
String input = keyboard.readLine();
System.out.println("input = " + input);
if(input.equals("1")){
return 1;
}
else if(input.equals("2")){
return 2;
}
else{
return 42;
}
}