JAVA基础整理(三)

1.包装类与自动拆箱装箱

int a1= 1000;int a2=1000;if(a1==a2){System.out.println("yes");}
java

  ==的比较,基本数据类型看的是值是否相等,引用数据类型是看是不是一个对象(地址是否相等)程序员

     Integer a=1377;面试

Integer b=1377;
    if(a==b){
        System.out.println("yes");
    }


在装箱的时候自动调用的是Integer的valueOf(int)方法。而在拆箱的时候自动调用的是Integer的intValue方法。数组

public static Integer valueOf(int i) {缓存

if (i >= IntegerCache.low && i <= IntegerCache.high)
    return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);

}
安全

由装箱函数valueof可知,-128~127之间的整数有一个 IntegerCache缓存,不会建立新的对象。多线程

面试题app

public class Main {函数

public static void main(String[] args) {

    Double i1 = 100.0;
    Double i2 = 100.0;
    Double i3 = 200.0;
    Double i4 = 200.0;

    System.out.println(i1==i2);
    System.out.println(i3==i4);
}

}
优化

若是是int型的,100不会建立新对象,200会,可是浮点型的就不同了,看一下double的valueof源码:

public static Double valueOf(String s) throws NumberFormatException {

return new Double(parseDouble(s));
}

直接new了一个新对象了。(由于double的数表示范围太大了,-128~127之间几乎有无数的数字)

总结

     Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是相似的。

     Double、Float的valueOf方法的实现是相似的。

    当 "=="运算符的两个操做数都是 包装器类型的引用,则是比较指向的是不是同一个对象,而若是其中有一个操做数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)。

public class Main {

public static void main(String[] args) {

    Integer a = 1;
    Integer b = 2;
    Integer c = 3;
    Integer d = 3;
    Integer e = 321;
    Integer f = 321;
    Long g = 3L;
    Long h = 2L;

    System.out.println(c==d); //在缓存的范围内,true
    System.out.println(e==f);//不在缓存的范围内,false
    System.out.println(c==(a+b));//a+b拆箱了,比较的是值,true
    System.out.println(c.equals(a+b));equals比较的是对象,因此拆箱后又装箱了,可是在缓存以内,仍是同一个对象 true
    System.out.println(g==(a+b));true 比较的是值
    System.out.println(g.equals(a+b));比较的是对象,两者都不是同一个数据类型的天然new出来不是一个对象,false
    System.out.println(g.equals(a+h));两者都是long型的,也就是longvalueof方法,也是有-128~127的缓存机制的,true
}

}

2.string,stringbuilder,stringbuffer

2.1String类:(不可变序列)

(1)final char value[]; 由一个字符串数组构成,final修饰,因此不可变。

(2)里面的一些字符串操做方法其实都是返回了一个新串:看一个源码函数substring

public String substring(int beginIndex, int endIndex) {

if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > value.length) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    int subLen = endIndex - beginIndex;
    if (subLen < 0) {
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return ((beginIndex == 0) && (endIndex == value.length)) ? this
            : new String(value, beginIndex, subLen);//new了一个新的string返回了
}


在这里要永远记住一点:

“对String对象的任何改变都不影响到原对象,相关的任何change操做都会生成新的对象”。

     String s1="hello";

String s2="hello";
    System.out.println(s1==s2);// true,hello是字符串常量,放在常量池里
    String s3=new String("hello");
    System.out.println(s1==s3);//false//new后是一个新的对象。

(2)stringbuilder类

public StringBuilder append(String str) {

super.append(str);
    return this;//在原有基础上作变化的。
}

  StringBuilder和StringBuffer类拥有的成员属性以及成员方法基本相同,区别是StringBuffer类的成员方法前面多了一个关键字:synchronized,不用多说,这个关键字是在多线程访问时起到安全保护做用的,也就是说StringBuffer是线程安全的。因此通常stringbuilder用的居多。

  对于直接相加字符串,效率很高,由于在编译器便肯定了它的值,也就是说形如”I”+”love”+”java”; 的字符串相加,在编译期间便被优化成了”Ilovejava”。

  对于间接相加(即包含字符串引用),形如s1+s2+s3; 效率要比直接相加低,由于在编译器不会对引用变量进行优化。

  String、StringBuilder、StringBuffer三者的执行效率:

StringBuilder > StringBuffer > String

固然这个是相对的,不必定在全部状况下都是这样。

