Math
Math类概述 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) |
public class MathDemo {
public static void main(String[] args) {
System.out.println(Math.abs(88));
System.out.println(Math.abs(-88));
System.out.println("——————————");
System.out.println(Math.ceil(34.56));
System.out.println(Math.ceil(54.74));
System.out.println("——————————");
System.out.println(Math.floor(43.64));
System.out.println(Math.floor(32.32));
System.out.println("——————————");
System.out.println(Math.round(34.4F));
System.out.println(Math.round(23.6F));
System.out.println("——————————");
System.out.println(Math.max(4, 6));
System.out.println(Math.max(43, 23));
System.out.println("——————————");
System.out.println(Math.min(4, 6));
System.out.println(Math.min(43, 23));
System.out.println("——————————");
System.out.println(Math.pow(2.0, 2.0));
System.out.println(Math.pow(4.5, 3.3));
System.out.println("——————————");
System.out.println(Math.random());
System.out.println(Math.random() * 100);
System.out.println((int) (Math.random() * 100));
System.out.println("——————————");
}
运行结果:  
System
System类概述 System包含几个有用的类字段和方法,它不能被实例化
System类的常用方法
方法名 | 说明 |
---|
public static void exit(int status) | | public static long current TimeMillis() | 返回当前时间(以毫秒为单位) |
public class SystemDemo {
public static void main(String[] args) {
System.out.println(System.currentTimeMillis() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
}
Object
Object类的概述 Object是类层次结构的根,每个类都可以将Object作为超类。所有类都直接或者间接的继承自该类
构造方法:public Object()
回想面向对象中,为什么说子类的构造方法默认访问的是父类的无参构造方法? 因为它们的顶级父类只有无参构造方法
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setName("小白");
s.setAge(12);
System.out.println(s);
System.out.println(s.toString());
}
}
运行结果:  Object类的常用方法
方法名 | 说明 |
---|
public String toString() | 返回对象的字符串表示形式。建议所有子类重写该方法,自动生成 | public boolean equals(Object obj) | 比较对象是否相等。默认比较地址,重写可以比较内容,自动生成 |
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
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;
}
@Override
public int hashCode() {
return Objects.hash(age, name);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
return age == other.age && Objects.equals(name, other.name);
}
public class ObjectDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("小白");
s1.setAge(14);
Student s2 = new Student();
s2.setName("小白");
s2.setAge(14);
System.out.println(s1.equals(s2));
}
}
运行结果: 
Arrays
冒泡排序 排序:将一组数据按照固定的规则进行排序
冒泡排序:一种排序方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在后面,依次对所有的数据进行操作,直至所有的数据按要求完成排序
- 如果有n个数据进行排序,总共需要比较n-1次
- 每一次比较完毕,下一次的比较就会少一个数据参与
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = { 12, 43, 54, 23, 56 };
System.out.println("排序前:" + arrayToString(arr));
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("排序后:" + arrayToString(arr));
}
public static String arrayToString(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(",");
}
}
sb.append("]");
String s = sb.toString();
return s;
}
}
运行结果:  Arrays类的概述和常用方法
Arrays类包含用于操作数组的各种方法
方法名 | 说明 |
---|
public static String toString(int[] a) | 返回指定数组的内容的字符换表示形式 | public static void sort(int[] a) | 按照数字顺序排列指定的数组 |
import java.util.Arrays;
public class ArraysDemo {
public static void main(String[] args) {
int[] arr = { 23, 65, 3, 67, 56 };
System.out.println("排序前:" + Arrays.toString(arr));
Arrays.sort(arr);
System.out.println("排序后:" + Arrays.toString(arr));
}
}
运行结果: 
工具类的设计思想:
- 构造方法用private修饰
- 成员用public static修饰
基本类型包装类
基本类型包装类概述 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
常用的操作之一:用于基本数据类型与字符串之间的转换
基本数据类型 | 包装类 |
---|
byte | Byte | short | Short | int | Integer | long | Long | float | Float | double | Double | char | Character | boolean | Boolean |
public class IntegerDemo {
public static void main(String[] args) {
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
}
}
运行结果: 
Integer类的概述和使用
Integer:包装一个对象中的原始类型int的值
方法名 | 说明 |
---|
public Integer(int valve) | 根据int值创建Integer对象(过时) | public Integer(String s) | 根据String值创建Integer对象(过时) | public static Integer valueOf(int i) | 返回表示指定的int值得Integer实例 | public static Integer valueOf(String s) | 返回一个保存指定值得Integer对象String |
public class IntegerDemo {
public static void main(String[] args) {
Integer i1 = new Integer(100);
System.out.println(i1);
Integer i2 = new Integer(100);
System.out.println(i2);
Integer i3 = Integer.valueOf(100);
System.out.println(i3);
Integer i4 = Integer.valueOf("100");
System.out.println(i4);
}
}
运行结果: 
int和String的相互转换
基本类型包装类的最常见操作就是:用于基本类型和字符串之间的相互转换
int转换为String public static String valueOf(int i):返回int参数的字符串表示形式。该方法是String类中的方法
String转换为int public static String parseInt(String s):将字符串解析为int类型。该方法是Integer类中的方法
public class IntegerDemo2 {
public static void main(String[] args) {
int number = 100;
String s1 = "" + number;
System.out.println(s1);
String s2 = String.valueOf(number);
System.out.println(s2);
System.out.println("————————————");
String s = "100";
Integer i = Integer.valueOf(s);
int x = i.intValue();
System.out.println(x);
int y = Integer.parseInt(s);
System.out.println(y);
}
}
运行结果: 
案例:字符串中数据排序
需求:有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:“27 38 46 50 91”
import java.util.Arrays;
public class IntegerText {
public static void main(String[] args) {
String s = "91 27 46 38 50";
String[] strArray = s.split(" ");
int[] arr = new int[strArray.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(strArray[i]);
}
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(" ");
}
}
String result = sb.toString();
System.out.println(result);
}
}
运行结果: 
自动装箱和拆箱
- 装箱:把基本数据类型转换为对应的包装类类型
- 拆箱:把包装类类型转换为对应的基本数据类型
public class IntegerDemo3 {
public static void main(String[] args) {
Integer i = Integer.valueOf(100);
Integer ii = 100;
ii += 200;
System.out.println(ii);
Integer iii = null;
if (iii != null) {
iii += 300;
}
}
}
注意: 在使用包装类类型的时候,如果做操作,最好先判断是否为null。 只要是对象,在使用前就必须进行不为null的判断
|