Java basic one: an array of objects, methods

As a refresher, the front part of the foundation just pick some key records


Array

  • Array itself is a reference data type, and the elements of the array may be any type of data, including
    basic data types and the reference data type.
  • Create an array of objects would open up a block of contiguous space in memory, while the array is the first name referenced in this continuous address space.
  • Once the length of the array, it can not be modified.

Using an array of

initialization

  • Dynamic Initialization: array declaration and assignment separately
int[] arr = new int[2];
arr[0] = 1;
arr[1] = 2;
  • Static Initialization: on assignment for the array element in the definition of the array at the same time
int[] arr = new int[]{1, 2};

Default initialization

  • Array is a reference type, whose elements corresponding to the class member variable, so the space allocated an array, where each element is also in the same manner as member variables are initialized implicitly, default initial value is 0.
  • For basic data type, default initialization value 0 (0.0, false and the like)
  • For reference data type, the default value is null initialization

An array of memory parsing

int[] s

Here Insert Picture Description

s = new int[10]

Here Insert Picture Description

  • Variable stack used to store local variables, such as the definition in the method (such as the main method, various custom methods) are local variables in
  • String stored in the constant pool, and the constant pool can have the same string (implication, the same point to the same address string String)
  • static stored in static fields
    Here Insert Picture Description
  • int[] agesCreate a stack agesvariable which points specified int[]objects; new int[4]heap created integer array length of 4; 0 is default initialization; int[] ages = new int[4]the first address value assigned to the array ages; ages[0] = 12the three sentences, each array element corresponding to modified content
  • String[] names在栈中创建了names变量,指定它指向String[]对象;new String[]{"赵宇", "张凯", "江运", "曹林"}在堆中创建长度为4的字符串数组,默认初始化为null,紧接着对每个数组元素的内容进行修改。然后把数组首地址值赋给namesnew String[]{"Tom", "Jerry"}创建并赋值了一个新的数组,names = new String[]{"Tom", "Jerry"}把新数组首地址值赋给names,覆盖了原地址;原来的数组此时没有变量指向,被丢弃,后续会有垃圾回收机制进行空间回收
    Here Insert Picture Description

多维数组

  • 对于二维数组的理解,我们可以看成是一维数组 array1又作为另一个一维数组array2的元素而存 在。其实,从数组底层的运行机制来看,其实没有多维数组

多维数组的使用

  • 和一维数组一样,有动态初始化和静态初始化
  • 初始化时可以不指定第二维数组的长度,第二维数组每一个后续可以赋予不同的长度,例如:
int[][] arr = new int[2][];
arr[0] = new int[3];
arr[1] = new int[2];
  • 但是诸如int[][] arr = new int[][2]这种创建形式是非法的。

特殊写法:int[] x, y[]:x是一维数组,y是二维数组

附:
声明:int[] x,y[]; 在给x,y变量赋值以后,以下选项允许通过编译的是:
a) x[0]=y; //no。左为单个元素右为二维数组
b) y[0] = x; //yes。左为一维数组右为一维数组
c) y[0][0] = x; no
d) x[0][0] = y; //no。左边本身就错的
e) y[0][0] = x[0]; yes
f ) x = y; no
以上就看等号左右是不是同类型的对象,是就可以赋值,不是就不可以。


多维数组内存解析

Here Insert Picture Description


Arrays工具类

1 boolean equals(int[] a, int[] b) 判断两个数组是否相等
2 String toString(int[] a) 以字符串输出数组
3 void fill(int[] a, int val) 将指定值填充到数组之中
4 void sort(int[] a) 对数组进行排序
5 int binarySearch(int[] a, int key) 对排序后的数组进行二分查找指定的值

数组常见异常

  • 数组脚标越界异常:ArrayIndexOutOfBoundsException
  • 空指针异常:NullPointerException

面向对象

  • 三大特性:封装、继承、多态

对象

对象的内存解析

  • 堆(Heap),此内存区域的唯一目的就是存放对象实例,几乎所有的对象实例都在这里分配内存。这一点在 Java虚拟机规范中的描述是:所有的对象实例以及数组都要在堆上分配。
  • 通常所说的栈(Stack),是指虚拟机栈。虚拟机栈用于存储局部变量等。 局部变量表存放了编译期可知长度的各种基本数据类型(boolean、byte、 char 、 short 、 int 、 float 、 long 、 double)、对象引用(reference类型, 它不等同于对象本身,是对象在堆内存的首地址)。 方法执行完,自动释放。
  • 方法区(Method Area),用于存储已被虚拟机加载的类信息、常量、静态变量、即时编译器编译后的代码等数据。
    Here Insert Picture Description
    Here Insert Picture Description

对象数组的内存解析

Here Insert Picture Description


方法

变量的分类

  • 变量分为
    • 在方法体外,类体内生命的变量称为成员变量
      • 实例变量(不以static修饰)
      • 类变量(以static修饰)
    • 在方法体内声明的变量称为局部变量
      • 形参(方法、构造器中定义的变量)
      • 方法局部变量(在方法内定义)
      • 代码块局部变量(在代码块内定义)
  • 内存加载位置:
    • 成员变量:堆空间或静态域内
    • 局部变量:栈空间

