Java常用API类之Math System tostring用法详解
1.注意(类名不能与math重名,否则可能报错误)
1.math:可以直接拿来用的接口类
Math.abs(-90);返回参数的绝对值
Math.max(60,98)返回参数的最大值
Math.random()*100随机函数:随机输出一个数
等等
public static void main(String[] args){ int a = 1300, b = 1000; System.out.println(Math.abs(-90)); System.out.println(Math.max(60,98)); System.out.println(Math.random()*100); System.out.println(Math.ceil(b)); }}
结果:
2.System的API:
System.exit(0);// 在这个API下的往下内容java代码会停止执行
System.currentTimeMillis();//返回与从1970年到现在的毫秒数
public class tyue { public static void main(String[] args){ System.out.println("开始!"); //System.exit(0); System.out.println("结束!"); System.out.println(System.currentTimeMillis()*1/1000/60/60/24/365+"年"); Long begin=System.currentTimeMillis(); for(int i=0;i<=1000;i++) { System.out.println(i); } Long end=System.currentTimeMillis(); System.out.println("共耗时"+(end-begin)+"多少毫秒"); }}
3.object类中的:tostring()方法
如果:直接怎么写:
zi sc=new zi();
System.out.println(sc);
System.out.println(sc.toString());
打印出:zi@1b6d3586;这样不易观察
可以通过:
println
Ctrl+B: 打开方法的声明处点击print(这个System.out.println(sc))
可以看到:
public class zi extends Object{ int a=30; String we="我是说"; public void show(){ System.out.println("我是说"); } }
public static void main(String[] args){ zi sc=new zi(); System.out.println(sc); System.out.println(sc.toString()); //public void println(Object x) { // String s = String.valueOf(x); // synchronized (this) { // print(s); // newLine(); // } // } //方法的声明处 ctrl+b /*public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } */ sc.show(); }
可以: Alt+insert(插入键(Insert key,缩写INS)是电脑键盘的一个键)
选择toString()
然后全选确认,自动生成
就Ok了
public class zi extends Object{ int a=30; String we="我是说"; @Override public String toString() { return "zi{" + "a=" + a + ", we='" + we + '\'' + '}'; } public void show(){ System.out.println("我是说"); } }
重新编译:得到
zi{a=30, we=‘我是说'} //明显
到此这篇关于Java常用API类之Math System tostring用法详解的文章就介绍到这了,更多相关Java 常用API类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot与Spring中数据缓存Cache超详细讲解
我们知道内存读取速度远大于硬盘读取速度,当需要重复获取相同数据时,一次一次的请求数据库或者远程服务,导致在数据库查询或者远程方法调用上小号大量的时间,最终导致程序性能降低,这就是数据缓存要解决的问题,学过计算机组成原理或者操作系统的同学们应该比较熟悉2022-10-10Mybatis中foreach标签带来的空格\换行\回车问题及解决方案
这篇文章主要介绍了解决Mybatis中foreach标签带来的空格,换行,回车问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-04-04详解Lombok安装及Spring Boot集成Lombok
这篇文章主要介绍了详解Lombok安装及Spring Boot集成Lombok,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2019-03-03
最新评论