java程序员面试笔试宝典4.5 字符串与数组

1.字符串创建与存储的机制是什么?

字符串的声明主要有以下两种情况:
1)String s1 = new String("abc");
String s2 = new String("abc");
s1和s2内容相同,但是在内存中的地址是不同的,只要用到new总会生成新的对象。
2)String s1 = "abc";
String s2 = "abc";
在JVM中存在一个字符串池,保存很多String对象,并且可以共享使用。
s1和s2引用的是同一个常量池中的对象。
String s = "abc";       //把abc放到常量池中,在编译时产生 
String s1 = "ab"+"c";   //把"ab"+"c"转换位字符串常量"abc"放到常量区中
String s2 = new String("abc");//在运行时把"abc"放到堆里面。

String s3 = "abc";      //在常量区里面存放了一个"abc"字符串对象
String s4 = "abc";      //s2引用常量区中的对象,因此不会创建新的对象
String s5 = new String("abc");//在堆中创建新的对象
String s6 = new String("abc");//在堆中又创建一个新的对象。
引申:
1)对于String 类型的变量s,赋值语句s=null与s=""是否相同?
s=null:其中s是一个字符串类型的引用,它不指向任何一个字符串
s="":它指向另外一个字符串,这个字符串的值位“”,即空字符串
因此:两者不同

2)new String("abc")创建了几个对象?
一个或两个
如果常量池中原来有abc,那么只创建了一个对象
如果常量池中原来没有字符串“abc”,那么就会创建两个对象。

2.“==” , equals 和 hashCode有什么区别?

1)== 运算符来比较两个变量的值是否相等
基本数据类型:比较对应的值是否相等
引用类型:比较两个引用是否指向同一个对象,即这两个变量所对应内存中的数值是否相等。
若要比较两个对象的内容是否相等,用==无法实现。
2)equals()方法是Object提供的方法。Object类中的equals方法是直接使用==进行比较两个对象的,所以比较的也是引用。
但是它可以被覆盖,例如String类的equals方法就是比较两个独立对象的内容是否相同,即堆中的内容是否相同。
1public static void main(String[] args) {
        String s1 = new String("Hello");
        String s2 = new String("Hello");
        System.out.println(s1==s2);
        System.out.println(s1.equals(s2));
    }

结果:
false
true
3)hashCode方法是从Object类中继承过来的,用来鉴定两个对象是否相等。
Object类中的hashCode方法返回对象在内存地址转化成的一个int值,
所以如果没有重写hashCode方法,任何对象的hashCode方法都是不相等的。
一般在覆盖equals方法的同时也会覆盖hashCode方法,否则会违反Object-hashCode的通用约定,导致该类无法与所有基于散列值(hash)的集合类(HashMap,HashSet,HashTable)结合在一起正常使用。
hashCode与equals的关系:
如果x.equals(y)为true:那么调用 两个对象中任意一个对象的hashCode方法都必须产生同样的整数结果。
如果x.equals(y)为false:那么两个对象的hashCode方法可能相等,也可能不相等
反之:
hashCode的返回值不相等:推出equals方法的返回值不相等
hashCodde的返回值相等:equals方法的返回值可能相等,可能不相等。
1:下列选项返回false的是:
String s = "Hello"; String t = "Hello"; char[] c = ['H','e','l','l','o']

A:s.equals(t)T
B:t.equals(c)F
C:s==t;T
D:t.equals(new String("Hello"))T

说明:t和c分别为字符串类型和数组类型,因此返回值为false2String s = "abc";
String s1 = "ab"+"c";
System.out.println(s==s1); //true

说明:"ab"+"c"在编译器就被转换为"abc",存放在常量区。

例3:Set里的元素是不能重复的,使用equals方法来区分是否重复。

3.String , StringBuffer , StringBuilder和StringTokenizer有什么区别?

java语言有四个类可以堆字符或字符串进行操作,分别是Character,String,StringBuffer和StringTokenizer.
其中Character用于单个字符操作
String字符串操作,属于不可变类
StringBuffer字符串操作,可变类

1)String和StringBuffer的区别:
当实例化String时,可以通过构造函数:String s1 = new String("World");也可以通过赋值的方式String s2 = "World";
而StringBuffer只能使用构造函数(StringBuffer s = new StringBuffer("Hello"))来初始化。

String字符串修改的原理:
首先创建一个StringBuffer,调用StringBuffer的append()方法,最后调用StringBuffer的toString()方法把结果返回。
        String s = "Hello";
        s+=" World";

等价于:
        StringBuffer sb = new StringBuffer(s);
        sb.append(" World");
        s = sb.toString();

