public class StringPool {
public static void main(String args[])
{
String s0="Hello";
String s1="Hello";
String s2="He"+"llo";
System.out.println(s0==s1);//true
System.out.println(s0==s2);//true
System.out.println(new String("Hello")==new String("Hello"));//false
}
}
public static void main(String args[])
{
String s1="a";
String s2=s1;
System.out.println(s1==s2);//true
s1+="b";
System.out.println(s1==s2);//false
System.out.println(s1=="ab");//false
System.out.println(s1.equals("ab"));//true
}
public boolean equals(Object obj)
{
return (this == obj);
}
//可见默认的equals方法,直接调用==,比较对象地址。
//
//不同的子类,可以重写此方法,进行两个对象的equals的判断。
//String类源码中重写的equals()方法的实现代码如下:
public boolean equals(Object anObject)
{
if(this==anObject) return true;
if(anObject instanceof String)
{
String anotherString=(String)anObject;
int n=value.length;
if(n==anotherString.value.length)
//若两个字符串长度一样,则一个个进行字符比较
{
char v1[]=value;//字符串转化成的对应数组
char v2[]=anotherString.value;
//字符串转化成的对应数组
int i=0;
while(n--!=0)
{
if(v1[i]!=v2[i]) return false;
//若比较过程中出现不等,则俩字符串不等,返回false
i++;
}
return true;
//直至比较完两个字符串长度,跳出while循环
// 此时说明俩字符串相等,返回true
}
}
return false;
//两个字符串长度不一样,俩字符串不等,
//不必一个个比较内容,直接返回false
}
String str="abc";
String result=str.trim().toUpperCase().concat("defg");
public class MyCounter {
int i;
MyCounter(int n){
i=n;
}
public MyCounter increase(int n) {
this.i=this.i+n;
return this;
}
public MyCounter decrease(int n) {
this.i=this.i-n;
return this;
}
public static void main(String[] args) {
MyCounter counter1=new MyCounter(1);
MyCounter counter2=counter1.increase(100).decrease(2).increase(3);
System.out.println("counter2.i="+counter2.i);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有