java 的一个奇怪问题(50分)

  • 主题发起人 低调一贱男
  • 开始时间

低调一贱男

Unregistered / Unconfirmed
GUEST, unregistred user!
/**
* @since J Builder 7.0 , JDK 1.31
* @author LiuKun
* @version 2.0
*/
import java.util.*;
public class helloworld {
public static void main(String[] args){
int i=1;
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2);//问题所在
System.out.println(n1 != n2);//问题所在
prt("i : "+i);
prt("++i : "+ ++i);
//先运算,再指派其值
prt("i++ : "+ i++);
//先指派其值,然后运算
prt("i : "+i);
prt("--i : "+ --i);
prt("i-- : "+ i--);
prt("i : "+i);
prt("Hello world");
try{
System.in.read();
}catch(Exception e){}
}
static void prt(String s){
/**
* @return void
* @param String s 需要打印的字符串
*/
System.out.println(s);
}
}
看看结果吧
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
false //问题所在 n1 == n2
true //问题所在 n1 != n2
i : 1
++i : 2
i++ : 2
i : 3
--i : 2
i-- : 2
i : 1
Hello world
 
这是类型和运算符的问题,换成下面的代码试试:
System.out.println(n1.intValue() == n2.intValue());//问题所在
System.out.println(n1.intValue() != n2.intValue());//问题所在
 
正确,但有别的解决方法,已经解决
 

Similar threads

I
回复
0
查看
608
import
I
I
回复
0
查看
593
import
I
I
回复
0
查看
699
import
I
I
回复
0
查看
631
import
I
顶部