JAVA编程思想笔记--初始化与清理

来源:互联网 发布:商业网络 编辑:程序博客网 时间:2024/06/05 14:15
  • foreach语法:一种新的更加简洁的for语句用于数组和容器,表示不必创建int变量去对由访问项构成的序列进行计数,foreach将自动产生每一项for(float x:f){} 定义了一个float类型的变量,继而将每一个f的元素赋值给x。
float f[] = new float[10];for(float x : f)  System.out.println(x);
  • break用于强制退出循环,不执行循环中剩下的语句。而continue则停止执行当前迭代,然后退出循环起始处,开始下一次迭代。
    public static void main(String[] args) {        int i = 0 ;          while(true){              i++;              int j = i*27 ;              if(j==1269)break;  //跳出循环              if(i % 10 !=0)continue; //返回循环头部              System.out.println(i+"");          }    }
  • break和continue跟goto一样,使用相同的机制:标签 ;



  • 初始化与清理: 清理指当使用完一个资源时,需要把占用的资源释放。

  • 当创建一个对象时,也就给此对象分配到的存储空间取一个名字。

  • 区分重载方法:每个重载的方法都必须有一个独一无二的参数类型列表

//方法的重载public class Tree {  int height;  Tree(){      height = 0 ;  }  Tree(int initialHeight){      height = initialHeight;  }  void info(){      System.out.println("Tree is " + height +"feet tall");  }  void info(String s){      System.out.println(s + ": Tree is" + height + "feet tall");  }}
  • 涉及基本类型的重载(构造方法,int,char,short):如果传入的数据类型(实际参数类型)小于方法中声明的形式参数类型,实际数据类型就会被提升。char型略有不同,如果无法找到恰好接受char参数的方法,就会把char直接提升到int型。2.如果传入的实际参数较大,就得通过类型转换来执行窄化转换。
public class PrimitiveOverloading {   void f1(char x){System.out.println("f1(char)");}   void f1(byte x){System.out.println("f1(byte)");}   void f1(short x){System.out.println("f1(short)");}   void f1(int x){System.out.println("f1(int)");}   void f1(long x){System.out.println("f1(long)");}   void f1(float x){System.out.println("f1(float)");}   void f1(double x){System.out.println("f1(double)");}   void f2(byte x){System.out.println("f2(byte)");}   void f2(short x){System.out.println("f2(short)");}   void f2(int x){System.out.println("f2(int)");}   void f2(long x){System.out.println("f2(long)");}   void f2(float x){System.out.println("f2(float)");}   void f2(double x){System.out.println("f2(double)");}   void f3(short x){System.out.println("f3(short)");}   void f3(int x){System.out.println("f3(int)");}   void f3(long x){System.out.println("f3(long)");}   void f3(float x){System.out.println("f3(float)");}   void f3(double x){System.out.println("f3(double)");}   void f4(int x){System.out.println("f4(int)");}   void f4(long x){System.out.println("f4(long)");}   void f4(float x){System.out.println("f4(float)");}   void f4(double x){System.out.println("f4(double)");}   void f5(long x){System.out.println("f5(long)");}   void f5(float x){System.out.println("f5(float)");}   void f5(double x){System.out.println("f5(double)");}   void f6(float x){System.out.println("f6(float)");}   void f6(double x){System.out.println("f6(double)");}   void f7(double x){System.out.println("f7(double)");}   void testConstVal(){       System.out.println("5: ");       f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);   }}//output: 5:f1(int) f2(int) f3(int) f4(int) f5(long) f6(float)//发现常数值5被当作int值处理,所以如果有某个重载方法接受int型参数,它就会被调用//至于其他情况,如果传入的数据类型小于方法中声明的形式参数类型,实际数据类型就会被提升。
  • 如果已经定义了一个构造器(无论是否有参数),编译器就不会帮你自动创建默认构造器。

  • this关键字只能在方法内部使用,表示对”调用方法的那个对象”的引用。

  • static的含义:static方法就是没有this的方法,在static方法的内部不能调用非静态方法,反过来倒是可以。而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法。这实际上正是static方法的主要用途

  • 垃圾回收器只知道释放那些经由new分配的内存,假定你的对象(并非使用new)获得一块”特殊”的内存区域,所以它不知道该如何释放改对象的这块”特殊”内存。

  • 只要程序没有频临存储空间用完的那一刻,对象占用的空间就总得不到释放。对象可能不被垃圾回收。垃圾回收只与内存有关。

  • 类的每个基本类型数据成员保证都会有一个初始值。

public class InitialValues {    boolean t ;    char c ;    byte b ;    short s ;    int i ;    long l ;    float f ;    double d ;    InitialValues reference ;    void printInitialValues(){        System.out.println("Data type   Initial value");        System.out.println("boolean     "+t);        System.out.println("char        "+c);        System.out.println("byte        "+b);        System.out.println("short       "+s);        System.out.println("int         "+i);        System.out.println("long        "+l);        System.out.println("float       "+f);        System.out.println("double      "+d);        System.out.println("reference   "+reference);    }}//输出://boolean     false//char        //byte        0//short       0//int         0//long        0//float       0.0//double      0.0//reference   null

初始化顺序:在类的内部,变量定义的先后顺序决定了初始化的顺序,即使变量定义散布于方法定义之间,它们仍旧会在任何方法(包含构造器)被调用之前得到初始化

public class Window {   Window(int marker){       System.out.println(marker);   }}public class House {    Window w1 = new Window(1);  //第一个执行    House(){        System.out.println("House构造方法初始化");        w3 = new Window(33);  //第四个执行    }    Window w2 = new Window(2); //第二个执行    void f(){        System.out.println("f()");    };    Window w3 = new Window(3); //第三个执行}//变量定义散布于方法之间,它们仍旧会在任何方法(包括构造器)被调用之前得到初始化public class ArrayMaker  {       public static void main(String[] args) {         House h = new House();         h.f();    }}
  • 静态数据的初始化
    1.static关键字不能应用于局部变量,因此只能作用于域。
    2.如果一个域是静态的基本类型域,且也没有对它进行初始化,那么它就会获得基本类型的标准初值;如果是一个对象引用,那么它的默认初始化值就是null。
//显示类public class Bowl {    Bowl(int marker){        System.out.println("Bowl("+marker+")");    }    void f1(int marker){        System.out.println("f1("+marker+")");    }}public class Table {    static Bowl bowl1 = new Bowl(1);  //第一个执行    Table(){                          //第三个执行        System.out.println("Table()");        bowl2.f1(1);    }    void f2(int marker){        System.out.println("f2("+marker+")");    }    static Bowl bowl2 = new Bowl(2);  //第二个执行}public class Cupboard {  Bowl bowl3 = new Bowl(3);         //第三个执行  static Bowl bowl4 = new Bowl(4);   //第一个执行  Cupboard(){                       //第四个执行      System.out.println("Cupboard");      bowl4.f1(2);  }  void f3(int marker){      System.out.println("f3("+marker+")");  }  static Bowl bowl5 = new Bowl(5);  //第二个执行}public class ArrayMaker  {       public static void main(String[] args) { //第三个执行         System.out.println("Creating new Cupboard() in main");         new Cupboard();         System.out.println("Creating new Cupboard() in main");         new Cupboard();         table.f2(1);         cupboard.f3(1);    }    static Table table = new Table();  //第一个执行    static Cupboard cupboard = new Cupboard(); //第二个执行}

1.初始化的顺序是先静态对象(如果它们尚未因前面的对象创建过程而被初始化),而后是”非静态”对象。

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

那么i首先会被置0,然后变成7.

2.静态初始化动作只进行一次。

  • 非静态实例初始化
public class Mugs {    Mug mug1;    Mug mug2;    {//实例初始化子句是在两个构造器之前执行        mug1 = new Mug(1);        mug2 = new Mug(2);    }    Mugs(){    }    Mugs(int i){    }}

8.数组的初始化
在java中可以将一个数组赋值给另一个数组,其实真正做的只是复制了一个引用。

int[] a1 = {1,2,3,4,5};int[] a2 ;a2 = a1 ;//例子:public class ArrayMaker  {       public static void main(String[] args) {         int[] a1 = {1,2,3,4,5};        int[] a2 ;        a2 = a1 ;        for(int i = 0 ;i<a2.length;i++)            a2[i] = a2[i] + 1 ;        for(int i = 0 ;i<a1.length;i++){            System.out.println("a1["+i+"]=" + a1[i]);        }    }    //输出:a1[0] = 2  ..........}

(1)int[] a :数组元素中的基本数据类型会自动初始化成空值(对于数字和字符,就是0:对于布尔值,是false)。

(2)整型包装器类Integer它是一个类而不是基本类型。

  • 枚举类型:枚举类型的实例是常量,因此按照命名习惯它们都用大写字母表示。enum是一个类
public class ArrayMaker  {       public static void main(String[] args) {          Spiciness  howHot = Spiciness.MEDIUM; //1.         System.out.println(howHot);         //ordinal用来表示某个特定enum常量声明顺序         for(Spicness s:Spicness.values())  //2.             System.out.println(s+",ordinal"+s.ordinal());     }    public enum Spiciness {        NOT,MILD,MEDIUM,HOT,FlAMING    }}

2.enum在switch中的实用

public class ArrayMaker {    public static void main(String[] args) {         describe(Spiciness.NOT);    }    public enum Spiciness {        NOT, MILD, MEDIUM, HOT, FlAMING    }    //static 方法中只能调用  static 方法    public static  void describe(Spiciness degree) {        switch (degree) {        case NOT:            System.out.println("NOT");            break;        case MILD:            System.out.println("MILD");            break;        default:            break;        }    }}
原创粉丝点击