好比String str = “hello”+ “world”的效率就比 StringBuilder st = new StringBuilder().append(“hello”).append(“world”)要高。

所以,这三个类是各有利弊,应当根据不一样的状况来进行选择使用:

当字符串相加操做或者改动较少的状况下,建议使用 String str=”hello”这种形式;

当字符串相加操做较多的状况下,建议使用StringBuilder,若是采用了多线程,则使用StringBuffer。

几个面试题:

1. 下面这段代码的输出结果是什么?

String a = “hello2″;   String b = “hello” + 2;   System.out.println((a == b));

输出结果为:true。缘由很简单,”hello”+2在编译期间就已经被优化成”hello2″,所以在运行期间,变量a和变量b指向的是同一个对象。

2.下面这段代码的输出结果是什么?

String a = “hello2″;   String b = “hello”; String c = b + 2; System.out.println((a == c));

输出结果为:false。因为有符号引用的存在,因此 String c = b + 2;不会在编译期间被优化,不会把b+2当作字面常量来处理的,所以这种方式生成的对象事实上是保存在堆上的。所以a和c指向的并非同一个对象。

3.下面这段代码的输出结果是什么?

String a = “hello2″;   final String b = “hello”; String c = b + 2; System.out.println((a == c));

输出结果为:true。对于被final修饰的变量,会在class文件常量池中保存一个副本,也就是说不会经过链接而进行访问,对final变量的访问在编译期间都会直接被替代为真实的值。那么String c = b + 2;在编译期间就会被优化成:String c = “hello” + 2

4.下面这段代码输出结果为:

public class Main { public static void main(String[] args) { String a = "hello2"; final String b = getHello(); String c = b + 2; System.out.println((a == c)); } public</code> <code>static</code> <code>String getHello() { return</code> <code>"hello"; }``}

输出结果为false。这里面虽然将b用final修饰了,可是因为其赋值是经过方法调用返回的,那么它的值只能在运行期间肯定,所以a和c指向的不是同一个对象。

5.下面这段代码的输出结果是什么?

public class Main { public static void main(String[] args) { String a = "hello"; String b = new</code> <code>String("hello"); String c = new String("hello"); String d = b.intern(); System.out.println(a==b); System.out.println(b==c); System.out.println(b==d); System.out.println(a==d); }}

这里面涉及到的是String.intern方法的使用。在String类中,intern方法是一个本地方法,在JAVA SE6以前,intern方法会在运行时常量池中查找是否存在内容相同的字符串,若是存在则返回指向该字符串的引用,若是不存在,则会将该字符串入池,并返回一个指向该字符串的引用。所以,a和d指向的是同一个对象,其他都是false

6.String str = new String(“abc”)建立了多少个对象?

这个问题在不少书籍上都有说到好比《Java程序员面试宝典》,包括不少国内大公司笔试面试题都会遇到,大部分网上流传的以及一些面试书籍上都说是2个对象,这种说法是片面的。

若是有不懂得地方能够参考这篇帖子:

http://rednaxelafx.iteye.com/...

首先必须弄清楚建立对象的含义,建立是何时建立的?这段代码在运行期间会建立2个对象么?毫无疑问不可能。

很显然,new只调用了一次,也就是说只建立了一个对象。

而这道题目让人混淆的地方就是这里,这段代码在运行期间确实只建立了一个对象,即在堆上建立了”abc”对象。而为何你们都在说是2个对象呢,这里面要澄清一个概念 该段代码执行过程和类的加载过程是有区别的。在类加载的过程当中,确实在运行时常量池中建立了一个”abc”对象,而在代码执行过程当中确实只建立了一个String对象。

所以,这个问题若是换成 String str = new String(“abc”)涉及到几个String对象?合理的解释是2个。

我的以为在面试的时候若是遇到这个问题,能够向面试官询问清楚”是这段代码执行过程当中建立了多少个对象仍是涉及到多少个对象“再根据具体的来进行回答。

7.下面这段代码1)和2)的区别是什么?

public class Main { public static void main(String[] args) { String str1 = "I"; //str1 += "love"+"java"; 1) str1 = str1+"love"+"java"; //2)</code> <code>}``}

1)的效率比2)的效率要高,1)中的”love”+”java”在编译期间会被优化成”lovejava”,而2)中的不会被优化。

3.Date类

4.Calendar类

5.Dateformat类

6.simpledateformat类

7.exception整理

相关文章
相关标签/搜索