当前位置:网站首页>黑马笔记---常用API
黑马笔记---常用API
2022-06-26 12:37:00 【小夫敲代码】
目录
总结:为什么拼接,反转字符串建议使用StringBuilder?
1.Object
Object类的作用:
1.Object类的方法是一切子类对象都可以直接使用的;
2.一个类要么默认继承了Object类,要么间接继承了Object类,Object类是Java中的祖宗类。
Object类的常用方法
toString存在的意义
父类toString()方法存在的意义就是为了被子类重写,以便返回对象的内容信息,而不是地址信息!
equals存在的意义
父类equals方法存在的意义就是为了被子类重写,以便子类自己来定制比较规则。
下面以具体的代码说明toString和equals方法:
import java.util.Objects;
public class Student { //extends Object{
private String name;
private char sex;
private int age;
public Student() {
}
public Student(String name, char sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
定制相等规则。
两个对象的内容一样就认为是相等的
s1.equals(s2)
比较者:s1 == this
被比较者: s2 ==> o
*/
@Override
public boolean equals(Object o) {
// 1、判断是否是同一个对象比较,如果是返回true。
if (this == o) return true;
// 2、如果o是null返回false 如果o不是学生类型返回false ...Student != ..Pig
if (o == null || this.getClass() != o.getClass()) return false;
// 3、说明o一定是学生类型而且不为null
Student student = (Student) o;
return sex == student.sex && age == student.age && Objects.equals(name, student.name);
}
/**
自己重写equals,自己定制相等规则。
两个对象的内容一样就认为是相等的
s1.equals(s2)
比较者:s1 == this
被比较者: s2 ==> o
*/
/* @Override
public boolean equals(Object o){
// 1、判断o是不是学生类型
if(o instanceof Student){
Student s2 = (Student) o;
// 2、判断2个对象的内容是否一样。
// if(this.name.equals(s2.name) &&
// this.age == s2.age && this.sex == s2.sex){
// return true;
// }else {
// return false;
// }
return this.name.equals(s2.name) && this.age == s2.age
&& this.sex == s2.sex ;
}else {
// 学生只能和学生比较,否则结果一定是false
return false;
}
}*/
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
'}';
}
}
/**
目标:掌握Object类中toString方法的使用。
*/
public class Test1 {
public static void main(String[] args) {
Student s = new Student("周雄", '男', 19);
// String rs = s.toString();
// System.out.println(rs);
// System.out.println(s.toString());
// 直接输出对象变量,默认可以省略toString调用不写的
System.out.println(s);
}
}
import java.util.Objects;
/**
目标:掌握Object类中equals方法的使用。
*/
public class Test2 {
public static void main(String[] args) {
Student s1 = new Student("周雄", '男', 19);
Student s2 = new Student("周雄", '男', 19);
// equals默认是比较2个对象的地址是否相同,子类重写后会调用子类重写的来比较内容是否相同。
System.out.println(s1.equals(s2));
System.out.println(s1 == s2);
System.out.println(Objects.equals(s1, s2));
}
}
2.Objects
Objects概述
Objects是一个工具类,提供了一些方法去完成一些功能。
官方在进行字符串比较时,没有用字符串对象的的equals方法,而是选择了Objects的equals方法来比较。
Objects的常见方法:
下面以具体的代码说明equals和isNull方法:
import java.util.Objects;
/**
目标:掌握objects类的常用方法:equals
*/
public class Test {
public static void main(String[] args) {
String s1 = null;
String s2 = new String("itheima");
// System.out.println(s1.equals(s2)); // 留下了隐患,可能出现空指针异常。
System.out.println(Objects.equals(s1, s2)); // 更安全,结果也是对的!
/**
Objects:
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
*/
System.out.println(Objects.isNull(s1)); // true
System.out.println(s1 == null); // true
System.out.println(Objects.isNull(s2)); // false
System.out.println(s2 == null); // false
}
}
总结:对象进行内容比较的时候建议使用什么?为什么?
1.建议使用Objects提供的equals方法。
2.比较的结果是一样的,但是更安全。
3.StringBuilder
StringBuilder概述
StringBuilder是一个可变的字符串类,我们可以把它看成是一个对象容器。
作用:提高字符串的操作效率,如拼接、修改等。
StringBuilder构造器
/**
目标:学会使用StringBuilder操作字符串,最终还需要知道它性能好的原因
*/
public class StringBuilderDemo1 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(); // ""
sb.append("a");
sb.append("b");
sb.append("c");
sb.append(1);
sb.append(false);
sb.append(3.3);
sb.append("abc");
System.out.println(sb);
StringBuilder sb1 = new StringBuilder();
// 支持链式编程
sb1.append("a").append("b").append("c").append("我爱你中国");
System.out.println(sb1);
// 反转
sb1.reverse().append("110");
System.out.println(sb1);
System.out.println(sb1.length());
// 注意:StringBuilder只是拼接字符串的手段:效率好。
// 最终的目的还是要恢复成String类型。
StringBuilder sb2 = new StringBuilder();
sb2.append("123").append("456");
// 恢复成String类型
String rs = sb2.toString();
check(rs);
}
public static void check(String data){
System.out.println(data);
}
}
主要思路:先使用StringBuilder完成拼接,修改,然后使用toString()将StringBuilder对象的地址返回给String变量,完成StringBuilder类型转为String类型。
StringBuilder常用方法
为什么String使用“+”拼接字符串不如使用StringBuilder方法?
如图:
总结:为什么拼接,反转字符串建议使用StringBuilder?
1.String :内容是不可变的、拼接字符串性能差。
2.StringBuilder:内容是可变的、拼接字符串性能好、代码优雅。
3.定义字符串使用String。
4.拼接、修改等操作字符串使用StringBuilder
案例:打印整形数组内容
需求:
设计一个方法用于输出任意整型数组的内容,要求输出成如下格式:
“该数组内容为:[11, 22, 33, 44, 55]”
分析:
1、定义一个方法,要求该方法能够接收数组,并输出数组内容。 ---> 需要参数吗?需要返回值类型申明吗?
2、定义一个静态初始化的数组,调用该方法,并传入该数组。
public class StringBuilderTest2 {
public static void main(String[] args) {
int[] arr1 = null;
System.out.println(toString(arr1));
int[] arr2 = {10, 88, 99};
System.out.println(toString(arr2));
int[] arr3 = {};
System.out.println(toString(arr3));
}
/**
1、定义方法接收任意整型数组,返回数组内容格式
*/
public static String toString(int[] arr){
if(arr != null){
// 2、开始拼接内容。
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] ).append(i == arr.length - 1 ? "" : ", ");
}
sb.append("]");
return sb.toString();
}else {
return null;
}
}
}
4.Math
Math类
1.包含执行基本数字运算的方法,Math类没有提供公开的构造器。
2.如何使用类中的成员呢?看类的成员是否都是静态的,如果是,通过类名就可以直接调用。
/**
目标:Math类的使用。
Math用于做数学运算。
Math类中的方法全部是静态方法,直接用类名调用即可。
方法:
方法名 说明
public static int abs(int a) 获取参数a的绝对值:
public static double ceil(double a) 向上取整
public static double floor(double a) 向下取整
public static double pow(double a, double b) 获取a的b次幂
public static long round(double a) 四舍五入取整
小结:
记住。
*/
public class MathDemo {
public static void main(String[] args) {
// 1.取绝对值:返回正数
System.out.println(Math.abs(10)); // 10
System.out.println(Math.abs(-10.3)); // 10.3
// 2.向上取整: 5
System.out.println(Math.ceil(4.00000001)); // 5.0
System.out.println(Math.ceil(4.0)); // 4.0
// 3.向下取整:4
System.out.println(Math.floor(4.99999999)); // 4.0
System.out.println(Math.floor(4.0)); // 4.0
// 4.求指数次方
System.out.println(Math.pow(2 , 3)); // 2^3 = 8.0
// 5.四舍五入 10
System.out.println(Math.round(4.49999)); // 4
System.out.println(Math.round(4.500001)); // 5
System.out.println(Math.random()); // 0.0 - 1.0 (包前不包后)
// 拓展: 3 - 9 之间的随机数 (0 - 6) + 3
// [0 - 6] + 3
int data = (int)(Math.random() * 7) + 3;
System.out.println(data);
}
}
5.System
System类概述
System也是一个工具类,代表了当前系统,提供了一些与系统相关的方法。
System类的常用方法
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Arrays;
/**
目标:System系统类的使用。
System代表当前系统。(虚拟机系统)
静态方法:
1.public static void exit(int status):终止JVM虚拟机,非0是异常终止。
2.public static long currentTimeMillis():获取当前系统此刻时间毫秒值。(重点)
3.可以做数组的拷贝。
arraycopy(Object var0, int var1, Object var2, int var3, int var4);
* 参数一:原数组
* 参数二:从原数组的哪个位置开始赋值。
* 参数三:目标数组
* 参数四:赋值到目标数组的哪个位置
* 参数五:赋值几个。
*/
public class SystemDemo {
public static void main(String[] args) {
System.out.println("程序开始。。。");
// System.exit(0); // JVM终止!
// 2、计算机认为时间有起源:返回1970-1-1 00:00:00 走到此刻的总的毫秒值:时间毫秒值。
long time = System.currentTimeMillis();
System.out.println(time);
long startTime = System.currentTimeMillis();
// 进行时间的计算:性能分析
for (int i = 0; i < 100000; i++) {
System.out.println("输出:" + i);
}
long endTime = System.currentTimeMillis();
System.out.println((endTime - startTime)/1000.0 + "s");
// 3、做数组拷贝(了解)
/**
arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length)
参数一:被拷贝的数组
参数二:从哪个索引位置开始拷贝
参数三:复制的目标数组
参数四:粘贴位置
参数五:拷贝元素的个数
*/
int[] arr1 = {10, 20, 30, 40, 50, 60, 70};
int[] arr2 = new int[6]; // [0, 0, 0, 0, 0, 0] ==> [0, 0, 40, 50, 60, 0]
System.arraycopy(arr1, 3, arr2, 2, 3);
System.out.println(Arrays.toString(arr2));
System.out.println("-------------------");
double i = 10.0;
double j = 3.0;
//
// System.out.println(k1);
System.out.println("程序结束。。。。");
}
}
6.BigDecimal
BigDecimal作用
用于解决浮点型运算精度失真的问题。
BigDecima常用API
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
/**
目标:BigDecimal大数据类。
引入:
浮点型运算的时候直接+ * / 可能会出现数据失真(精度问题)。
BigDecimal可以解决浮点型运算数据失真的问题。
BigDicimal类:
包:java.math.
创建对象的方式(最好的方式:)
public static BigDecimal valueOf(double val) :包装浮点数成为大数据对象。
方法声明
public BigDecimal add(BigDecimal value) 加法运算
public BigDecimal subtract(BigDecimal value) 减法运算
public BigDecimal multiply(BigDecimal value) 乘法运算
public BigDecimal divide(BigDecimal value) 除法运算
public double doubleValue(): 把BigDecimal转换成double类型。
*/
public class BigDecimalDemo {
public static void main(String[] args) {
// 浮点型运算的时候直接+ * / 可能会出现数据失真(精度问题)。
System.out.println(0.09 + 0.01);
System.out.println(1.0 - 0.32);
System.out.println(1.015 * 100);
System.out.println(1.301 / 100);
System.out.println("-------------------------");
double a = 0.1;
double b = 0.2;
double c = a + b;
System.out.println(c);
System.out.println("--------------------------");
// 包装浮点型数据成为大数据对象 BigDeciaml
BigDecimal a1 = BigDecimal.valueOf(a);
BigDecimal b1 = BigDecimal.valueOf(b);
BigDecimal c1 = a1.add(b1);
// BigDecimal c1 = a1.subtract(b1);
// BigDecimal c1 = a1.multiply(b1);
// BigDecimal c1 = a1.divide(b1);
System.out.println(c1);
// 目的:double
double rs = c1.doubleValue();
System.out.println(rs);
// 注意事项:BigDecimal是一定要精度运算的
BigDecimal a11 = BigDecimal.valueOf(10.0);
BigDecimal b11 = BigDecimal.valueOf(3.0);
/**
参数一:除数 参数二:保留小数位数 参数三:舍入模式
*/
BigDecimal c11 = a11.divide(b11, 2, RoundingMode.HALF_UP); // 3.3333333333
System.out.println(c11);
System.out.println("-------------------");
}
}
主要思路:首先创建BigDecimal封装浮点型数据(最好的方式是调用方法),得出结果是BigDecimal类型,利用变量名.doubleValue()可以转化成double类型。
边栏推荐
- 老司机总结的12条 SQL 优化方案(非常实用)
- TP5 thinkphp5 report serialization of'closure'is not allowed
- Websocket and socket IO case practice
- 四类线性相位 FIR滤波器设计 —— MATLAB源码全集
- Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
- 中科软外包一面
- 【Spark】.scala文件在IDEA中几种图标的解释
- Several rare but useful JS techniques
- PHP laravel+gatewayworker completes im instant messaging and file transfer (Chapter 1: basic configuration)
- China Medical Grade hydrogel market supply and demand research and prospect analysis report 2022 Edition
猜你喜欢
Lightflow completed the compatibility certification with "daocloud Enterprise Cloud native application cloud platform"
el-form-item 包含两个input, 校验这两个input
小程序中控件里面的内容较多,让其支持滚动的良好方案
New routing file in laravel framework
NoSQL mongodb - 02 mongodb server installation, mongodb shell, basic concepts and visualization tools
【Spark】.scala文件在IDEA中几种图标的解释
【网络是怎么连接的】第二章(下):一个网络包的接收
power designer - 自定义注释按钮
Installing MySQL under Linux (RPM package installation)
PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
随机推荐
TSMC Samsung will mass produce 3nm chips in 2022: will the iPhone be the first?
processing 随机生成线动画
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
【网络是怎么连接的】第二章(下):一个网络包的接收
imagecopymerge
Installing MySQL under Linux (RPM package installation)
简易数字电路交通灯设计
中科软外包一面
关于NaN的一些总结
Realize microservice load balancing (ribbon)
不到40行代码手撸一个BlocProvider
【Spark】.scala文件在IDEA中几种图标的解释
ES6模块
Iframe usage and contentwindow, parent and PostMessage communication methods
el-form-item 包含两个input, 校验这两个input
Why is password salt called "salt"? [Close] - why is a password salt called a "salt"? [closed]
美学心得(第二百三十八集) 罗国正
2022 edition of China's cotton chemical fiber printing and dyeing Market Status Investigation and Prospect Forecast Analysis Report
Nodejs framework express and KOA
Xiaobai lazy special-win10-win11 one click installation version