java字符串面试题

1.

package com.gavin.convert;

public class Test3 {

	public static void main(String[] args) {
		String str1 = new String("abc");
		StringBuffer buffer = new StringBuffer("abc");
		joinChar(str1);
		appendChar(buffer);
		
		System.out.println(str1);
		System.out.println(buffer);
		
	}
	
	public static void joinChar(String string){
		string = string +'d';
	}
	
	public static void appendChar(StringBuffer buffer){
		buffer = buffer.append('d');
	}

}


结果:

abc
abcd

2.

参考:http://www.360doc.com/content/11/0310/12/3446769_99823666.shtml 

package com.gavin.convert;

public class Test4 {

	public static void main(String[] args) {
		String aString = "a1";
		String bString = "a"+1;
		System.out.println(aString == bString);
	}

}

结果:
true

3.

package com.gavin.convert;

public class Test5 {
	public static void main(String[] args) {
		String aString2 = "b1";
		String cString2 = "1";
		String bString2 = "b"+cString2;
		System.out.println(aString2 == bString2);
	}
}

结果:
false

4.

package com.gavin.convert;

public class Test6 {

	public static void main(String[] args) {
		String aString2 = "b1";
		final String cString2 = "1";
		String bString2 = "b"+cString2;
		System.out.println(aString2 == bString2);
		
	}
}

 

结果:
true

5.

package com.gavin.convert;

public class Test7 {

	public static void main(String[] args) {
		String aString2 = "ab";
		final String cString2 = getB();
		String bString2 = "a"+cString2;
		System.out.println(aString2 == bString2);
		
	}
	
	private static String getB() {
		return "b";
	}

}


 

结果:
false

6.

 

package com.gavin.convert;

public class Test8 {
	private static String aString = "ab";
	public static void main(String[] args) {
		String aString2 = "a";
		String bString2 = "b";
		String string = aString2 + bString2;
		System.out.println(aString == string);
		System.out.println(aString == string.intern());
	}
}


 

结果:
false
true

7.

package com.gavin.convert;

public class Test9 {

	private static String aString = new String("ab");
	public static void main(String[] args) {
		String aString2 = "a";
		String bString2 = "b";
		String string = aString2 + bString2;
		System.out.println(aString == string);
		System.out.println(aString == string.intern());
		System.out.println(aString.intern() == string.intern());
	}

}


结果:

false
false
true

 

8.String aStr = new String("abc");创建了几个对象?

答案:2个;

9.String aStr = “abc”;创建了几个对象?

答案:1个;

10.String aStr = "abc"; String bStr = "abc";创建了几个对象?

答案:1个;

11.String a = “ab” + "cd";创建了几个对象?

答案:3个;

12.String aString = new String("ab" + "cd");创建了几个对象?

答案:2个;

package com.gavin.convert;

public class Test10 {
	public static void main(String[] args) {
		System.out.println("ab" == ("a" + "b"));
	}
}



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值