zoukankan      html  css  js  c++  java
  • [笔记]java初始化,数组

    public class xxx{

    //data初始化,或者在{}初始化

    //method

    //static method,data初始化,或者在{}初始化

    }

    this关键字

    垃圾收集器:释放由new分配的内存,

    一旦垃圾收集器准备好释放对象占用的存储空间,它首先调用 finalize(),而且只有在下 一次垃圾收集过程中,才会真正回收对象的内存。所以如果使用 finalize(),就可以在垃圾收集期间进行一 些重要的清除或清扫工作。 finalize()主要用来释放调用c/c++中的内存和观察垃圾收集的过程。 

    java 不允许我们创建本地(局部)对象——无论如何都要使用 new 

     

    制定初始化:

    类似C++的“构建器初始模块列表"

    //那些自变量不可是尚未初始化的其他类成员。因此,下面这样做是合法 的:
    class CInit {
     int i = f(); 
    int j = g(i); //...
    }
    //但下面这样做是非法的:
    class CInit {
     int j = g(i); 
    int i = f(); //...
    }
    class Mug { Mug(int marker) {
    System.out.println("Mug(" + marker + ")"); }
    void f(int marker) { System.out.println("f(" + marker + ")");
    }
    }
    public class Mugs { Mug c1; Mug c2;
    { //块初始化 c1
    = new Mug(1); c2 = new Mug(2); System.out.println("c1 & c2 initialized"); }
    Mugs() { System.out.println(
    "Mugs()"); } public static void main(String[] args) { System.out.println("Inside main()"); Mugs x = new Mugs(); } } ///:

    class Counter {
    int i;
    Counter() { i = 7; } 

    }//那么 i 首先会初始化成零,然后变成 7 

     

    static 初始化: data同非static,

    如果它属于一个基本类型(主类型),而且未对其 初始化,就会自动获得自己的标准基本类型初始值;如果它是指向一个对象的句柄,那么除非新建一个对 象,并将句柄同它连接起来,否则就会得到一个空值(NULL) 

    初始化的顺序是首先static(如果它们尚未由前一次对象创建过程初始化),接着是非static 对象

    明确进行的静态初始化 ,“static构建从句”(有时也叫作“静态 块”):

    class Spoon { 
    static int i;
     static {
        i = 47; }
    }

    这段代码仅执行一次——首次生成那个类的一个对象时,或者首次访问属于那个类的一个 static 成员时 (即便从未生成过那个类的对象)

     

    基本数据类型的数组元素会自动初始化成“空” 值(对于数值,空值就是零;对于char,它是null;而对于boolean,它却是false) 

    数组可以赋值,相当于别名

    public class Arrays {
    public static void main(String[] args) {
    
    Integer[] a = { new Integer(1), new Integer(2), new Integer(3),}; //包含对象的数组
    f(new Object[] {
    new Integer(47), new VarArgs(), //动态分配
    new Float(3.14), new Double(11.11) });
    f(new Object[] {"one", "two", "three" });
    f(new Object[] {new A(), new A(), new A()});
    Integer[][] a4 = {
    { new Integer(1), new Integer(2)}, { new Integer(3), new Integer(4)}, { new Integer(5), new Integer(6)},
    };//多维数组
    
    int[] a1 = { 1, 2, 3, 4, 5 }; int[] a2;
    a2 = a1;
    for(int i = 0; i < a2.length; i++)
    a2[i]++;
    for(int i = 0; i < a1.length; i++)
    prt("a1[" + i + "] = " + a1[i]); }
    static void prt(String s) { System.out.println(s);
    }
    static void f(Object[] x) {
    for(int i = 0; i < x.length; i++) System.out.println(x[i]);
    }
    } ///:~
    run:
    a1[0] = 2
    a1[1] = 3
    a1[2] = 4
    a1[3] = 5
    a1[4] = 6

    初始化顺序:

    基类static初始化->子类static初始化->基类非static初始化->基类init->子类非static初始化->子类init

    public class JavaApplication2 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            test2 teademo = new test2();
        }
    }
    class test1{
        static {
            System.out.println("static test1");
        }
          {
            System.out.println("no static test1");
        }
          public test1(){
              System.out.println("init test1");
          }
    }
    class test2 extends test1{
        static {
            System.out.println("static test2");
        }
          {
            System.out.println("no static test2");
        }
          public test2(){
              System.out.println("init test2");
          }
    run:
    static test1
    static test2
    no static test1
    init test1
    no static test2
    init test2
    }
  • 相关阅读:
    小小的封装了一个pie的echarts
    recent.css常用的页面初始化样式
    bootstrap表格多样式及代码
    Java内存回收机制
    栈帧
    互斥锁和条件变量
    UML类图的常见关系
    堆栈详解(数据与内存中的存储方式)
    PR消减视频中的鼠标声
    SmartPlant Review 三维视图快捷键
  • 原文地址:https://www.cnblogs.com/zengyou/p/2771638.html
Copyright © 2011-2022 走看看