Math类、Random类和System类的基本概述及其常用方法

一:Math类:

                Math类概述:  Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
                成员变量
                                 public static final double E :         自然底数
                                 public static final double PI:        圆周率
                 成员方法
                                  public static int abs(int a)        取绝对值
                                  public static double ceil(double a)    向上取整
                                  public static double floor(double a)    向下取整
                                  public static int max(int a,int b)      获取最大值
                                  public static int min(int a, int b)    获取最小值
                                  public static double pow(double a,double b) 获取a的b次幂
                                  public static double random()    获取随机数 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
                                  public static int round(float a) 四舍五入
                                  public static double sqrt(double a)获取正平方根

Math类中方法应用举例:

package org.westos.demo;

public class MathDemo {

	public static void main(String[] args) {
		//Math数学工具类
		//静态常量
		System.out.println(Math.PI);//圆周率
		System.out.println(Math.E);//自然指数
		//静态方法
		System.out.println(Math.abs(-1));//求绝对值
		System.out.println(Math.max(Math.max(3.14,6), 60));//求最大值
		System.out.println(Math.min(3, 6));//求最小值
		System.out.println(Math.ceil(3.14));//向上取整
		System.out.println(Math.floor(3.14));//向下取整
		System.out.println(Math.round(3.14));//四舍五入
		System.out.println(Math.random());//取随机数 0---1
		System.out.println(Math.pow(2, 3));//求一个数的几次幂
		
		

	}

}

二:Random类

            Random类的概述:
                          此类用于产生随机数如果用相同的种子创建两个 Random 实例,
                          则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
            构造方法
                           public Random()             没有给定种子,使用的是默认的(当前系统的毫秒值)
                           public Random(long seed)         给定一个long类型的种子,给定以后每一次生成的随机数是相同的
            成员方法
                           public int nextInt()//没有参数 表示的随机数范围 是int类型的范围
                           public int nextInt(int n)//可以指定一个随机数范围

Random类中方法的应用举例:

package org.westos.demo;

import java.util.Random;

public class RandomDemo {

	public static void main(String[] args) {

		Random random = new Random();
 
//             	 //生成随机数据 int nextInt() 
//		 int nextInt()  返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。 
//        	
//               int nextInt(int n) 返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。 
			for(int i=0;i<10;i++) {
				//你不传参 生成的随机整数在int范围内
				//int num = random.nextInt();
				//指定生成随机数的范围
				int num=random.nextInt(100);//  0=<随机数<100
				System.out.println(num);
			}
		


	}

}

三:System类

              System类的概述:
                         System 类包含一些有用的类字段和方法。它不能被实例化
                          in:标准输入流                InputStream  字节输入流
                         

out:标准输出流                PrintStream  字节打印流                PrintWriter:字符打印流
              成员方法
                         public static void gc()//调用垃圾回收器
                         public static void exit(int status)//退出java虚拟机 0 为正常退出 非0为 异常退出
                         public static long currentTimeMillis()//获取当前时间的毫秒值

System类中方法应用举例:

package westos.org.system.demo;
/*
 * 
 * 	gc():public static void gc()  :Java 虚拟机做了一些努力来回收未用对象
 * 				开启垃圾回收器,其实质是是调用了重写Object中的finalize()方法
 * 
 * 			final,finalize,finally(加入异常操作的时候:处理异常释放资源)之间的区别?
 * 		public static void exit(int status):这个方法执行,jvm就退出了 ,参数是0
 * @author Administrator
 *
 */
public class SystemDemo {
	
	public static void main(String[] args) {
		System.out.println("我爱***");
		System.exit(0); //jvm退出了...
		System.out.println("我还爱***");
		
		
		
		//创建学生对象
		Student student = new Student("高圆圆",28	) ;
		System.out.println(student);//westos.org.system.demo.Student@70dea4e
		//Student [name=高圆圆, age=28]
		
		
		student = null ;//student不在指定堆内存
		//启动垃圾回收器
		System.gc(); 
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41942241/article/details/81350161