博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
03、常用类解析
阅读量:7080 次
发布时间:2019-06-28

本文共 18443 字,大约阅读时间需要 61 分钟。

一、常用类解析

1.1、String类

String类是被final所修饰的,不可以被继承。

构造函数:

  • String(byte[ ] bytes):通过byte数组构造字符串对象。
  • String(char[ ] value):通过char数组构造字符串对象。
  • String(Sting original):构造一个original的副本。即:拷贝一个original。
  • String(StringBuffer buffer):通过StringBuffer数组构造字符串对象。

常用方法:

  • length() 字符串的长度。
  • charAt() 截取一个字符。
  • getChars() 截取多个字符。
  • getBytes()替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()。
  • toCharArray() 转换成字节数组。
  • equals()和equalsIgnoreCase() 比较两个字符串
  • regionMatches() 用于比较一个字符串中特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写。
  • startsWith()和endsWith() startsWith()方法决定是否以特定字符串开始,endWith()方法决定是否以特定字符串结束
  • equals() 字符串比较。
  • compareTo()和compareToIgnoreCase() 比较字符串
  • indexOf()和lastIndexOf()
  • substring() 它有两种形式,第一种是:String substring(int startIndex)
  • concat() 连接两个字符串
  • replace() 替换
  • trim() 去掉起始和结尾的空格
  • valueOf() 转换为字符串
  • toLowerCase() 转换为小写
  • toUpperCase() 转换为大写

具体请查询String的api:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

两种占位符:

第一种

String str = "select %s from %s";String formatStr = String.format(str, "sn", "users");

第二种

String str = "select {0} from {1}";String formatStr = MessageFormat.format(str, "sn", "users");

1.2、System类

System类中包含一些有用的类字段和方法,它不能被实例化,所有的方法和属性都是静态的。

  • out:标准输出,默认是控制台,
  • in:标准输入,默认是键盘。
  • err:标准错误输出                                   

描述系统信息:

Properties getProperties():获取系统属性信息。

  • 因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。
  • 那么可以通过map的方法取出该集合中的元素。
  • setProperty():在系统中自定义特有信息。
  • getProperty():获取系统指定属性信息。

打印系统信息:

Properties properties = System.getProperties();Set
propertyNames = properties.stringPropertyNames();for (String name : propertyNames) { System.out.println(name + "::" + properties.getProperty(name));}

1.3、Runtime类

  Runtime类封装了运行时的环境。每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。

一般不能实例化一个Runtime对象,应用程序也不能创建自己的 Runtime 类实例,但可以通过 getRuntime 方法获取当前Runtime运行时对象的引用。
一旦得到了一个当前的Runtime对象的引用,就可以调用Runtime对象的方法去控制Java虚拟机的状态和行为。
当Applet和其他不被信任的代码调用任何Runtime方法时,常常会引起SecurityException异常。

1、方法介绍

注:其中过时的方法这里不列举。

2、常用操作

a) 内存管理

  Java提供了无用单元自动收集机制。通过totalMemory()和freeMemory()方法可以知道对象的堆内存有多大,还剩多少。Java会周期性的回收垃圾对象(未使用的对象),

以便释放内存空间。但是如果想先于收集器的下一次指定周期来收集废弃的对象,可以通过调用gc()方法来根据需要运行无用单元收集器。一个很好的试验方法是先调用gc()方法,

然后调用freeMemory()方法来查看基本的内存使用情况,接着执行代码,然后再次调用freeMemory()方法看看分配了多少内存。下面的程序演示了这个构想。

