Java笔记,疯狂Java讲义,第五六章Review

Java笔记

public class StaticCode{
    final int w;
    final  int a;//final成员变量系统不会隐式初始化
    //final 修饰引用数据类型,只保证所引用数据类型的地址不变,但对象的内容可以发生改变

    //final 修饰的类Field初始化:在静态初始化块中或声明时指定值
    //final修饰的实例Field初始化:在非静态初始化块,声明改Field或构造器中指定初始值

    //final修饰方法(其方法不是private),其子类不能重写


    //静态初始化块

    static {
        System.out.println("静态初始化块");

    }
    //普通初始化块
    {
        a = 6;
        if (a > 4){
            System.out.println("StaticCode初始化块:局部变量a的值为 "+a);
            System.out.println("第一个初始化块");
        }
    }

    {

        System.out.println("第二个初始化块");
    }

    public StaticCode(){
        w = 0;
        System.out.println("无参数构造函数");
    }


    public StaticCode(int w){
        this.w = w;
    }

    public static void main(String[] args){

        StaticCode test = new StaticCode(1);
        StaticCode test1 = new StaticCode(2);
        System.out.println(test.w+" || "+ test1.w);


        new StaticCode();



        int i = 9;
    //  System.out.println(i instanceof Integer);这句话是错的 因为i是一个基本数据类型并不是一个类的实例
        //装箱
        Integer i1 = new Integer(i);
        System.out.println(i1 instanceof Integer);
        //拆箱
        System.out.println(i1.intValue());

        /*
            除了Character类以外,其他数据类型对应的包装类都提供parseXxx(String s)
            利用包装类提供的Xxx(String s)
        */
        String ftStr = String.valueOf(2.345f);
        System.out.println(ftStr);


        //== (不要求数据类型一致,另外String类型使用==时要涉及到“常量池”这个概念)和 equals()(String类重写这个Object类的equals()方法,只要字符串序列相同)
        int q1 = 1;
        long q2 = 1;
        System.out.println(q1 == q2);


        /*
            获取实例对象的类名可以用getClass()方法

            在类中用static修饰为类成员,属于整个类,可以通过类来直接调用或者实例来调用,但是,类成员只在类加载时只加载一次,存放在内存区(数据共享区)
        */

        /*
            单例(Singleton)类:做法:把该类构造器隐藏起来,且要提供public static方法访问该类,因此成员变量需要使用static修饰

            class Singleton{

                private static Singleton instance;
                private Singleton(){
                }

                public static SingletongtInstance(){
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                    return instance;
                }
            }

            public class SingletonTest{

                public static void main(String[] args){

                    Singleton s1 = Singleton.getInstance();
                    Singleton.getInstance();

                    System.out.println(s1 == s2);


                }
            }
        */
    }
}


/*
    8个包装类和String类是不可变类,即创建String实例后,其实例Field都是不变的的即hashCode()不变
*/




/*  缓存不可变类:用于创建很多相同的对象。


class CacheImmutale{

    private static int  MAX_SIZE = 10;
    //使用数组来缓存已有的实例
    private static CacheImmutable[] chche = new CacheImmutable[MAX_SIZE];
    //记录缓存实例在缓存中的位置,cache[pos-1]是最新的缓存实例
    private static int pos = 0;
    private final String name;
    private CacheImmutable(String name){

        this.name = name;
    }

    private String getName(){
        return name ;
    }

    public static CacheImmutable valueOf(String name){
        //遍历已缓存的对象
        for(int i = 0; i < MAX_SIZE; i++){

            if(cache[i] != null && cache[i].getName().equals(name))
            {
                return cache[i];
            }
        }
        //如果缓存池已经满
        if (pos == MAX_SIZE)
        {
            //把缓存的第一个对象覆盖,即把刚刚生成的对象放在缓存池的最前面
            cache[0] = new CacheImmutable(name);
            pos = 1;
        }
        else
        {
            //把新创建的对象缓存起来,pos加1
            cache[pos++] = new CacheImmutable(name);
        }
        return cache[pos-1]
    }

    public boolean equals(Object obj){

        if (this == obj){
            return true;
        }
        if(obj != null && obj.getClass() == CacheImmutable.class){

            CacheImmutable ci = (CacheImmutable)obj;
            return name.equals(cigetName());
        }
        return false;
    }

    public int hashCode(){
        return name.hashCode();
    }

}


public class CacheImmutableTest{

    public static void main (String[] args){
        CacheImmutable c1 = CacheImmutable.valueOf("hello");
        CacheImmutable c2 = CacheImmutable.valueOf("hello");
        System.out.println(c1 == c2);
    }
}




*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值