java 基础记录

1,Long类型,必须带后缀l或者L,最好用大写的。long类型,可以不用带后缀,但是会把int转化为long类型,所以如果超过int范围就会报错,所以最好加上后缀。比如:

System.out.println(Long.MAX_VALUE);
        long L = 9223372036854775807;//报错
        long L1 = 9223372036854775807L;//通过
Long.MAX_VALUE是9223372036854775807

2.包装类型默认值为null,比如JavaBean中,Integer类型没有值,默认为null。基本类型int,则默认为0。

3,字段串“==”和equals的比较

String a = "a";
        String b = new String("a");
        if(a == b){
            System.out.println("a ==  b" + " : equals");
        }else{
            System.out.println("a == b" +  ": not equals");
        }
        
        if(a.equals(b)){
            System.out.println("a equals b" + " : equals");
        }else{
            System.out.println("a equals b" +  ": not equals");
        }
        
        String c = "c";
        String d = "c";
        if(c == d){
            System.out.println("c == d" + " : equals");
        }else{
            System.out.println("c == d" + ": not equals");
        }

//output:

a == b: not equals
a equals b : equals
c == d : equals

字符串比较一定不能用 “==”,这个只能确定两个字符在同一个位置上。当然如果在同一个位置上,必然相等。要用equals方法。

整型“==”和equals的的比较

 Integer a = 1;
            Integer b = 1;
            if(a.equals(b)){
                System.out.println("a.equals(b)" + ":equals");
            }else {
                System.out.println("a.equals(b)" + ":not equals");
            }
            if(a == b){
                System.out.println("a == b" + ":equals");
            }else {
                System.out.println("a == b" + ":not equals");
            }
            
            Integer c = new Integer(1);
            Integer d = new Integer(1);
            if(c.equals(d)){
                System.out.println("c.equals(d)" + ":equals");
            }else {
                System.out.println("c.equals(d)" + ":not equals");
            }
            if(c == d){
                System.out.println("c == d" + ":equals");
            }else{
                System.out.println("c == d" + ":not equals");
            }

output:

a.equals(b):equals
a == b:equals
c.equals(d):equals
c == d:not equals

Integer常量赋值和new Integer对象是不一样的。

4,BigDecimal

                BigDecimal a = new BigDecimal("0.02");
        BigDecimal b = new BigDecimal(0.02);
        System.out.println(a);
        System.out.println(b);

output:

0.02
0.0200000000000000004163336342344337026588618755340576171875

要准确的,以字符串的形式。

5、String

 

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {}

String的定义,不可变的类。

字符串常量池(String pool, String intern pool, String保留池) 是Java方法区中一个特殊的存储区域, 当创建一个String对象时,假如此字符串值已经存在于常量池中,则不会创建一个新的对象,而是引用已经存在的对象。 
如下面的代码所示,将会在堆内存中只创建一个实际String对象. 

例如:

String s1 = "abcd"; 
String s2 = "abcd"; 

 参考:https://blog.csdn.net/ylyg050518/article/details/52352993

 private final char value[];//string存储在char数组中。

6,HashMap的原理

参考:http://www.importnew.com/18633.html

7,基本类型优于装箱基本类型

    先看两个例子:

Long sum = 0L;
        long start = System.currentTimeMillis();
        for(long i = 0;i < Integer.MAX_VALUE;i++){
            sum +=i;
        }
        long end = System.currentTimeMillis();
        System.out.println(sum);
        System.out.println(end - start + "ms");

output:

2305843005992468481
5508ms

long sum = 0L;
        long start = System.currentTimeMillis();
        for(long i = 0;i < Integer.MAX_VALUE;i++){
            sum +=i;
        }
        long end = System.currentTimeMillis();
        System.out.println(sum);
        System.out.println(end - start + "ms");

output:

2305843005992468481
612ms

很明显,第二个比第一个快了很多,因为第一个用的是大写的Long类型。变量被反复的装箱和拆箱,导致性能明显下降。

参考:《effective java 2th》

posted @ 2018-09-03 19:25  阿罗luo  阅读(73)  评论(0)    收藏  举报