Java基础知识《Java程序员面试笔试宝典》--字符串与数组

一、字符串创建和存储机制

    String s1 = “abc”;    //在常量区存放“abc”

    String s2 =  “abc”;    //引用常量区的变量,不需要创建新的对象

    String s3 = new String("abc");    //在堆中创建新的变量,并将常量区中的“abc”引用进去。

    String s4 = new String("abc");    //又在堆中创建新的变量,并将常量区中的对象引用进去。

   

二、“==”、equals 和 hashCode()

    1、== 用来比较基本数据类型是否相等,比较引用是否指向同一对象。

    2、Object中的equals(Object),在没有被覆盖之前,内部实现就是通过“==”,因此两者没有差异。

    3、在自定义equals时,一并需要重写hashCode()函数,否则将导致该类无法和基于散列值的集合类一起运行。

    4、满足equals返回true,hashCode()返回值一定相等,hashCode返回值不相等,equals一定false;

    5、hashCode()的重写方式,参看    点击打开链接


三、String、StringBuilder、StringBuffer

    1、在一个字符串需要经常被修改的情况下,最好使用StringBuffer来实现

String s = "hello";
s += "world";
//以上代码等价于
StringBuffer sb = new StringBuffer(s);
s.append("world");
s = sb.toString();

补充测试:

public static void testString(){
		String s = "Hello";
		String s1= "World";
		long start = System.currentTimeMillis();
		for(int i=0;i<10000;i++){
			s+=s1;
		}
		long end = System.currentTimeMillis();
		long runTime = (end - start);
		System.out.println("testString:"+runTime);
	}
	
	public static void testStringBuffer(){
		StringBuffer sb = new StringBuffer("Hello");
		String s1= "World";
		long start = System.currentTimeMillis();
		for(int i=0;i<10000;i++){
			sb.append(s1);
		}
		long end = System.currentTimeMillis();
		long runTime = (end - start);
		System.out.println("testStringBuffer:"+runTime);
	}
	
	public static void main(String[] args)
	{
		testString();
		testStringBuffer();
	}

结果

testString:841

testStringBuffer:3

    2、StringBuilder不是线程安全的,在单线程中使用字符串缓冲区,则StringBuilder效率更高;

    3、选择标准:在操作量比较小的情况下,使用String,在单线程中操作量大的情况下使用StringBuilder,在多线程中操作量比较大的情况下使用StringBuffer。


四、数组是不是对象?是,数组有自己的方法调用,有自己的属性(length),可以用instanceof判断类型


五、数组的声明和初始化

    1、1维数组:

        1) int[] a = new int[5];

        2) int a[] = {1,2,3,4,5};

    2、二维数组

        1) int[][] arr、int arr[][]、int[] arr[];

        2) 二维数组的第二维长度可以不同

            int[][] arr = {{1,2},{2,1,3}};

            int[][] a = new int[2][];

            a[0] = new int[]{1,2};

            a[1] = {1,2,3};


六、Java中的几种长度

    数组 int[] a  = {1,2,3}; a.length;//数组的属性

    字符串 String s = "123"; s.length();

    对于集合 size();//查看有多少个元素





猜你喜欢

转载自blog.csdn.net/qq_24888697/article/details/80784539