小说网站 搜小说 无限网 烟雨红尘 小说爱好者 免费小说 免费小说网站

《java入门第一季》之类面试题

面试题一:

    String,StringBuffer,StringBuilder的区别?
 * A:String是内容不可变的,而StringBuffer,StringBuilder都是内容可变的。
 * B:StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高


 面试题二:

   StringBuffer和数组的区别?而它是最常用的,功能和StringBuffer完全一样
 * 二者都可以看成是一个容器,装其他的数据。
 * 但是呢,StringBuffer的数据最终是一个字符串数据。
 * 而数组可以放置多种数据,但必须是同一种数据类型的不同的数组都有不同的数据类型。


面试题三:

String类型的数据作为形式参数传递会改变实际参数吗?

通过代码验证:

public class StringBufferDemo {
	public static void main(String[] args) {
		String s1 = "hello";
		String s2 = "world";
		System.out.println(s1 + "---" + s2);// hello---world
		change(s1, s2);
		System.out.println(s1 + "---" + s2);// hello---world

		StringBuffer sb1 = new StringBuffer("hello");
		StringBuffer sb2 = new StringBuffer("world");
		System.out.println(sb1 + "---" + sb2);// hello---world
		change(sb1, sb2);
		System.out.println(sb1 + "---" + sb2);// hello---worldworld,

	}
       //方法重载
	public static void change(StringBuffer sb1, StringBuffer sb2) {
		sb1 = sb2;//sb1=world,这里对原来的sb1不会发生改变。
		sb2.append(sb1);//append拼接方式。
	}

	public static void change(String s1, String s2) {
		s1 = s2;
		s2 = s1 + s2;
	}
}
</pre><pre code_snippet_id="1677075" snippet_file_name="blog_20160510_3_2822793" name="code" class="java">结论:
<pre name="code" class="java">/ * String作为参数传递
 *  StringBuffer作为参数传递 
 * 
 * 形式参数:
 * 		基本类型:形式参数的改变不影响实际参数
 * 		引用类型:形式参数的改变直接影响实际参数
 * 
 * 注意:
 *<span style="white-space:pre">		</span>特例:
 * 		String引用类型作为参数传递,效果和基本类型作为参数传递是一样的。即:形式参数的改变影响实际参数的改变
 */


下一篇开始进入Arrays类。

posted on 2016-05-10 08:50  王小航  阅读(134)  评论(0编辑  收藏  举报

导航