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) {
// public static int abs(int a) 返回参数的绝对值
System.out.println(Math.abs(88));
System.out.println(Math.abs(-88));
System.out.println("——————————");
// public static double ceil(double a)返回大于或等于参数的最小double值,等于一个整数
System.out.println(Math.ceil(34.56));
System.out.println(Math.ceil(54.74));
System.out.println("——————————");
// public static double floor(double a)返回小于或等于参数的最大double值,等于一个整数
System.out.println(Math.floor(43.64));
System.out.println(Math.floor(32.32));
System.out.println("——————————");
// public static int round(float a)按照四舍五入返回最接近参数的int
System.out.println(Math.round(34.4F));
System.out.println(Math.round(23.6F));
System.out.println("——————————");
// public static int max(int a,int b)返回两个int值中的较大值
System.out.println(Math.max(4, 6));
System.out.println(Math.max(43, 23));
System.out.println("——————————");
// public static int min(int a,int b)返回两个int值中的较小值
System.out.println(Math.min(4, 6));
System.out.println(Math.min(43, 23));
System.out.println("——————————");
// public static double pow(double a,double b)返回a的b次幂的值
System.out.println(Math.pow(2.0, 2.0));
System.out.println(Math.pow(4.5, 3.3));
System.out.println("——————————");
// public static double random()返回值为double的正值,[0.0,1.0)
System.out.println(Math.random());
System.out.println(Math.random() * 100);// 扩到100倍
System.out.println((int) (Math.random() * 100));// 取整
System.out.println("——————————");
}
运行结果:
System
System类概述
System包含几个有用的类字段和方法,它不能被实例化
System类的常用方法
方法名 | 说明 |
---|---|
public static void exit(int status) | 终止当前运行的Java虚拟机,非零表示异常终止 |
public static long current TimeMillis() | 返回当前时间(以毫秒为单位) |
public class SystemDemo {
public static void main(String[] args) {
// System.out.println("开始");
// public static void exit(int status) 终止当前运行的Java虚拟机,非零表示异常终止
// System.exit(0);
// System.out.println("结束");//已经终止了Java虚拟机,不会再运行这一条
// public static long current TimeMillis()返回当前时间(以毫秒为单位)
// System.out.println(System.currentTimeMillis());//当前时间和1970年之间的毫秒值
System.out.println(System.currentTimeMillis() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");// 当前时间和1970年之间的年数
// 得知这个for循环用了多长时间
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 + "]";
}
}
//重写tostring方法
//测试类
public class ObjectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setName("小白");
s.setAge(12);
System.out.println(s);// API.Student@3ac3fd8b,原因如下
System.out.println(s.toString());// 同上
// 查看println源码
/*
* public void println(Object x) { String s = String.valueOf(x); if (getClass()
* == PrintStream.class) { // need to apply String.valueOf again since first
* invocation // might return null writeln(String.valueOf(s)); } else {
* synchronized (this) { print(s); newLine(); } } }
*
* public static String valueOf(Object obj) { return (obj == null) ? "null" :
* obj.toString(); }
*
* public String toString() { return getClass().getName() + "@" +
* Integer.toHexString(hashCode()); }
*/
}
}
运行结果:
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);
}
//重写equals,自动生成
@Override
public boolean equals(Object obj) {
// this---s1
// obj---s2
if (this == obj)// 比较地址值
return true;
if (obj == null)// 判断参数为null
return false;
if (getClass() != obj.getClass())// 判断是否来自同一个类
return false;
Student other = (Student) obj;// 向下转型,other----s2
// age相同,然后调方法
return age == other.age && Objects.equals(name, other.name);
}
//重写tostring方法
//测试类
//public boolean equals(Object obj);指示一些其他对象是否等于此
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==s2);//false,比较的是地址值
System.out.println(s1.equals(s2));
/*
* public boolean equals(Object obj) { this----s1 obj----s2 return (this ==
* obj); }
*/
}
}
运行结果:
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));
}
// 把数组中的元素按照指定规则组成字符串[元素1,元素2,...]
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;
//Arrays类包含用于操作数组的各种方法
//public static String toString(int[] a) 返回指定数组的内容的字符换表示形式|
//public static void sort(int[] a)按照数字顺序排列指定的数组
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) {
// 需求:要判断一个数据是否在int范围内
// public static final int MIN_VALUE
// public static final int MAX_VALUE
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 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) {
// public Integer(int valve) 根据int值创建Integer对象(过时)
Integer i1 = new Integer(100);
System.out.println(i1);
// public Integer(String s)根据String值创建Integer对象(过时)
Integer i2 = new Integer(100);
// Integer i2=new Integer("abc");//NumberFormatException
System.out.println(i2);
// public static Integer valueOf(int i)返回表示指定的int值得Integer实例
Integer i3 = Integer.valueOf(100);
System.out.println(i3);
// public static Integer valueOf(String s)返回一个保存指定值得Integer对象String
Integer i4 = Integer.valueOf("100");// 若写字母,还是会NumberFormatException
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类中的方法
//int和String的相互转换
public class IntegerDemo2 {
public static void main(String[] args) {
// int 转到 String
int number = 100;
// 法一
String s1 = "" + number;// 字符串连接
System.out.println(s1);
// 法二 public static String valueOf(int i)
String s2 = String.valueOf(number);
System.out.println(s2);
System.out.println("————————————");
// String 转到 int
String s = "100";
// 法一
// String---Integer---int
Integer i = Integer.valueOf(s);
int x = i.intValue();// public int intValue();
System.out.println(x);
// 法二
// public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println(y);
}
}
运行结果:
案例:字符串中数据排序
需求:有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:“27 38 46 50 91”
import java.util.Arrays;
//字符串中数据排序
//需求:有一个字符串:“91 27 46 38 50”
//请写程序实现最终输出结果是:“27 38 46 50 91”
public class IntegerText {
public static void main(String[] args) {
// 定义一个字符串
String s = "91 27 46 38 50";
// 把字符串中的数据存储到一个int类型的数组中
// public String[] split(String regex)
String[] strArray = s.split(" ");
// for (int i = 0; i < strArray.length; i++) {
// System.out.println(strArray[i]);
// }
// 定义一个int数组,把String数组中的每个元素存储到int数组中
int[] arr = new int[strArray.length];
for (int i = 0; i < arr.length; i++) {
// public static int parseInt(String s)
arr[i] = Integer.parseInt(strArray[i]);
}
// 对int数组排序
Arrays.sort(arr);
// 把排序后的int数组中的元素拼接得到一个字符串
// 用StringBuilder实现
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;// Integer.valueOf(100)
// 拆箱:把包装类类型转换为对应的基本数据类型
// ii+=200;
// 手动拆箱:ii.intValue()
// ii=ii.intValue()+200;
// 自动拆箱
ii += 200;
System.out.println(ii);
Integer iii = null;
// iii+=300;//NullPointerException
if (null != iii) {
iii += 300;
}
}
}
注意:
在使用包装类类型的时候,如果做操作,最好先判断是否为null。
只要是对象,在使用前就必须进行不为null的判断