当前位置: 首页 > news >正文

东莞百度网站快速排名湘潭建网站

东莞百度网站快速排名,湘潭建网站,thinkphp网站开发实例教程,广告网站设计公司API#xff08;全称 Application Programming Interface#xff1a;应用程序编程接口#xff09; API就是Java帮我们已经写好的一些程序#xff0c;如类、方法等#xff0c;可以直接拿过来用 JDK8 API文档#xff1a;Java Platform SE 8 一. Object Object类的作用 Ob…API全称 Application Programming Interface应用程序编程接口 API就是Java帮我们已经写好的一些程序如类、方法等可以直接拿过来用 JDK8 API文档Java Platform SE 8 一. Object Object类的作用 Object类是Java中所有类的祖宗类因此Java中所有类的对象都可以直接使用Object类中提供的一些方法 Object类的常见方法 方法名说明public String toString()返回对象的字符串表示形式public boolean equals(Object o)判断两个对象是否相等protected Object clone()对象克隆 toString存在的意义toString()方法存在的意义就是为了被子类重写以便返回对象具体的内容 equals存在的意义直接比较两个对象的地址是否相同完全可以用代替equalsequals存在的意义就是为了被子类重写以便子类自己来定制比较规则比如比较对象内容 clone()方法 当某个对象调用这个方法时这个方法会复制一个一模一样的新对象返回 clone()方法分为浅克隆和深克隆 浅克隆拷贝出的新对象与原对象中的数据一模一样引用类型拷贝的只是地址 深克隆对象中基本类型的数据直接拷贝字符串数据拷贝的还是地址对象中还包含的其他对象不会拷贝地址会创建新对象  //demo public class demo {public static void main(String[] args) throws CloneNotSupportedException {Student s1 new Student(小美,20,new double[]{99,100,90});System.out.println(s1); //默认输出s1就是输出s1.toString()System.out.println(s1.toString()); //不重写toString方法的话返回的是s的地址Student s2 new Student(小美,20,new double[]{99,100,90}); // //不重写equals方法 // System.out.println(s2.equals(s1)); // false equals方法判断两个对象的地址是否相等 // System.out.println(s2 s1); // false 比较两个对象地址是否一样可以直接用 // Student s3 s1; // System.out.println(s3.equals(s1)); // true equals方法判断两个对象的地址是否相等System.out.println(s2.equals(s1)); //true 重写了equals方法//s1.clone(); //因为clone()方法是protected的因此不能直接使用需要重写clone()方法Student s4 (Student) s1.clone(); //重写后System.out.println(s4.getName()); //小美System.out.println(s4.getAge()); //20System.out.println(s4.getScore());System.out.println(s4 s1); //false克隆后s4和s1的地址不一样克隆只是复制内容System.out.println(s4.equals(s1)); //true重写后的equals方法比较内容是否相同} }//Student //Cloneable是一个标记接口实现Cloneable接口才能使用clone()方法 public class Student implements Cloneable { //extends Objectprivate String name;private int age;private double[] score; //学生成绩数组如语数英三门成绩public Student() {}public Student(String name, int age, double[] score) {this.name name;this.age age;this.score score;}Overrideprotected Object clone() throws CloneNotSupportedException {//通过super调用父类Object中的clone方法//浅克隆//return super.clone(); //等同于 Student s (Student) super.clone(); return s;//深克隆Student s (Student) super.clone();s.score s.score.clone(); //对象中基本类型的数据直接拷贝字符串数据拷贝的还是地址对象中还包含的其他对象不会拷贝地址会创建新对象 return s;}//重写toString方法得到对象内容自动生成Overridepublic String toString() {return Student{ name name \ , age age , score Arrays.toString(score) };}//重写equals方法比较两个对象的内容是否一致自动生成//比较者s2 this//被比较者s1 oOverridepublic boolean equals(Object o) {//判断两个对象地址是否一致一致直接返回trueif (this o) return true;//判断o是否是nullo是null直接返回false//判断比较者和被比较者的类型是否一致不一致直接返回false//这里的getClass()相当于this.getClass()if (o null || getClass() ! o.getClass()) return false;//因为接收的时候是Object类型这里需要进行强转为StudentStudent student (Student) o;return age student.age Objects.equals(name, student.name) Arrays.equals(score, student.score);}Overridepublic int hashCode() {return Objects.hash(name, age);}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public double[] getScore() {return score;}public void setScore(double[] score) {this.score score;} } 二. Objects  Objects Objects是一个工具类提供了很多操作对象的静态方法给我们使用 静态方法可以直接用类名.静态方法 Objects类的常见方法 方法名说明public static boolean equals(Object a,Object b)先做非空判断再比较两个对象public static boolean isNull(Object obj)判断对象是否为null为null返回true反之返回falsepublic static boolean nonNull(Object obj)判断对象是否不为null不为null返回true反之返回false //demo public class demo {public static void main(String[] args) {String s1 null;String s2 zhang;//System.out.println(s1.equals(s2)); //如果s1是null这种比较方法会报错空指针异常 NullPointerExceptionSystem.out.println(Objects.equals(s1, s2)); //更安全System.out.println(Objects.isNull(s1)); //trueSystem.out.println(s1 null); //trueSystem.out.println(Objects.isNull(s2)); //falseSystem.out.println(s2 null); //falseSystem.out.println(Objects.nonNull(s1)); //falseSystem.out.println(Objects.nonNull(s2)); //true} } 三. 包装类 Java的思想是万物皆对象但是Java基本类型的数据byte  short  int  long  char  float  double  boolean不是对象因此引入了包装类 包装类 包装类就是把基本类型的数据包装成对象 基本数据类型对应的包装类引用数据类型byteByteshortShortintIntegerlongLongcharCharacterfloatFloatdoubleDoublebooleanBoolean 包装类的其他常见操作 · 可以把基本类型的数据转换成字符串类型toString() · 可以把字符串类型的数值转换成数值本身对应的数据类型valueOf()更推荐 //demo public class demo {public static void main(String[] args) {//Integer a new Integer(12); //过时方案Integer a Integer.valueOf(12);System.out.println(a);//自动装箱机制可以自动把基本类型的数据转换成对象Integer b 12;//自动拆箱机制可以自动把包装类型的对象转换成对应的基本数据类型int c b;//泛型和集合不支持基本数据类型只能支持引用数据类型//ArrayListint list new ArrayList(); //报错ArrayListInteger list new ArrayList();list.add(12); //自动装箱int r list.get(0); //自动拆箱System.out.println();//1.把基本类型的数据转换成字符串Integer x 23; //自动装箱String s1 Integer.toString(x); //自动拆箱 Integer.toString(x)把x转换成字符串类型System.out.println(s1 1); //231String s2 x.toString(); //继承Object类toString()也是转换成字符串System.out.println(s2 1); //231String s3 x ; //这样也可以转换成字符串System.out.println(s3 1); //231//2.把字符串类型的数值转换成对应的基本类型//字符串类型的数值!!!不是数值没法转换//不是对应的数据类型也没办法转换字符串中的数值是浮点型不能用IntegerString m1 29;//int z1 Integer.parseInt(m1); //字符串转换成对应的基本类型 parseInt()不推荐使用int z1 Integer.valueOf(m1);System.out.println(z1 1); //30String m2 99.5;//double z2 Double.parseDouble(m2); //字符串转换成对应的基本类型 parseDouble()不推荐使用double z2 Double.valueOf(m2);System.out.println(z2 1); //100.5} } 四. StringBuilder StringBuilder代表可变字符串对象相当于一个容器它里面装的字符串是可以改变的就是用来操作字符串的 好处StringBuilder比String更适合做字符串的修改操作效率会更高代码也会更简洁 构造器说明public StringBuilder()创建一个空白的可变的字符串对象不包含任何内容public StringBuilder(String str)创建一个指定字符串内容的可变字符串对象 方法名称说明public StringBuilder append(任意类型)添加数据并返回StringBuilder对象本身public StringBuilder reverse()将对象的内容反转public int length()返回对象内容长度public String toString()通过toString()就可以实现把StringBuilder转换为String //demo public class demo {public static void main(String[] args) {//StringBuilder sb new StringBuilder(); //sb StringBuilder sb new StringBuilder(wosun);//1.拼接内容sb.append(12); //拼接intsb.append(true); //拼接booleansb.append(13.14); //拼接double/floatsb.append(你好); //拼接String//支持链式编程sb.append(1).append(a).append(5.20).append(false);System.out.println(sb);//2.内容反转sb.reverse(); //内容反转System.out.println(sb);//3.返回字符串长度System.out.println(sb.length());//4.把StringBuilder对象又转换成String类型String s sb.toString();System.out.println(s);System.out.println();//需求拼接100万次abc//String 很很恨很慢 // String rs ; // for (int i 0; i 1000000; i) { // rs abc; // } // System.out.println(rs);//StringBuilder 非常快StringBuilder rs new StringBuilder();for (int i 0; i 1000000; i) {rs.append(abc);}System.out.println(rs);} } 注意如果操作字符串较少或者不需要操作以及定义字符串变量还是建议使用String如果需要频繁的拼接、修改等建议使用StringBuilder 案例返回任意整型数组的内容 //demo public class demo {public static void main(String[] args) {int[] arr1 new int[]{1,2,3,4,5};System.out.println(method(arr1));int[] arr2 new int[5];System.out.println(method(arr2));int[] arr3 null;System.out.println(method(arr3));}//一般来说返回值是String不会是StringBuilderpublic static String method(int[] arr){//要先进行非空校验判断arr是否为nullif(arr null){return null; //数组对象不存在}//数组对象存在StringBuilder sb new StringBuilder();sb.append([);for (int i 0; i arr.length; i) { // if(i0){ // sb.append([); // }sb.append(arr[i]);if(i!arr.length-1){sb.append(,);}else{sb.append(]);}}return sb.toString();} } 五. StringBuffer StringBuffer的用法和StringBuilder是一模一样的但StringBuilder是线程不安全的StringBuffer是线程安全的 六. StringJoiner StringJoiner是JDK8开始才有的跟StringBuilder一样也是用来操作字符串的也可以看成是一个容器创建之后里面的内容是可变的 好处不仅能提高字符串的操作效率并且在有些场景下使用它操作字符串代码会更简洁 构造器说明public StringJoiner(间隔符号)创建一个StringJoiner对象指定拼接时的间隔符号public StringJoiner(间隔符号,开始符号,结束符号)创建一个StringJoiner对象指定拼接时的间隔符号、开始符号、结束符号 方法名称说明public StringJoiner add(添加de)添加数据并返回对象本身public int length()返回长度字符出现的个数public String toString()返回一个字符串该字符串就是拼接后的结果 //demo public class demo {public static void main(String[] args) {//StringJoiner s new StringJoiner(,); //间隔符StringJoiner s new StringJoiner(,,[,]); //间隔符 开始符 结束符s.add(java1);s.add(java2);s.add(java3);System.out.println(s); //java1,java2,java3int[] arr new int[]{1,2,3,4,5};System.out.println(method(arr));}public static String method(int[] arr){if(arr null){return null;}StringJoiner sj new StringJoiner(,,[,]);for (int i 0; i arr.length; i) {String s arr[i] ; //把数组里的整型数值 转换成 字符串sj.add(s);}return sj.toString();} } 七. Math 代表数学是一个工具类里面提供的都是对数据进行操作的一些静态方法 Math类提供的常见方法 方法名说明public static int abs(int a)获取参数绝对值public static double ceil(double a)向上取整public static double floor(double a)向下取整public static long round(float a)四舍五入public static int max(int a,int b)获取两个int值中的较大值public static double pow(double a,double b)返回a的b次幂的值public static double random()返回值为double的随机值范围[0.0 , 1.0] //demo public class demo {public static void main(String[] args) { // 获取参数绝对值 public static int abs(int a) 获取参数绝对值 // 获取参数绝对值 public static double abs(double a)System.out.println(Math.abs(-12)); //12System.out.println(Math.abs(123)); //123System.out.println(Math.abs(0)); //0System.out.println(Math.abs(-13.14)); //13.14// 向上取整 public static double ceil(double a)System.out.println(Math.ceil(4.3)); //5.0System.out.println(Math.ceil(3)); //3.0// 向下取整 public static double floor(double a)System.out.println(Math.floor(4.99)); //4.0System.out.println(Math.floor(3)); //3.0// 四舍五入 public static long round(float a)System.out.println(Math.round(4.49)); //4System.out.println(Math.round(4.51)); //5// 获取两个int值中的较大值 public static int max(int a,int b) (还可以是浮点型或long型) // 获取两个int值中的较小值 public static int min(int a,int b)System.out.println(Math.max(10,20)); //20System.out.println(Math.min(10,20)); //10System.out.println(Math.min(10.07,10.09)); //10.07// 返回a的b次幂的值 public static double pow(double a,double b)System.out.println(Math.pow(2, 3)); //8.0 // 返回值为double的随机值范围[0.0 , 1.0] public static double random()System.out.println(Math.random()); //随机数 如0.5848377958673234} } 八. System System代表程序所在的系统也是一个工具类 System类提供的常见方法 方法名说明public static void exit(int status)终止当前运行的Java虚拟机public static long currentTimeMillis()返回当前系统的时间毫秒值形式 时间毫秒值指的是从1970年1月1日00:00:00走到此刻的总的毫秒值ms  1s1000ms //demo public class demo {public static void main(String[] args) { // public static void exit(int status)//终止当前运行的Java虚拟机//该参数用作状态代码按照惯例非零状态代码表示异常终止//System.exit(0); //人为终止虚拟机不要使用System.out.println();// public static long currentTimeMillis()//返回当前系统的时间毫秒值形式Long l1 System.currentTimeMillis();System.out.println(l1);for (int i 0; i 10000; i) {System.out.println(i);}Long l2 System.currentTimeMillis();System.out.println((l2-l1)/1000.0 s); //l2-l1是毫秒值//时间差即两次时间中间程序的运行时间可以做程序的性能分析} } 九. Runtime Runtime代表程序所在的运行环境 Runtime是一个单例类 Runtime类提供的常见方法 方法名说明public static Runtime getRuntime()返回与当前Java应用程序关联的运行时对象public void exit(int status)终止当前运行的虚拟机public int availableProcessors()返回Java虚拟机可用的处理器数public long totalMemory()返回Java虚拟机中的内存总量public long freeMemory()返回Java虚拟机中的可用内存public Process exec(String command)启动某个程序并返回代表该程序的对象 //demo public class demo {public static void main(String[] args) throws IOException, InterruptedException {//public static Runtime getRuntime() 返回与当前Java应用程序关联的运行时对象Runtime r Runtime.getRuntime();//public void exit(int status) 终止当前运行的虚拟机该参数用作状态代码按照惯例非零状态代码表示异常终止//r.exit(0); //不要使用//public int availableProcessors() 返回Java虚拟机可用的处理器数System.out.println(r.availableProcessors());//public long totalMemory() 返回Java虚拟机中的内存总量//1024 1Kb 1024 * 1024 1MbSystem.out.println(r.totalMemory()); //返回的是字节System.out.println(r.totalMemory() / 1024.0 Kb);System.out.println(r.totalMemory() / 1024.0 /1024.0 Mb);//public long freeMemory() 返回Java虚拟机中的可用内存System.out.println(r.freeMemory() );System.out.println(r.freeMemory() / 1024.0 /1024.0 MB);//public Process exec(String command) 启动某个程序并返回代表该程序的对象Process p r.exec(); //电脑中QQ程序的地址//r.exec(QQ); //想要这样直接启动QQ需要把QQ配置到环境变量Thread.sleep(5000); //让程序在这里暂停5s再往下走p.destroy(); //销毁即关闭程序} }十. BigDecimal 用来解决浮点型运算时出现结果失真的问题 BigDecimal的常见构造器、常用方法 构造器说明public BigDecimal(double val)  注意不推荐用这个将double转换为BIgDecimalpublic BigDecimal(String val)将String转换为BIgDecimal 方法名说明public static BigDecimal valueOf(double val)转换一个double成BIgDecimalpublic BigDecimal add(BigDecimal b)加法public BigDecimal subtract(BigDecimal b)减法public BigDecimal multiply(BigDecimal b)乘法public BigDecimal divide(BigDecimal b)除法 public BigDecimal divide(另一个BigDecimal对象精确几位舍入模式) public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 除法可以控制精确到小数几位public double doubleValue()将BigDecimal转换为double //demo public class demo {public static void main(String[] args) throws IOException, InterruptedException {System.out.println(0.10.2); //0.30000000000000004 失真System.out.println();//把他们变成字符串封装成BigDecimal对象来运算double a 0.1;double b 0.2; //public BigDecimal(String val) 将String转换为BIgDecimal//BigDecimal a1 new BigDecimal(Double.toString(a));//BigDecimal b1 new BigDecimal(Double.toString(b));//推荐用以下方式把小数转换成字符串再得到BigDecimal对象来使用更简洁BigDecimal a1 BigDecimal.valueOf(a);BigDecimal b1 BigDecimal.valueOf(b);BigDecimal c1 a1.add(b1);BigDecimal c2 a1.subtract(b1);BigDecimal c3 a1.multiply(b1);BigDecimal c4 a1.divide(b1);System.out.println(c1);System.out.println(c2);System.out.println(c3);System.out.println(c4);//除法中如果除不尽不给精确位数会报错BigDecimal x BigDecimal.valueOf(0.1);BigDecimal y BigDecimal.valueOf(0.3);//System.out.println(x.divide(y)); //报错 没有精确值System.out.println(x.divide(y,2, RoundingMode.HALF_UP)); //0.4//不会写代码的时候用ctrlalt空格//public double doubleValue() 将BigDecimal转换为doubledouble m x.doubleValue();System.out.println(m);} }
http://www.pierceye.com/news/530984/

