请教一下关于String类的赋值问题

kof02guy 2007-04-11 11:30:10
import java.io.*;
class test
{
public static void main(String[] args)
{
String[] s=new String[5];
System.out.println(s[0]);
InputStreamReader ir;
BufferedReader in;
String a=new String();
try
{
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
a=in.readLine();
}
catch(IOException e)
{
System.out.println(e);
}

if(a=="0")
System.out.println("为0");
else
System.out.println("不为0");

}
}

代码如上
结果我运行的时候,输入了0,结果他居然显示出“不为0”,请问这是为什么啊?
...全文
523 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
rhbyear 2007-04-15
  • 打赏
  • 举报
回复
非常专业的解释 我顶顶顶!!!!
shuhang1106 2007-04-12
  • 打赏
  • 举报
回复
String类型在java中是作为一个类存在的,在java中比较两个对象是否相等可以使用"=="和String 类的equals(String str)方法,

"=="的使用:
   对于基本数据类型,比较的是数据的值,例如 int a=11;int b=11;c=12;
boolean istrueAB=(a==b);
boolean istrueAC=(a==c);
结果: istrueAB=true;istrueAC=flase;
   对于引用数据类型,比较的是对象的引用值(既对象在内存中的地址),例如
                        String a=new String("aaa");
String b=a;
String c=new String("aaa");
boolean istrueAB=(a==b);
boolean istrueAC=(a==c);
结果:istrueAB=true;istrueAC=flase;
因为a和b指向同一块内存,所以引用本身的值是                     相等(即第一个new String("aaa")的内存地                      址)而第二个new String("aaa")是另一个内存                     地址.
"equals()"的使用:该方法用来比较两个引用变量所引用的对象的内容是否相等
       例如: String a=new String("aaa");
String b=a;
 String c=new String("aaa");
boolean istrueAB=(a.equals(b));
boolean istrueAC=(a.equals(c));
结果:istrueAB=true;istrueAC=true;
有些专业语言说的不够准确,见谅,希望对你有帮助
godfather521 2007-04-12
  • 打赏
  • 举报
回复

两个字符串 之间比较 如果是 == 号 那么比较的是 内存中的地址
你要想比较内容 应该 用 equals() 方法 equals 比较的是内容

你可以 将 a=="0" 改写为 a.equals("0")
richboylf 2007-04-12
  • 打赏
  • 举报
回复
同意ls说的,equals方法是检测两个字符串是否相等,判断两个字符串相等应该用xx.equals("xxx");如果相同,返回true,反之返回false,如果想检测的两个字符串是否相等,并且不区分大小写的话,可以使用equalsLgnoreCase方法
hyylcz3 2007-04-12
  • 打赏
  • 举报
回复
楼上说的是
flushtime 2007-04-12
  • 打赏
  • 举报
回复
要用a.equals("0")
约翰羊 2007-04-12
  • 打赏
  • 举报
回复
import java.io.*;
class test{
public static void main(String[] args){
String[] s=new String[5];
System.out.println(s[0]);
InputStreamReader ir;
BufferedReader in;
String a=new String();
try{
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
a=in.readLine();
}
catch(IOException e){
System.out.println(e);
}
if(a.equals("0")) //改为equals
System.out.println("为0");
else
System.out.println("不为0");

}
}

凑个热闹
xiaxinhuo 2007-04-12
  • 打赏
  • 举报
回复
如果你想用0去比较的话也可以的
前面String a=new String(); 改为 int a=0;
再把a=in.readLine();  改为a=Integer.parseInt(in.readLine());
最后再把if(a=="0"): 改成if(a==0)就可以了
如果用string的话就应该用equals去判断了.
yiyi2007 2007-04-12
  • 打赏
  • 举报
回复
up
lionest 2007-04-12
  • 打赏
  • 举报
回复
应该用equals 来比较字符串的内容是否相同
== 是比较两个对象的内存地址的
在此程序中 显然 a=="0" 永远不能返回true
网络咖啡 2007-04-12
  • 打赏
  • 举报
回复
应该修改为
a.equals("0");
判别对象的内容相等一般用equals方法,基本数据类型才可以使用==
momo8303 2007-04-12
  • 打赏
  • 举报
回复
解释的好,楼上的,顶你
家有萌宝V 2007-04-12
  • 打赏
  • 举报
回复
该说的都被你们说了,凶残!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