package com.shushine.framework.第七章Java标准类库;

/**
 *
 * <p>
 * 描述该类情况 {@link 代表跟谁有关系}
 * </p>
 *
 * @author 王超
 * @since 1.0
 * @date 2016年10月21日 上午11:27:48
 * @see 新建|修改|放弃
 * @see com.shushine.framework.第七章Java标准类库.SystemAndRuntimeAndMath
 *      System类包含了许多静态方法和变量,有:标准的输入(in)、输出(out)和错误输出(error),
 *      对外部定义的属性和环境变量的访问,加载文件和库的方法,还有快速复制数组的一部分的实用方法
 *      Runtime类封装了运行时的环境。用户一般不实例化一个Runtime对象,但是可以通过调用静态方法Runtime.getRuntime()
 *      从而获得当前Runtime的对象。 一旦获得了当前对象的引用,就可以调用几个控制Java虚拟机的状态和行为的方法
 *      Math类用于数学计算,包含了所有用于几何学、三角函数计算以及几种一般用途的方法的浮点函数,这些函数都被定义成静态方法
 */

public class SystemAndRuntimeAndMath {
    public static void main(String[] args) {
        long start, end, sum = 0, times = 100000000;
        System.out.println("执行" + times + "次循环所需要的时间");
        // 计时器开始
        start = System.nanoTime();
        for (int i = 0; i < times; i++) {
            sum = sum + i * i;
        }
        // 计时器结束
        end = System.nanoTime();
        System.out.println("需要的时间是:" + (end - start) + "纳秒");
        // 复制数组
        byte a[] = { 65, 64, 68, 69, 55, 70 };
        byte b[] = { 8, 88, 8, 8, 88, 88, 88, 88 };
        // 数组转换相对应的字符串
        System.out.println("a=" + new String(a));
        System.out.println("b=" + new String(b));
        // 把a数组复制给b数组 从a[0]开始到a[a.length-1] 赋给b[0]到b[a.length-1]
        System.arraycopy(a, 0, b, 0, a.length);
        System.out.println("b=" + new String(b));
        System.arraycopy(a, 0, a, 1, a.length - 1);
        System.arraycopy(b, 1, b, 0, b.length - 1);
        System.out.println("a=" + new String(a));
        System.out.println("b=" + new String(b));
        // 访问一些JVM的属性
        System.out.println(System.getProperty("java.class.path"));
        System.out.println(System.getProperty("user.dir"));
        // 计算内存使用量
        // 获取Runtime的对象
        Runtime r = Runtime.getRuntime();
        long mem1, mem2;
        Integer someints[] = new Integer[1000];
        // 总可用内存,不是计算机的总内存,是JVM中可用的总内存
        System.out.println("总内存是:" + r.totalMemory());
        // 空闲内存
        mem1 = r.freeMemory();
        System.out.println("初始内存是:" + mem1);
        // 垃圾回收方法
        r.gc();
        // 垃圾回收后的内存
        mem1 = r.freeMemory();
        System.out.println("垃圾回收后的内存是:" + mem1);
        // 进行内存分配
        for (int i = 0; i < someints.length; i++) {
            someints[i] = new Integer(i);
        }
        // 分配内存后可用内存空间
        mem2 = r.freeMemory();
        System.out.println("分配内存后的可用内存是:" + mem2);
        // 数组占用内存
        System.out.println("数组占用内存是:" + (mem1 - mem2));
        // 释放对象
        for (int i = 0; i < someints.length; i++) {
            someints[i] = null;
        }
        r.gc();
        mem2 = r.freeMemory();
        System.out.println("垃圾回收后的内存是:" + mem2);
        // Math类中的一些方法应用举例
        double d1 = 3.1415926;
        System.out.println("ceil函数:" + Math.ceil(d1) + " Round函数:" + Math.round(d1) + " floor函数:" + Math.floor(d1)
                + " exp函数:" + Math.exp(d1) + " pow函数:" + Math.pow(Math.E, d1) + " random函数:" + Math.random());

    }
}