方法的重载

  • 重载的概念:在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。
  • 重载的特点:与返回值类型无关,只看参数列表,且参数列表必须不同。(参数个数或参数类 型)。调用时,根据方法参数列表的不同来区别。

方法的重写

这是出现在继承里的一个概念,指在子类中可以根据需要对从父类中继承来的方法进行改造,也称为方法的重置、覆盖。在程序执行时,子类的方法将覆盖父类的方法。

  • 子类重写的方法必须和父类被重写的方法具有相同的方法名称、参数列表
  • 子类重写的方法的返回值类型不能大于父类被重写的方法的返回值类型
  • 子类重写的方法使用的访问权限不能小于父类被重写的方法的访问权限(子类不能重写父类中声明为private权限的方法。不过写了也不报错,但此时不是重写了,各用各的方法。因为子类根本“看不见”父类的private方法,所以也不存在“重写”)
  • 子类方法抛出的异常不能大于父类被重写方法的异常

注:子类与父类中同名同参数的方法必须同时声明为非static的(即为重写),或者同时声明为 static的(不是重写)。因为static方法是属于类的,子类无法覆盖父类的static方法。

可变个数的形参

  • JDK 5.0以前:采用数组形参来定义方法,传入多个同一类型变量
 public static void test(int a ,String[] books); 
  • JDK5.0:采用可变个数形参来定义方法,传入多个同一类型变量
public static void test(int a ,String...books);

一个方法最多只能声明一个可变个数的形参,并且要放在形参声明的最后


方法参数的值传递机制(重点!!!!!!!)

  • 方法,必须由其所在类或对象调用才有意义。若方法含有参数:

    • 形参:方法声明时的参数
    • 实参:方法调用时实际传给形参的参数值
  • Parameter passing method in Java is only one: the value passed. A copy of the actual parameter values ​​within the coming of (replica) passed into the method, and the parameter itself is not affected.

    • If the parameter is a basic data types: the argument is the basic data type of the variable " data value " to the parameter
    • If the parameter is a reference data type: the argument is a reference "data type of the variable address value " to the parameter
  • Parameter passing basic data types:
    Here Insert Picture Description

  • Reference data type of parameter passing:

public static void main(String[] args) {
	Person obj = new Person();
	obj.age = 5;
	System.out.println("修改之前age = " + obj.age);// 5 // x是实参
	change(obj);
	System.out.println("修改之后age = " + obj.age);// 3 
}
public static void change(Person obj) { 
	System.out.println("change:修改之前age = " + obj.age); 
	obj.age = 3;
	System.out.println("change:修改之后age = " + obj.age);
}

class Person{
	int age; 
}

Here Insert Picture Description

public static void main(String[] args) {
	Person obj = new Person();
	obj.age = 5;
	System.out.println("修改之前age = " + obj.age);// 5 // x是实参
    change(obj);
	System.out.println("修改之后age = " + obj.age);// 5 
}
public static void change(Person obj) {
	obj = new Person(); //创建了新对象
	System.out.println("change:修改之前age = " + obj.age); 
	obj.age = 3;//指向的是新对象的age
	System.out.println("change:修改之后age = " + obj.age);
}
class Person{
	int age; 
}

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

public class TransferTest3 {
    public static void main(String args[]) {
        TransferTest3 test = new TransferTest3();
        test.first();
    }

    public void first() {
        int i = 5;
        Value1 v = new Value1();
        v.i = 25;
        second(v, i); 
        System.out.println("first:" + v.i + " " + i);
    }
    public void second(Value1 v, int i) {
        i = 0;
        v.i = 20;
        Value1 val = new Value1();
        v = val;
        System.out.println("second:" + v.i + " " + i);

    }
}

class Value1 {
    int i = 15;
}

Here Insert Picture Description


  • Questions 1
public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 10;
        method(a, b);
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        //需要只打印出a = 100, b = 200.请写出method方法的代码
    }
}

A thought: to rewrite the output stream

public static void method(int a, int b){
        PrintStream ps = new PrintStream(System.out){
            @Override
            public void println(String x){
                if("a = 10".equals(x)){
                    x = "a = 100";
                }else if ("b = 10".equals(x)){
                    x = "b = 200";
                }
                
                super.println(x);
            }
        };
        
        System.setOut(ps);
    }

Thinking two: do not execute the back System.out.println:

public static void method(int a, int b){
        a = a * 10;
        b = b * 20;
        System.out.println(a);
        System.out.println(b);
        System.exit(0);
    }

  • Thinking Problem 2:
    define an int array: int[] arr = new int[]{12,3,3,34,56,77,432}; value of each position in the array so that the removal of elements of the first results of position, resulting, as the new value at that location. Traversing the new array.

Error writing:

for(int i= 0;i < arr.length;i++){
	arr[i] = arr[i] / arr[0]; 
}

A correct wording:

for(int i = arr.length – 1;i >= 0;i--){
	arr[i] = arr[i] / arr[0]; 
}

Correct wording II:

int temp = arr[0];
for(int i= 0;i < arr.length;i++){
	arr[i] = arr[i] / temp; 
}

Attachment: experience recursive want clear, you can think about a way in the figure below
Here Insert Picture Description

Released three original articles · won praise 0 · Views 50

Guess you like

Origin blog.csdn.net/bigdreamerxz/article/details/104576142