Math
无构造方法,里面的方法都是 静态的,直接类名.方法名调用
| 方法名 | 说明 |
|---|---|
| public static int abs(int a) | 返回参数的绝对值 |
| public static double ceil(double a) | 返回大于或等于参数的最小double值,等于一个整数 |
| public static double floor(double a) | 返回小于或等于参数的最大double值,等于一个整数 |
| public static int round(float a) | 按照四舍五入返回最接近参数的int |
| public static int max(int a,int b) | 返回两个int值中的较大值 |
| public static int min(int a,int b) | 返回两个int值中的较小值 |
| public static double pow (double a,double b) | 返回a的b次幂的值 |
| public static double random() | 返回值为double的正值,[0.0,1.0) |
System
| 方法名 | 说明 |
|---|---|
| public static void exit(int status) | 终止当前运行的 Java 虚拟机,非零表示异常终止 |
| public static long currentTimeMillis() | 返回当前时间(以毫秒为单位) |
Object类
(1)、toString()方法:以良好的格式,更方便的展示对象中的属性值
(2)、equals()方法
作用:用于对象之间的比较
重写equals方法的场景:不希望比较对象的地址值,想要结合对象属性进行比较的时候。
案例:一个面试题
// 看程序,分析结果
String s = “abc”;
StringBuilder sb = new StringBuilder(“abc”);
s.equals(sb);
sb.equals(s);
public class InterviewTest {
public static void main(String[] args) {
String s1 = "abc";
StringBuilder sb = new StringBuilder("abc");
//1.此时调用的是String类中的equals方法.
//保证参数也是字符串,否则不会比较属性值而直接返回false
//System.out.println(s1.equals(sb)); // false
//StringBuilder类中是没有重写equals方法,用的就是Object类中的.
System.out.println(sb.equals(s1)); // false
}
}
(3)、常用方法
| 方法名 | 说明 |
|---|---|
| public static String toString(对象) | 返回参数中对象的字符串表示形式 |
| public static String toString(对象, 默认字符串) | 返回对象的字符串表示形式 |
| public static Boolean isNull(对象) | 判断对象是否为空 |
| public static Boolean nonNull(对象) | 判断对象是否不为空 |
BigDecimal
1、构造方法
| 方法名 | 说明 |
|---|---|
| BigDecimal(double val) | 参数为double |
| BigDecimal(String val) | 参数为String |
2、常用方法
| 方法名 | 说明 |
|---|---|
| public BigDecimal add(另一个BigDecimal对象) | 加法 |
| public BigDecimal subtract (另一个BigDecimal对象) | 减法 |
| public BigDecimal multiply (另一个BigDecimal对象) | 乘法 |
| public BigDecimal divide (另一个BigDecimal对象) | 除法 |
| public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) | 除法 |
3、注意事项
(1) BigDecimal是用来进行精确计算的
(2) 创建BigDecimal的对象,构造方法使用参数类型为字符串的(使用字符串创建更精确)
(3) 四则运算中的除法,如果除不尽请使用divide的三个参数的方法
4、重要属性
BigDecimal.ROUND_UP 进一法
BigDecimal.ROUND_FLOOR 去尾法
BigDecimal.ROUND_HALF_UP 四舍五入
包装类
1、基本类型的包装类
| 基本数据类型 | 包装类 |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
2、自动装箱和拆箱
自动装箱:把基本数据类型转换为对应的包装类类型
自动拆箱:把包装类类型转换为对应的基本数据类型
3、int和String 类型的互相转换
int转为String
(1)直接在数字后面加一个空字符串
(2)通过String的静态方法valueOf()
示例:
public class IntegerDemo {
public static void main(String[] args) {
//int --- String
int number = 100;
//方式1
String s1 = number + "";
System.out.println(s1);
//方式2
//public static String valueOf(int i)
String s2 = String.valueOf(number);
System.out.println(s2);
System.out.println("--------");
}
}
String转为int
(1)使用Integer类的静态方法valueOf(String s),会返回一个Integer类型的数据
(2)通过Integer静态方法parseInt()
示例:
public class IntegerDemo {
public static void main(String[] args) {
//String --- int
String s = "100";
//方式1:String --- Integer --- int
Integer i = Integer.valueOf(s);
//public int intValue()
int x = i.intValue();
System.out.println(x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println(y);
}
}
递归
注意事项:
(1)递归一定要有出口。否则内存溢出
(2)递归虽然有出口,但是递归的次数也不宜过多。否则内存溢出
案例:快速排序
/**快速排序概述
冒泡排序算法中,一次循环结束,就相当于确定了当前的最大值,也能确定最大值在数组中应存入的位置
快速排序算法中,每一次递归时以第一个数为基准数,找到数组中所有比基准数小的.再找到所有比基准数大的.小的全部放左边,大的全部放右边,确定基准数的正确位置 */
public class MyQuiteSortDemo2 {
public static void main(String[] args) {
// 1,从右开始找比基准数小的
// 2,从左开始找比基准数大的
// 3,交换两个值的位置
// 4,红色继续往左找,蓝色继续往右找,直到两个箭头指向同一个索引为止
// 5,基准数归位
int[] arr = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
quiteSort(arr,0,arr.length-1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
private static void quiteSort(int[] arr, int left, int right) {
// 递归结束的条件
if(right < left){
return;
}
int left0 = left;
int right0 = right;
//计算出基准数
int baseNumber = arr[left0];
while(left != right){
// 1,从右开始找比基准数小的
while(arr[right] >= baseNumber && right > left){
right--;
}
// 2,从左开始找比基准数大的
while(arr[left] <= baseNumber && right > left){
left++;
}
// 3,交换两个值的位置
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//基准数归位
int temp = arr[left];
arr[left] = arr[left0];
arr[left0] = temp;
// 递归调用自己,将左半部分排好序
quiteSort(arr,left0,left-1);
// 递归调用自己,将右半部分排好序
quiteSort(arr,left +1,right0);
}
}
本文深入探讨了Java中的Math、System及Object等核心类的功能与使用方法,并介绍了包装类和BigDecimal类在数值处理中的应用,同时讲解了递归算法的基础知识及其实现案例。

322

被折叠的 条评论
为什么被折叠?