public class MemoryDemo {    public static void main(String args[]) {        Runtime r = Runtime.getRuntime();        long mem1, mem2;        Integer someints[] = new Integer[1000];        System.out.println("Total memory is :" + r.totalMemory());        mem1 = r.freeMemory();        System.out.println("Initial free is : " + mem1);        r.gc();        mem1 = r.freeMemory();        System.out.println("Free memory after garbage collection : " + mem1);        // allocate integers        for (int i = 0; i < 1000; i++)            someints[i] = new Integer(i);        mem2 = r.freeMemory();        System.out.println("Free memory after allocation : " + mem2);        System.out.println("Memory used by allocation : " + (mem1 - mem2));        // discard Intergers        for (int i = 0; i < 1000; i++)            someints[i] = null;        r.gc(); // request garbage collection        mem2 = r.freeMemory();        System.out.println("Free memory after collecting "                + "discarded integers : " + mem2);    }}

输出结果:

Total memory is :257425408Initial free is : 253398664Free memory after garbage collection : 256881992Free memory after allocation : 255539768Memory used by allocation : 1342224Free memory after collecting discarded integers : 256882512

b) 执行程序

  在安全的环境中,可以在多任务操作系统中使用Java去执行其他特别大的进程(也就是程序)。ecec()方法有几种形式命名想要运行的程序和它的输入参数。

ecec()方法返回一个Process对象,可以使用这个对象控制Java程序与新运行的进程进行交互。ecec()方法本质是依赖于环境。

下面的例子是使用ecec()方法启动windows的记事本notepad。这个例子必须在Windows操作系统上运行。

public class ExecDemo {    public static void main(String args[]) {        Runtime r = Runtime.getRuntime();        Process p = null;        try {            p = r.exec("notepad");        } catch (Exception e) {            System.out.println("Error executing notepad.");        }    }}

下面是关于ecec()方法的例子的改进版本。例子被修改为等待,直到运行的进程退出后,程序才结束:

public class ExecDemoFini {    public static void main(String args[]) {        Runtime r = Runtime.getRuntime();        Process p = null;        try {            p = r.exec("notepad");            p.waitFor();        } catch (Exception e) {            System.out.println("Error executing notepad.");        }        System.out.println("Notepad returned " + p.exitValue());    }}

当子进程正在运行时,可以对标准输入输出进行读写。getOutputStream()方法和getInPutStream()方法返回对子进程的标准输入和输出(已过时)。

1.4、时间和日期

日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式都是非常复杂的问题。

在Java中,操作日期主要涉及到一下几个类:

1、Date

  Date 表示特定的瞬间,精确到毫秒。从 JDK 1.1 开始,应该使用 Calendar 类实现日期和时间字段之间转换,使用 DateFormat 类来格式化和分析日期字符串。

Date 中的把日期解释为年、月、日、小时、分钟和秒值的方法已废弃(不列举)。

下面是一个Date类的综合实例:

public class TestDate {    public static void main(String args[]) {        TestDate nowDate = new TestDate();        nowDate.getSystemCurrentTime();        nowDate.getCurrentDate();    }    /**     * 获取系统当前时间     * System.currentTimeMillis()返回系统当前时间,结果为1970年1月1日0时0分0秒开始,到程序执行取得系统时间为止所经过的毫秒数     * 1秒=1000毫秒     */    public void getSystemCurrentTime() {        System.out.println("----获取系统当前时间----");        System.out.println("系统当前时间 = " + System.currentTimeMillis());    }    /**     * 通过Date类获取当前日期和当前时间     * date.toString()把日期转换为dow mon dd hh:mm:ss zzz yyyy     */    public void getCurrentDate() {        System.out.println("----获取系统当前日期----");        //创建并初始化一个日期(初始值为当前日期)        Date date = new Date();        System.out.println("现在的日期是 = " + date.toString());        System.out.println("自1970年1月1日0时0分0秒开始至今所经历的毫秒数 = " + date.getTime());    }}

运行结果:

----获取系统当前时间----系统当前时间 = 1196413077278----获取系统当前日期----现在的日期是 = Fri Nov 30 16:57:57 CST 2007自1970年1月1日0时0分0秒开始至今所经历的毫秒数 = 1196413077278Process finished with exit code 0

2、DateFormat 和 SimpleDateFormat

  DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并分析日期或时间。日期/时间格式化子类(如 SimpleDateFormat)允许进行格式化(也就是日期 -> 文本)、

分析(文本-> 日期)和标准化。将日期表示为 Date 对象,或者表示为从 GMT(格林尼治标准时间)1970 年,1 月 1 日 00:00:00 这一刻开始的毫秒数。

Date date = new Date();SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getInstance();SimpleDateFormat sdf1 = (SimpleDateFormat) DateFormat.getTimeInstance();SimpleDateFormat sdf2 = (SimpleDateFormat) DateFormat.getDateTimeInstance();System.out.println("第一种:" + sdf.format(date));System.out.println("第二种:" + sdf1.format(date));System.out.println("第三种:" + sdf2.format(date));

输出的结果:

第一种:16-5-27 下午11:51第二种:23:51:29第三种:2016-5-27 23:51:29

  SimpleDateFormat 是一个以与语言环境相关的方式来格式化和分析日期的具体类。它允许进行格式化(日期 -> 文本)、分析(文本 -> 日期)和规范化。

SimpleDateFormat 使得可以选择任何用户定义的日期-时间格式的模式。但是,仍然建议通过 DateFormat 中的 getTimeInstance、getDateInstance 或 getDateTimeInstance 来新的创建日期-时间格式化程序。

日期和时间模式

日期和时间格式由日期和时间模式 字符串指定。在日期和时间模式字符串中,未加引号的字母 'A' 到 'Z' 和 'a' 到 'z' 被解释为模式字母,用来表示日期或时间字符串元素。文本可以使用单引号 (') 引起来,以免进行解释。
"''" 表示单引号。所有其他字符均不解释;只是在格式化时将它们简单复制到输出字符串,或者在分析时与输入字符串进行匹配。
定义了以下模式字母(所有其他字符 'A' 到 'Z' 和 'a' 到 'z' 都被保留):
public class TestSimpleDateFormat {    public static void main(String args[]) throws ParseException {        TestSimpleDateFormat test = new TestSimpleDateFormat();        test.testDateFormat();    }    public void testDateFormat() throws ParseException {        //创建日期        Date date = new Date();        //创建不同的日期格式        DateFormat df1 = DateFormat.getInstance();        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss EE");        DateFormat df3 = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA);     //产生一个指定国家指定长度的日期格式,长度不同,显示的日期完整性也不同        DateFormat df4 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒 EE", Locale.CHINA);        DateFormat df5 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss EEEEEE", Locale.US);        DateFormat df6 = new SimpleDateFormat("yyyy-MM-dd");        DateFormat df7 = new SimpleDateFormat("yyyy年MM月dd日");        //将日期按照不同格式进行输出        System.out.println("-------将日期按照不同格式进行输出------");        System.out.println("按照Java默认的日期格式,默认的区域                      : " + df1.format(date));        System.out.println("按照指定格式 yyyy-MM-dd hh:mm:ss EE ,系统默认区域      :" + df2.format(date));        System.out.println("按照日期的FULL模式,区域设置为中文                      : " + df3.format(date));        System.out.println("按照指定格式 yyyy年MM月dd日 hh时mm分ss秒 EE ,区域为中文 : " + df4.format(date));        System.out.println("按照指定格式 yyyy-MM-dd hh:mm:ss EE ,区域为美国        : " + df5.format(date));        System.out.println("按照指定格式 yyyy-MM-dd ,系统默认区域                  : " + df6.format(date));        //将符合该格式的字符串转换为日期,若格式不相配,则会出错        Date date1 = df1.parse("07-11-30 下午2:32");        Date date2 = df2.parse("2007-11-30 02:51:07 星期五");        Date date3 = df3.parse("2007年11月30日 星期五");        Date date4 = df4.parse("2007年11月30日 02时51分18秒 星期五");        Date date5 = df5.parse("2007-11-30 02:51:18 Friday");        Date date6 = df6.parse("2007-11-30");        System.out.println("-------输出将字符串转换为日期的结果------");        System.out.println(date1);        System.out.println(date2);        System.out.println(date3);        System.out.println(date4);        System.out.println(date5);        System.out.println(date6);    }}

运行结果:

-------将日期按照不同格式进行输出------按照Java默认的日期格式,默认的区域                      : 16-5-28 上午12:53按照指定格式 yyyy-MM-dd hh:mm:ss EE ,系统默认区域      :2016-05-28 12:53:31 星期六按照日期的FULL模式,区域设置为中文                      : 2016年5月28日 星期六按照指定格式 yyyy年MM月dd日 hh时mm分ss秒 EE ,区域为中文 : 2016年05月28日 12时53分31秒 星期六按照指定格式 yyyy-MM-dd hh:mm:ss EE ,区域为美国        : 2016-05-28 12:53:31 Saturday按照指定格式 yyyy-MM-dd ,系统默认区域                  : 2016-05-28-------输出将字符串转换为日期的结果------Fri Nov 30 14:32:00 CST 2007Fri Nov 30 02:51:07 CST 2007Fri Nov 30 00:00:00 CST 2007Fri Nov 30 02:51:18 CST 2007Fri Nov 30 02:51:18 CST 2007Fri Nov 30 00:00:00 CST 2007

3、Calendar

  Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。

常用方法:
  • void add(int field, int amount):field是字段值,amount是偏移量。根据日历的规则,为给定的日历字段添加或减去指定的时间量。
  • int get(int field): 返回给定日历字段的值。 
  • void set(int field, int value):将给定的日历字段设置为给定值。(注:api中有大量的重载)
  • static Calendar getInstance():使用默认时区和语言环境获得一个日历。(注意返回值是类类型的)

Calendar中些陷阱,很容易掉下去:

  • Calendar的星期是从周日开始的,常量值为0。
  • Calendar的月份是从一月开始的,常量值为0。
  • Calendar的每个月的第一天值为1。
public class TestCalendar {    public static void main(String args[]) {        TestCalendar testCalendar = new TestCalendar();        testCalendar.testCalendar();    }    public void testCalendar() {        //创建Calendar的方式        Calendar now1 = Calendar.getInstance();        Calendar now2 = new GregorianCalendar();        Calendar now3 = new GregorianCalendar(2007, 10, 30);        Calendar now4 = new GregorianCalendar(2007, 10, 30, 15, 55);      //陷阱:Calendar的月份是0~11        Calendar now5 = new GregorianCalendar(2007, 10, 30, 15, 55, 44);        Calendar now6 = new GregorianCalendar(Locale.US);        Calendar now7 = new GregorianCalendar(TimeZone.getTimeZone("GMT-8:00"));        //通过日期和毫秒数设置Calendar        now2.setTime(new Date());        System.out.println(now2);        now2.setTimeInMillis(new Date().getTime());        System.out.println(now2);        //定义日期的中文输出格式,并输出日期        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒 E", Locale.CHINA);        System.out.println("获取日期中文格式化化输出:" + df.format(now5.getTime()));        System.out.println();        System.out.println("--------通过Calendar获取日期中年月日等相关信息--------");        System.out.println("获取年:" + now5.get(Calendar.YEAR));        System.out.println("获取月(月份是从0开始的):" + now5.get(Calendar.MONTH));        System.out.println("获取日:" + now5.get(Calendar.DAY_OF_MONTH));        System.out.println("获取时:" + now5.get(Calendar.HOUR));        System.out.println("获取分:" + now5.get(Calendar.MINUTE));        System.out.println("获取秒:" + now5.get(Calendar.SECOND));        System.out.println("获取上午、下午:" + now5.get(Calendar.AM_PM));        System.out.println("获取星期数值(星期是从周日开始的):" + now5.get(Calendar.DAY_OF_WEEK));        System.out.println();        System.out.println("---------通用星期中文化转换---------");        String dayOfWeek[] = {"", "日", "一", "二", "三", "四", "五", "六"};        System.out.println("now5对象的星期是:" + dayOfWeek[now5.get(Calendar.DAY_OF_WEEK)]);        System.out.println();        System.out.println("---------通用月份中文化转换---------");        String months[] = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};        System.out.println("now5对象的月份是: " + months[now5.get(Calendar.MONTH)]);    }}

运行结果:

java.util.GregorianCalendar[time=1464368078927,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=4,WEEK_OF_YEAR=22,WEEK_OF_MONTH=4,DAY_OF_MONTH=28,DAY_OF_YEAR=149,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=54,SECOND=38,MILLISECOND=927,ZONE_OFFSET=28800000,DST_OFFSET=0]java.util.GregorianCalendar[time=1464368078927,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=4,WEEK_OF_YEAR=22,WEEK_OF_MONTH=4,DAY_OF_MONTH=28,DAY_OF_YEAR=149,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=54,SECOND=38,MILLISECOND=927,ZONE_OFFSET=28800000,DST_OFFSET=0]获取日期中文格式化化输出:2007年11月30日 03时55分44秒 星期五--------通过Calendar获取日期中年月日等相关信息--------获取年:2007获取月(月份是从0开始的):10获取日:30获取时:3获取分:55获取秒:44获取上午、下午:1获取星期数值(星期是从周日开始的):6---------通用星期中文化转换---------now5对象的星期是:五---------通用月份中文化转换---------now5对象的月份是: 十一月

4、GregorianCalendar

  GregorianCalendar 是 Calendar 的一个具体子类,提供了世界上大多数国家使用的标准日历系统。

GregorianCalendar 是一种混合日历,在单一间断性的支持下同时支持儒略历和格里高利历系统,在默认情况下,它对应格里高利日历创立时的格里高利历日期(某些国家是在 1582 年 10 月 15 日创立,

在其他国家要晚一些)。可由调用方通过调用 setGregorianChange() 来更改起始日期。
 

5、日期时间实例

a) 使用Calendar得到当前月的所有格式数据集合
/** * 获取当前月中所有天数集合 * @return */private static List
getMonthMaxDay() { List
dateList = new ArrayList
(); Calendar calendar = Calendar.getInstance(); int year = calendar.get(calendar.YEAR); int month = calendar.get(calendar.MONTH); int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 1; i <= maxDay; i++) { String day = year + "-" + (month + 1) + "-" + i; dateList.add(day); } return dateList;}

输出结果:

2016-5-12016-5-22016-5-3......2016-5-31

b) 使用Calendar和date来获取当前往后推30天的所有数据集合

/** * 今天往后推30天的所有天数 */private static List
getBack30Days() { List
days = new ArrayList
(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd EE"); Date date = new Date(); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); // 添加当前时间 days.add(sdf.format(date)); for (int i = 0; i < 30; i++) { //把日期往后增加一天.整数往后推,负数往前移动 calendar.add(calendar.DATE,1); Date time = calendar.getTime(); days.add(sdf.format(time)); } return days;}

输出结果:

2016-05-28 星期六2016-05-29 星期日2016-05-30 星期一......2016-06-27 星期一

1.5、Math类

java.lang.Math 类包含的方法进行基本的数字操作,如基本的指数,对数,平方根和三角函数等。

1、常用方法:

  • static 基本数据类型 abs(基本数据类型 a):返回 基本数据类型 值的绝对值。
  • static double acos(double a):返回一个值的反余弦;返回的角度范围在 0.0 到 pi 之间。
  • static double asin(double a):返回一个值的反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。
  • static double atan(double a):返回一个值的反正切;返回的角度范围在 -pi/2 到 pi/2 之间。
  • static double ceil(double a):返回大于指定数据的最小整数。
  • static double floor(double a):返回指定数据的最大整数。
  • static long or int round(double or float a): 返回最接近参数的 long or int 。(四舍五入)
  • static double pow(double a, double b):返回第一个参数的第二个参数次幂的值。(a的b次方)
  • static double random():返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。(伪随机数,必须掌握),取随机数也可以用Random类创建Random对象调用nextInt()等方法。
  • static 数据类型 max(数据类型 a, 数据类型 b):返回两个数中较大的一个。
  • static 数据类型 min(数据类型 a, 数据类型 b):返回两个数中较小的一个。

  详情参见java api:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

2、实例解析

public class MathDemo {    public static void main(String args[]){        /**         * abs求绝对值         */        System.out.println(Math.abs(-10.4));    //10.4        System.out.println(Math.abs(10.1));        //10.1                /**         * ceil天花板的意思,就是返回大的值,注意一些特殊值         */        System.out.println(Math.ceil(-10.1));    //-10.0        System.out.println(Math.ceil(10.7));    //11.0        System.out.println(Math.ceil(-0.7));    //-0.0        System.out.println(Math.ceil(0.0));        //0.0        System.out.println(Math.ceil(-0.0));    //-0.0                /**         * floor地板的意思,就是返回小的值         */        System.out.println(Math.floor(-10.1));    //-11.0        System.out.println(Math.floor(10.7));    //10.0        System.out.println(Math.floor(-0.7));    //-1.0        System.out.println(Math.floor(0.0));    //0.0        System.out.println(Math.floor(-0.0));    //-0.0                /**         * max 两个中返回大的值,min和它相反,就不举例了         */        System.out.println(Math.max(-10.1, -10));    //-10.0        System.out.println(Math.max(10.7, 10));        //10.7        System.out.println(Math.max(0.0, -0.0));    //0.0                /**         * random 取得一个大于或者等于0.0小于不等于1.0的随机数         */        System.out.println(Math.random());    //0.08417657924317234        System.out.println(Math.random());    //0.43527904004403717                /**         * rint 四舍五入,返回double值         * 注意.5的时候会取偶数         */        System.out.println(Math.rint(10.1));    //10.0        System.out.println(Math.rint(10.7));    //11.0        System.out.println(Math.rint(11.5));    //12.0        System.out.println(Math.rint(10.5));    //10.0        System.out.println(Math.rint(10.51));    //11.0        System.out.println(Math.rint(-10.5));    //-10.0        System.out.println(Math.rint(-11.5));    //-12.0        System.out.println(Math.rint(-10.51));    //-11.0        System.out.println(Math.rint(-10.6));    //-11.0        System.out.println(Math.rint(-10.2));    //-10.0                /**         * round 四舍五入,float时返回int值,double时返回long值         */        System.out.println(Math.round(10.1));    //10        System.out.println(Math.round(10.7));    //11        System.out.println(Math.round(10.5));    //11        System.out.println(Math.round(10.51));    //11        System.out.println(Math.round(-10.5));    //-10        System.out.println(Math.round(-10.51));    //-11        System.out.println(Math.round(-10.6));    //-11        System.out.println(Math.round(-10.2));    //-10    }}

注:Java的Math下的Random类是和系统时间有关的,较短时间内可能得到相同值,所以推荐使用Math的random()方法。

 

转载于:https://www.cnblogs.com/pengjingya/p/5525322.html

你可能感兴趣的文章
jQuery方式事件冒泡的2个方法
查看>>
[教程]MongoDB 从入门到进阶 (aggregation数据库状态)
查看>>
查看linux是ubuntu还是centos
查看>>
H5——表单验证新特性,注册模态框!
查看>>
sintimental analysis
查看>>
Java并发包--线程池原理
查看>>
获取网页数据的例子
查看>>
洛谷P3265 装备购买
查看>>
Database | SQL
查看>>
struts2的配置文件
查看>>
匆匆三月,归来已非少年
查看>>
php加载mysql
查看>>
HDOJ 2098 分拆素数和(筛选法求素数)
查看>>
Java Web整合开发(21) -- 宏观把握Hibernate
查看>>
JSP第5次测试---测试分析
查看>>
泛型介绍
查看>>
单例设计模式(这一篇足够了)
查看>>
C#语法-虚方法详解 Virtual 虚函数
查看>>
UIPickerView 的 多重选择
查看>>
从底层了解ASP.NET体系结构
查看>>