System & Runtime &Math的更多相关文章

  1. 工具类System,Runtime,Math,Date,Calendar

    API--- java.lang.System: 属性和行为都是静态的. long currentTimeMillis(); // 返回当前时间毫秒值   exit();  // 退出虚拟机 Prop ...

  2. VS2015 出现 .NETSystem.Runtime.Remoting.RemotingException: TCP 错误

    错误内容: 界面显示内容为: .NET�������������System.Runtime.Remoting.RemotingException: TCP 淇¢亾鍗忚鍐茬獊: 搴斾负鎶ュご銆� 鍦 ...

  3. 找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”的解决办法

    找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”.的解决办法站点发布后部署到了两 ...

  4. Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute'

    [TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from as ...

  5. 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则

    在.NET 4.0下使用最新版本的log4Net 1.2.10,会遇到下面这样的错误: 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObject ...

  6. IIS提示“异常详细信息: System.Runtime.InteropServices.ExternalException: 无法执行程序”

    先来看错误提示: 无法执行程序.所执行的命令为 "C:/Windows/Microsoft.NET/Framework/v3.5/csc.exe" /noconfig /fullp ...

  7. System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800AC472

    更新至服务器后运行出错: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800AC472 解决方法 注册 ...

  8. System.Runtime.InteropServices.COMException (0x800706BA) 解决方法

    提示“操作失败:无法获取MAC地址.”错误的解决方法. .NET 获取 MAC地址可能会遇到   System.Runtime.InteropServices.COMException (0x8007 ...

  9. 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttribute..ctor” 解决方案

    我自己使用的解决方法 错误产生环境及非完美解决办法 错误提示:缺少编译器要求的成员"System.Runtime.CompilerServices.ExtensionAttribute..c ...

随机推荐

  1. Unity UGUI 裁剪TTF字体

    BitBucket上找到了一个perl工程,font-optimizer.拉取代码到本地.为了运行它,还需要装Perl解释器,可以在Perl的官网上下载ActivePerl.装好ActivePerl后 ...

  2. ACM Binary String Match

    #include <stdio.h> #include <string.h> #include <stdlib.h> void SubString(char sub ...

  3. C# Array

    一.声明数组时,方括号[]必须跟在类型后面,而不是标识符后面 int[] table;  //而不是 int table[]; 二.数组的大小不是其类型的一部分 int[] numbers; numb ...

  4. Rearrange a string so that all same characters become d distance away

    Given a string and a positive integer d. Some characters may be repeated in the given string. Rearra ...

  5. <script>元素的位置

    脚本元素会组织下载网页内容,浏览器可以同时下载多个组件,但一旦遇到一个外部脚本文本后,浏览器会停止进一步下载,知道这个脚本文件下载,解析并执行完毕.这会严重影响网页载入的总时间,特别是在网页在入时会发 ...

  6. 获取 Cookie

    /// <summary>    /// 获取WPF url 地址中的Cookies    /// </summary>    public partial class Coo ...

  7. 3D touch的 使用心得

    一.设置图标touch 快捷进入 1.静态标签 静态标签是我们在项目的配置plist文件中配置的标签,在用户安装程序后就可以使用,并且排序会在动态标签的前面. 我们先来看静态标签的配置: 首先,在in ...

  8. ls 只显示目录

    只显示目录: ls -d */ 在实际应用中,我们有时需要仅列出目录,下面是 4 种不同的方法. 1. 利用 ls 命令的 -d 选项: $ ls -d */ 2. 利用 ls 命令的 -F 选项: ...

  9. tesseract ocr文字识别Android实例程序和训练工具全部源代码

    tesseract ocr是一个开源的文字识别引擎,Android系统中也可以使用.可以识别50多种语言,通过自己训练识别库的方式,可以大大提高识别的准确率. 为了节省大家的学习时间,现将自己近期的学 ...

  10. LUA OOP编程实现方法

    lua原生不支持OOP特性 确实如此, 同时可以采用其它lua代码的方式实现OOP的特性. OOP四大特性 抽象 封装 继承 多态 http://www.cnblogs.com/xiaosongluf ...