动手动脑2

1.纯随机数生成方法:

我们可以用math.random来生成随机数,但是通过这种方法生成的随机数会出现重复,因此它生成的随机数称为伪随机数。为了避免发生重复,可以运用种子和random类共同生成随机数,这种方法通过对种子的不断更新进行随机数的输出,而使重复的概率大大降低,以下是老师提供的生成随机数的公式x[n+1]=(ax[n]+c) mod m,生成的随机数可以称为纯随机数。

生成纯随机数的源代码如下:

package suijishu;
import java.util.Random;
import java.util.Scanner;
public class Dong_1 {
	public static void main(String[] args)
	{
		shengcheng();//调用随机数生成的函数
	}
	private static void shengcheng()//定义生成随机数的方法
	{
		long seed=1000;
		Scanner reader=new Scanner(System.in);
		System.out.println("请输入所生成随机数的个数")	;
		int ge=reader.nextInt();
		for(int i=0;i<ge;i++)
		{
			seed=setseed(seed);
			Random a=new Random(seed);//种子
			System.out.println(a.nextInt());
		}
		
		
	}
	private static long setseed(long seed)
	{//生成种子
		long seeds;
		seeds=(long) ((16807*seed)%(2e31-1));
		return seeds;
	}

}

2.jdk的System.out.println方法

以下是jdk中此方法的源码

//System.out.println()的jdk源码:
    /**
     * Prints a String and then terminate the line.  This method behaves as
     * though it invokes <code>{@link #print(String)}</code> and then
     * <code>{@link #println()}</code>.
     *
     * @param x  The <code>String</code> to be printed.
     */
    public void println(String x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }

  通过源码可知System是一个类,继承自根类Object,out是类PrintStream类实例化的一个对象,且是System类的静态成员变量,println()是PrintStream的成员方法,被对象out调用。通过查询可知在Java项目中日志输出不建议使用System.out.println(),和log4j等日志工具相比,除了不能对日志进行灵活配置还会影响其性能,代码中的System.out.println()和Java运行程序在同一线程,业务程序会等待system.out的动作导致资源被占用。所以得出结论大量使用system.out.println()势必会影响项目的性能。

3.以下程序的特殊之处

public class MethodOverload {
	public static void main(String[] args) {
		System.out.println("The square of integer 7 is"+square(7));
		System.out.println("
The square of double 7.5 is"+square(7.5));
	}
	public static int squre(int x)
	{
		return x*x;
	}
	public static double square(double y) {
		return y*y;
	}

}

  此程序运行后的结果为

The square of integer 7 is49.0

The square of double 7.5 is56.25

  通过观察两个函数的函数名相同,数据类型不同,所以使用相同的函数名可以执行不同的操作。

4.Java的方法重载

方法名相同

方法的参数类型,参数个数不一样

方法的返回值类型可以不相同(返回值不作为方法重载的判断条件)

方法的修饰符可以不相同

main方法也可以被重载

  

迷失在灿烂之中 消失在万里晴空
原文地址:https://www.cnblogs.com/wxy2000/p/9787096.html