Java入门及faq__1(2)

4 StringBuffer s1 = new StringBuffer("a");
StringBuffer s2 = new StringBuffer("a");
s1.equals(s2)//为什么是false
String s1 = new String("a");
String s2 = new String("a");
s1.equals(s2)//为什么是true

其实很简单,我来总结一下。
一、String的equals方法是覆盖Object的,所以String a=new String("ss");String b=new String("ss"); 生成了两个对象,但是a.equals(b)=true,比较的是值。
二、StringBuffer的equals方法没有覆盖Object的方法,所以StringBuffer a=new StringBuffer("ss");StringBuffer b=new StringBuffer("ss");,生成了两个对象,
a.equals(b)=false,比较的是地址。
三、等号比较的是地址,String a = "a";String b=a;只生成了一个对象a==b true。
StringBuffer a=new StringBuffer("a");StringBuffer b = a; 也是生成一个对象a==b true。
四、String a="ss";String b="ss"; a.equals(b)=true a==b true因为也是一个对象。这是因为编译时只产生了一个对象"ss"。String a=new String("ss");String b=new String("ss");a.equals(b)=true,没有疑问,a==b false。因为a,b是在运行时才产生的对象,产生了2个对象。
要点:编译对象和运行对象。


5. Java Web Start是什么?
看这里
http://java.sun.com/products/javawe...adme_zh_CN.html

6 trim()函数的用法

使用一个对象。如:
String aa;
aa=" aaaa ";
aa=aa.trim();
aa return "aaaa".
是去掉字符串的前部和后部的空格。


7 怎么提取系统时间? 

Date myDate=new Date(System.currentTimeMillis());
//This time format is your local time format.
System.out.println(myDate.toString());
//Used the simple format to get the String you want .
//The valid string ,you can reference the JDK Doc
SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy/MM/DD HH:mm:ss");
System.out.println(sDateFormat.format(myDate));

使用Date和SimpleDateFormat 可以得到任何格式的时间,另外还有一个更强的类是Calendar,下面会有讨论。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值