System & Runtime &Math

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());

}
}

时间: 2024-07-28 20:27:31

System & Runtime &Math的相关文章

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

API--- java.lang.System: 属性和行为都是静态的. long currentTimeMillis(); // 返回当前时间毫秒值   exit();  // 退出虚拟机 Properties getProperties() ;  // 获取当前系统的属性信息 Properties prop = System.getProperties(); //获取系统的属性信息,并将这些信息存储到Properties集合中. System.setProperty("myname"

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

namespace System.Runtime.CompilerServices { public class ExtensionAttribute : Attribute { } } 网上找的其他方法错误产生环境及非完美解决办法错误提示:缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttribute..ctor”这个错误真的非常诡异,因为双击这个错误的时候无法定位到出错的地方.而且这个错误实在是非常不明确.其实,产生这个错误的人大部

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

在.NET 4.0下使用最新版本的log4Net 1.2.10,会遇到下面这样的错误: 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则.重写方法的安全可访问性必须与所重写方法的安全可访问性 匹配. 说明: 执行当

错误 1 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttrib

错误 1 缺少编译器要求的成员"System.Runtime.CompilerServices.ExtensionAttrib 删除Newtonsoft.Json.dll 引用 ,再重新引用即可. 原文:http://bbs.csdn.net/topics/360254920 错误 1 缺少编译器要求的成员"System.Runtime.CompilerServices.ExtensionAttrib,布布扣,bubuko.com 错误 1 缺少编译器要求的成员"System

C# System.Runtime.Caching使用

System.Runtime.Caching命名空间是.NET 4.0新增的,目的是将以前的.NET 版本中的System.Web.Caching单独提取出来,独立使用,这样web和其他.NET程序如WPF都可以使用. System.Runtime.Caching包含缓存类和监视类,包括文件.数据库.缓存等的监视,与以前在System.Web.Caching中的一样,但重新包装. 可以预见在以后的版本中,System.Web.Caching命名空间会被标记为Obsolete(过时),或者重写为包

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

错误内容: 界面显示内容为: .NET?????????????System.Runtime.Remoting.RemotingException: TCP 淇¢亾鍗忚鍐茬獊: 搴斾负鎶ュご銆? 鍦? System.Runtime.Remoting.Channels.Tcp.TcpSocketHandler.ReadAndMatchPreamble() 鍦? System.Runtime.Remoting.Channels.Tcp.TcpSocketHandler.ReadVersionAnd

System.Runtime.InteropServices.COMException (0x8004E00F): COM+ 无法与 Microsoft 分布式事务协调程序交谈 (异常来自 HRESU

错误信息: System.Runtime.InteropServices.COMException (0x8004E00F): COM+ 无法与 Microsoft 分布式事务协调程序交谈 (异常来自 HRESULT:0x8004E00F) 解决方案: 1.删除注册表中的键:     HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/MSDTC HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MSDTC HKEY

找不到System.Runtime.Serialization.Json的解决方案

System.ServiceModel System.ServiceModel.Web System.Runtime.Serialization 三者均要添加引用

HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cache.       我们再用.NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的实现: HttpContext.Cache和HttpRuntime.Cache实现    //System.Web.HttpContext.Cache属性实现