java 学习笔记(一)

对象

  • new 出来的对象都是保存在堆内存中的
  • 不是使用 new 创建变量,而是使用一个“自动”变量。 这个变量直接存储"值",并置于栈内存中,因此更加高效。
  • 基本类型有自己对应的包装类型,如果你希望在堆内存里表示基本类型的数据,就需要用到它们的包装类
  • 或者使用 自动装箱

    Character ch = 'x';

    image-20200429150556862.png

对象作用域

Java 对象与基本类型具有不同的生命周期。当我们使用 new 关键字来创建 Java 对象时,它的生命周期将会超出作用域。

基本类型默认值

image-20200429152518319.png

无参构造器

一旦你显式地定义了构造器(无论有参还是无参),编译器就不会自动为你创建无参构造器。

this

  • this就是调用改方法的对象,就像是

image-20200429204229894.png

  • 或者用去在构造器中调用其他其他构造器
  • static 方法中不会存在 this。你不能在静态方法中调用非静态方法

static只能用于属性(字段、域) 不能用于局部变量

静态初始化顺序

class Bowl {
  Bowl(int marker) {
    System.out.println("Bowl(" + marker + ")");
  }
  void f1(int marker) {
    System.out.println("f1(" + marker + ")");
  }
}

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);
}

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 StaticInitialization {
  public static void main(String[] args) {
    System.out.println("main creating new Cupboard()");
    new Cupboard();
    System.out.println("main creating new Cupboard()");
    new Cupboard();
    table.f2(1);
    cupboard.f3(1);
  }
  static Table table = new Table();
  static Cupboard cupboard = new Cupboard();
}

==输出:==

Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
main creating new Cupboard()
Bowl(3)
Cupboard()
f1(2)
main creating new Cupboard()
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
  1. 一个java文件里面只能有一个public class
  2. 静态属性安申明顺序执行,之后再是构造器
  3. 类中的static只会初始化一次

非静态初始化

static{
    /* 静态代码块*/
}

{
    /*非静态代码块*/
}
  1. ==非静态代码块在每次生成对象是都会执行==

// housekeeping/Mugs.java
// Instance initialization

class Mug {
    Mug(int marker) {
        System.out.println("Mug(" + marker + ")");
    }
}

public class Mugs {
    Mug mug1;
    Mug mug2;
    { // [1]
        mug1 = new Mug(1);
        mug2 = new Mug(2);
        System.out.println("mug1 & mug2 initialized");
    }

    Mugs() {
        System.out.println("Mugs()");
    }

    Mugs(int i) {
        System.out.println("Mugs(int)");
    }

    public static void main(String[] args) {
        System.out.println("Inside main()");
        new Mugs();
        System.out.println("new Mugs() completed");
        new Mugs(1);
        System.out.println("new Mugs(1) completed");
    }
}

==输出:==

Inside main
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs()
new Mugs() completed
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs(int)
new Mugs(1) completed

数组的创建

int[] a1 = {1, 2, 3, 4, 5};
int[] a = new int[rand.nextInt(20)];
或者
new String[] {"fiddle", "de", "dum"}

==数组的定义==

int[] a
int a[]

可变参数列表

static void printArray(Object... args)
public class NewVarArgs {
    static void printArray(Object... args) {
        for (Object obj : args)
            System.out.print(obj + "!! ");
        System.out.println();
    }

    public static void main(String[] args) {
        // Can take individual elements:
        printArray(47, (float) 3.14, 11.11);
        printArray(47, 3.14F, 11.11);
        printArray("one", "two", "three");
        printArray(new A(), new A(), new A());
        // Or an array:
        printArray((Object[]) new Integer[] { 1, 2, 3, 4 });
        printArray(); // Empty list is OK
    }
}

==输出:==

image-20200429223907779.png

==如果把数组传入可变对象参数中,该方法会把它们当作可变参数列表来接受。==

评论

暂无评论