Mag*_*pie 3 java string int if-statement
可能重复:
Java String.equals与==
我认为这将是一种结构化选择器方法的简洁方法,但输出不是前两个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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是终端的结果:
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
Run Code Online (Sandbox Code Playgroud)
如果我输入2,则同样如此."response"print语句是来自main类中print语句的方法的输出.
我以前没试过这种方式,但我认为它应该有效.我真的不明白为什么不是.有人能清除这个吗?谢谢
在Java中,您需要使用equals方法比较Strings:
if ( input.equals("1") ) {
// do something...
}
Run Code Online (Sandbox Code Playgroud)
从Java 7开始,您也可以在switch语句中使用Strings:
switch ( input ) {
case "1":
// do something...
break;
case "2":
// do something...
break;
}
Run Code Online (Sandbox Code Playgroud)
编辑:补充我的答案,这里是一个类的例子,它使用带字符串的开关和类的反汇编代码(使用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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
反汇编的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
}
Run Code Online (Sandbox Code Playgroud)
一个有趣的事情是用整数创建另一个开关(标签62),它是做"实际工作"的.
1.用于在java中.equals进行比较Objects,并且String是Java中的Object.
例如:
if(input.equals("1"))
2.这是可选的,但如果您尝试使用Scanner类,它也会很好.
例如:
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
Run Code Online (Sandbox Code Playgroud)
3.它也将建议使用switch的语句来代替 if-else梯,如果开关用来使用String那么你必须有 jdk 1.7 or above,否则,如果你使用的是JDK版本低于 1.7,那么你就可以转换为字符串的字符,然后在交换机中使用它.
对于测试Object(非基本类型)的相等性,请使用Object.equals().
if(input.equals("1")) {
return 1;
}
Run Code Online (Sandbox Code Playgroud)
该==运营商检查是否引用的对象是相等的.参考相等性的测试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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
377 次 |
| 最近记录: |