相关文章:

  • 做网站的服务器带宽一般多少游戏开发培训机构
  • 网站设计制作培训微信开放平台文档
  • 私人申请建设网站多少钱html如何建网站
  • 网站怎么在微博推广石家庄模板建站平台
  • 贵阳网站开发方舟网络wordpress静态化链接
  • 如何建设一个公司网站英文网站建设多少钱
  • 国外做水广告网站大全app开发公司查询
  • 苏州商城网站制作免费下载ppt模板的网站有哪些
  • 北京智能网站建设企业wordpress 找源码
  • 无锡网站维护公司wordpress 目录排序
  • 自己搭建的ftp怎么做网站装修公司展厅效果图
  • 做网站手机验证收费吗百度竞价推广是什么工作
  • 电商网站 案例熊掌号怎么域名做网站
  • 做网站怎么改关键词安卓开发软件工具
  • 做SEO公司多给网站wordpress 固定链接 无法访问
  • 潍坊百度网站优化网站建设相关文章
  • 做学术研究的网站怎样建设个人游戏网站
  • dede淘宝客网站网站页面优化简单吗
  • 长春做网站优化的公司赣州做网站公司哪家好
  • 网站开发宝典做网站属于软件开发吗
  • 网站建设要求 优帮云福州模板建站定制网站
  • wordpress本地更换为网站域名jsp网站开发书籍
  • 做一个网站的流程沧州网站建设
  • 山东省城乡住房建设厅网站住房建设部网站监理员
  • 怎么做百度网站验证保健品商城网站模板
  • 丹东市做网站广东做网站的公司
  • 网站收录大全销售推广
  • 网站发展历程东莞企业网站建设制作
  • 厦门市建设局查询保障摇号网站首页做房产网站长
  • 公司网站建设的普遍性长沙建站网