例1public class Test{

    public static void testString(){
        String s = "Hello";
        String s1 = " World";
        long start = System.currentTimeMillis();
        for (int i = 0; i < 30000; i++) {
            s += s1;
        }
        long end = System.currentTimeMillis();
        long runtime = end-start;
        System.out.println("testString:"+runtime);
    }

    public static void testStringBuffer(){
        StringBuffer s = new StringBuffer("Hello");
        String s1 = " World";
        long start = System.currentTimeMillis();
        for (int i = 0; i < 30000; i++) {
            s.append(s1);
        }
        long end = System.currentTimeMillis();
        long runtime = end-start;
        System.out.println("testStringBuffer:"+runtime);
    }

    public static void main(String[] args) {
        testString();
        testStringBuffer();
    }
}

结果:
testString:2719
testStringBuffer:15
2)StringBuilder和StringBuffer的区别:
StringBuilder不是线程安全的,只有在单线程访问时可以使用StringBuilder
执行效率方面StringBuilder>StringBuffer>String
结论:
如果要操作的数据量比较小用String
单线程下操作大量数据用StringBuilder
多线程下操作大量数据StringBuffer

3)StringTokenizer是用来分割字符串的工具类
public class Test{

    public static void main(String[] args) {
        StringTokenizer st = new StringTokenizer("Welcome to BeiJing");
        while(st.hasMoreTokens()){
            System.out.println(st.nextToken());
        }
    }
}

结果:
Welcome
to
BeiJing

4.Java中数组是不是对象?

数组是指具有相同类型的数据的集合,它们一般具有固定的长度,并且在内存中占据连续的空间。
在java中,数组不仅有自己的属性(length),也有方法(clone)。
对象的特点是封装了一些数据,同时提供了属性和方法。所以数组是对象。
可以通过instanceof来判断数据的类型。
package com.zcl.test;
public class Test{
    public static void main(String[] args) {
        int[] a = {1,2};
        int[][] b = new int[2][4];
        String[] s = {"a","b"};
        if(a instanceof int[]){
            System.out.println("the type for a is int[]");
        }
        if(b instanceof int[][]){
            System.out.println("the type for b is int[][]");
        }
        if(s instanceof String[]){
            System.out.println("the type for s is String[]");
        }
    }
}

结果:
the type for a is int[]
the type for b is int[][]
the type for s is String[]

5.数组的初始化方式有哪几种?

在java语言中,一维数组的声明方式为:type arrayName[] 或者 type[] arrayName
数组被创建后会根据数组存放的数据类型初始化成对应的初始值。
java数组在定义时,并不会给数组分配内存空间,因此[]中不需要指定数组的长度。但是在使用时必须为之分配空间。
分配方法为:arrayName = new type[arraySize]

在完成数组的声明后,需要对其进行初始化,以下两种初始化方式:
1)int[] a = new int[5];
动态创建了一个包含5个整形值的数组,默认初始化为0
2)int[] a = {1,2,3,4,5};
声明一个数组类型变量并初始化。
当然也可以分开来写:
1)int[] a;
a = new int[5];
2)int[] a;
a = new int[]{1,2,3,4,5};



二维数组的声明与初始化方式
3种声明方式
1)type arrayName[][]
2) type[][] arrayName
3) type[] arrayName[]
注意:在声明二维数组时,[]必须为空。

初始化:
type arrayName[][] = {{c11,c12,c13},{c21,c22,c23},{c31,c32,c33}};
type arrayName[][] = new type[行数][列数];
注意:在java语言中,二维数组的第二维的长度可以不同,如下第一行2列,第二行3列。
1)int[][] arr = {{1,2},{3,4,5}};
2) int[][] a = new int[2][];
a[0] = new int[]{1,2} 
a[1] = new int[]{3,4,5}
1:二维数组的遍历方法
public class Test{

    public static void main(String[] args) {
        int a[][] = new int[2][];
        a[0] = new int[]{1,2};
        a[1] = new int[]{3,4,5};
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j]+" ");
            }
        }
    }
}

结果:
1 2 3 4 5 
引申:
1)数组是一种原生类(x)
原生类指未被实例化的类,数组一般指实例化,被分配空间的内
2)数组的大小可以任意改变(x)

6.length属性和length()方法有什么区别?

在java语言中,String提供了length()方法来计算字符串的长度
public class Test{

    public static void testArray(int[] arr){
        System.out.println("数组的长度为:"+arr.length);
    }

    public static void testString(String s){
        System.out.println("字符串的长度为:"+s.length());
    }

    public static void main(String[] args) {
        int[] arr = {1,3,5,7};
        String s = "1357";
        testArray(arr);
        testString(s);
    }
}

结果:
数组的长度为:4
字符串的长度为:4

引申:除了length属性与length()方法外,Java中还有一个计算对象大小的方法size(),该方法是针对泛型集合而言的。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值