javase personal garbage review notes 08 variable parameters... and finalize() method, and Java Scanner class

Variable parameters
In the method declaration, add an ellipsis (...) after the specified parameter type.

Only one variable parameter can be specified in a method, and it must be the last parameter of the method. Any ordinary parameters must be declared before it.

public class VarargsDemo {
    
    
    public static void main(String args[]) {
    
    
        // 调用可变参数的方法
        printMax(34, 3, 3, 2, 56.5);
        printMax(new double[]{
    
    1, 2, 3});
    }
 
    public static void printMax( double... numbers) {
    
    
        if (numbers.length == 0) {
    
    
            System.out.println("No argument passed");
            return;
        }
 
        double result = numbers[0];
 
        for (int i = 1; i <  numbers.length; i++){
    
    
            if (numbers[i] >  result) {
    
    
                result = numbers[i];
            }
        }
        System.out.println("The max value is " + result);
    }
}
/*以上实例编译运行结果如下:

The max value is 56.5
The max value is 3.0

The finalize() method
Java allows you to define such a method, which is called before the object is destroyed (reclaimed) by the garbage collector. This method is called finalize( ), which is used to clear the reclaimed object.

For example, you can use finalize() to ensure that a file opened by an object is closed.

public class FinalizationDemo {
    
      
  public static void main(String[] args) {
    
      
    Cake c1 = new Cake(1);  
    Cake c2 = new Cake(2);  
    Cake c3 = new Cake(3);  
      
    c2 = c3 = null;  
    System.gc(); //调用Java垃圾收集器
  }  
}  
 
class Cake extends Object {
    
      
  private int id;  
  public Cake(int id) {
    
      
    this.id = id;  
    System.out.println("Cake Object " + id + "is created");  
  }  
    
  protected void finalize() throws java.lang.Throwable {
    
      
    super.finalize();  
    System.out.println("Cake Object " + id + "is disposed");  
  }  
}
运行以上代码,输出结果如下:

$ javac FinalizationDemo.java 
$ java FinalizationDemo
Cake Object 1is created
Cake Object 2is created
Cake Object 3is created
Cake Object 3is disposed
Cake Object 2is disposed

Java Scanner class
The following is the basic syntax for creating Scanner objects:
Scanner s = new Scanner(System.in);
Use the next method:

import java.util.Scanner; 
 
public class ScannerDemo {
    
    
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据
 
        // next方式接收字符串
        System.out.println("next方式接收:");
        // 判断是否还有输入
        if (scan.hasNext()) {
    
    
            String str1 = scan.next();
            System.out.println("输入的数据为:" + str1);
        }
        scan.close();
    }
}
/*执行以上程序输出结果为:

$ javac ScannerDemo.java
$ java ScannerDemo
next方式接收:
runoob com
输入的数据为:runoob

Use the nextLine method:

import java.util.Scanner;
 
public class ScannerDemo {
    
    
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据
 
        // nextLine方式接收字符串
        System.out.println("nextLine方式接收:");
        // 判断是否还有输入
        if (scan.hasNextLine()) {
    
    
            String str2 = scan.nextLine();
            System.out.println("输入的数据为:" + str2);
        }
        scan.close();
    }
}

The difference between next() and nextLine()
next():
1. You must read valid characters before you can end the input.
2. The next() method will automatically remove blanks encountered before entering valid characters.
3. Only after valid characters are entered, the blanks entered after them are used as separators or terminators.
next() cannot get a string with spaces.

nextLine():
1. Take Enter as the terminator, which means that the nextLine() method returns all the characters before the carriage return.
2. Blank can be obtained.

If you want to input int or float type data, it is also supported in the Scanner class, but it is better to use the hasNextXxx() method to verify before inputting, and then use nextXxx() to read

Code example 1:

import java.util.Scanner;
 
public class ScannerDemo {
    
    
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据
        int i = 0;
        float f = 0.0f;
        System.out.print("输入整数:");
        if (scan.hasNextInt()) {
    
    
            // 判断输入的是否是整数
            i = scan.nextInt();
            // 接收整数
            System.out.println("整数数据:" + i);
        } else {
    
    
            // 输入错误的信息
            System.out.println("输入的不是整数!");
        }
        System.out.print("输入小数:");
        if (scan.hasNextFloat()) {
    
    
            // 判断输入的是否是小数
            f = scan.nextFloat();
            // 接收小数
            System.out.println("小数数据:" + f);
        } else {
    
    
            // 输入错误的信息
            System.out.println("输入的不是小数!");
        }
        scan.close();
    }
}
/*执行以上程序输出结果为:

$ javac ScannerDemo.java
$ java ScannerDemo
输入整数:12
整数数据:12
输入小数:1.2
小数数据:1.2

Code example 2:

import java.util.Scanner;
 
class ScannerDemo {
    
    
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
 
        double sum = 0;
        int m = 0;
 
        while (scan.hasNextDouble()) {
    
    
            double x = scan.nextDouble();
            m = m + 1;
            sum = sum + x;
        }
 
        System.out.println(m + "个数的和为" + sum);
        System.out.println(m + "个数的平均值是" + (sum / m));
        scan.close();
    }
}
/*执行以上程序输出结果为:

$ javac ScannerDemo.java
$ java ScannerDemo
12
23
15
21.4
end
4个数的和为71.4
4个数的平均值是17.85

Guess you like

Origin blog.csdn.net/qq_45864370/article/details/108549596
Recommended