文章目录
1.API
1.1 API概述【理解】
-
什么是API
API (Application Programming Interface) :应用程序编程接口
-
java中的API
指的就是 JDK 中提供的各种功能的 Java类,这些类将底层的实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可,我们可以通过帮助文档来学习这些API如何使用。
1.2 如何使用API帮助文档【应用】
- 打开帮助文档
- 找到索引选项卡中的输入框
- 在输入框中输入Random
- 看类在哪个包下
- 看类的描述
- 看构造方法
- 看成员方法
2.常用API
2.1 Math(应用)
-
1、Math类概述
- Math 包含执行基本数字运算的方法
-
2、Math中方法的调用方式
- Math类中无构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用
-
3、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) |
- 4.示例代码
package com.a.demo1;
public class MathDemo {
public static void main(String[] args) {
// public static int abs(int a) 返回参数的绝对值
// int abs = Math.abs(10);
// System.out.println(abs);
// public static double ceil(double a) 向上取整
// double ceil = Math.ceil(10.1);
// System.out.println(ceil);
// public static double floor(double a) 向下取整
// double floor = Math.floor(10.9);
// System.out.println(floor);
// public static int round(float a) 四舍五入
// long round = Math.round(10.1);
// System.out.println(round);
//
// long round1 = Math.round(1.9);
// System.out.println(round1);
// public static int max(int a,int b) 返回两个int值中的较大值
// int max = Math.max(10, 20);
// System.out.println(max);
// public static int min(int a,int b) 返回两个int值中的较小值
// int min = Math.min(10, 20);
// System.out.println(min);
// public static double pow(double a,double b)返回a的b次幂的值
// double pow = Math.pow(2, 3);
// System.out.println(pow);
// public static double random() 返回值为double的正值,[0.0,1.0)
for (int i = 0; i < 10 ; i++) {
double random = Math.random();
System.out.println(random);
}
}
}
2.2 System(应用)
- System类的常用方法
方法名 说明 public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止 public static long currentTimeMillis() 返回当前时间(以毫秒为单位)
package com.a.demo1;
public class SystemDemo {
public static void main(String[] args) {
// public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止
// System.out.println(111);
// //while(true){}
// System.exit(0); //当代码执行到这个方法的时候,就表示虚拟机已经停止了
// System.out.println(2222);
// public static long currentTimeMillis() 返回当前时间(以毫秒为单位)
// long start = System.currentTimeMillis();//获取当前时间
// //System.out.println(l);
// for (int i = 0; i < 10000; i++) {
// System.out.println(i);
// }
// long end = System.currentTimeMillis();//获取当前时间
// System.out.println(end - start);//472 --- 得到的就是这个for循环运行的时间.
// arraycopy(数据源数组, 起始索引, 目的地数组, 起始索引, 拷贝个数) 数组copy
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = new int[10];
//需求:我要把arr1中的数据拷贝到arr2中.
//System.arraycopy(arr1,0,arr2,0,arr1.length);
/*for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}*/
//我要把arr1最后两个元素,拷贝到arr2的最后两个索引上
System.arraycopy(arr1, 3, arr2, 8, 2);
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
}
}
-
示例代码
- 需求:在控制台输出1-10000,计算这段代码执行了多少毫秒
public class SystemDemo { public static void main(String[] args) { // 获取开始的时间节点 long start = System.currentTimeMillis(); for (int i = 1; i <= 10000; i++) { System.out.println(i); } // 获取代码运行结束后的时间节点 long end = System.currentTimeMillis(); System.out.println("共耗时:" + (end - start) + "毫秒"); } }
2.3 Object类的toString方法(应用)
-
Object类概述
- Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份
-
查看方法源码的方式
- 选中方法,按下Ctrl + B
-
重写toString方法的方式
- 1.Alt + Insert 选择toString
- 1.在类的空白区域,右键 -> Generate -> 选择toString
-
toString方法的作用:
- 以良好的格式,更方便的展示对象中的属性值
-
示例代码:
class Student extends Object { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = 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; } @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(30); System.out.println(s); System.out.println(s.toString()); } }
-
运行结果:
Student{name='林青霞', age=30} Student{name='林青霞', age=30}
2.4 Object类的equals方法(应用)
-
equals方法的作用
- 用于对象之间的比较,返回true和false的结果
- 举例:s1.equals(s2); s1和s2是两个对象
-
重写equals方法的场景
- 不希望比较对象的地址值,想要结合对象属性进行比较的时候。
-
重写equals方法的方式
- 1.alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可
- 2.在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。
-
示例代码:
class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = 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; } @Override public boolean equals(Object o) { //this -- s1 //o -- s2 if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; //student -- s2 if (age != student.age) return false; return name != null ? name.equals(student.name) : student.name == null; } } public class ObjectDemo { public static void main(String[] args) { Student s1 = new Student(); s1.setName("林青霞"); s1.setAge(30); Student s2 = new Student(); s2.setName("林青霞"); s2.setAge(30); //需求:比较两个对象的内容是否相同 System.out.println(s1.equals(s2)); } }
-
面试题
// 看程序,分析结果 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 } }
2.5 Objects (应用)
-
常用方法
方法名 说明 public static String toString(对象) 返回参数中对象的字符串表示形式。 public static String toString(对象, 默认字符串) 返回对象的字符串表示形式。 public static Boolean isNull(对象) 判断对象是否为空 public static Boolean nonNull(对象) 判断对象是否不为空 -
示例代码
学生类
class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
测试类
public class MyObjectsDemo { public static void main(String[] args) { // public static String toString(对象): 返回参数中对象的字符串表示形式。 // Student s = new Student("小罗同学",50); // String result = Objects.toString(s); // System.out.println(result); // System.out.println(s); // public static String toString(对象, 默认字符串): 返回对象的字符串表示形式。如果对象为空,那么返回第二个参数. //Student s = new Student("小花同学",23); // Student s = null; // String result = Objects.toString(s, "随便写一个"); // System.out.println(result); // public static Boolean isNull(对象): 判断对象是否为空 //Student s = null; // Student s = new Student(); // boolean result = Objects.isNull(s); // System.out.println(result); // public static Boolean nonNull(对象): 判断对象是否不为空 //Student s = new Student(); Student s = null; boolean result = Objects.nonNull(s); System.out.println(result); } }
2.6 BigDecimal (应用)
- 作用
可以用来进行精确计算
- 构造方法
方法名 | 说明 |
---|---|
BigDecimal(double val) | 参数为double |
BigDecimal(String val) | 参数为String |
- 常用方法
方法名 | 说明 |
---|---|
public BigDecimal add(另一个BigDecimal对象) | 加法 |
public BigDecimal subtract (另一个BigDecimal对象) | 减法 |
public BigDecimal multiply (另一个BigDecimal对象) | 乘法 |
public BigDecimal divide (另一个BigDecimal对象) | 除法 |
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) | 除法 |
- 代码示例:
package com.ia.demo;
import java.math.BigDecimal;
public class MyBigDecimalDemo3 {
//如果想要进行精确运算,那么请使用字符串的构造
public static void main(String[] args) {
// BigDecimal bd1 = new BigDecimal(0.1);
// BigDecimal bd2 = new BigDecimal(0.2);
BigDecimal bd1 = new BigDecimal("0.1");
BigDecimal bd2 = new BigDecimal("0.2");
// public BigDecimal add(另一个BigDecimal对象) 加法
BigDecimal add = bd1.add(bd2);
System.out.println("和为" + add);
//System.out.println(0.1 + 0.2);
// public BigDecimal subtract (另一个BigDecimal对象) 减法
BigDecimal subtract = bd1.subtract(bd2);
System.out.println("差为" + subtract);
// public BigDecimal multiply (另一个BigDecimal对象) 乘法
BigDecimal multiply = bd1.multiply(bd2);
System.out.println("积为" + multiply);
// public BigDecimal divide (另一个BigDecimal对象) 除法
BigDecimal divide = bd1.divide(bd2);
System.out.println("商为"+divide);
}
}
package com.a.demo;
import java.math.BigDecimal;
public class MyBigDecimalDemo4 {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("0.3");
BigDecimal bd2 = new BigDecimal("4"); //0.075
/* BigDecimal divide = bd1.divide(bd2);
System.out.println(divide);*/
//参数一:表示参数运算的另一个对象
//参数二:表示小数点后精确到多少位
//参数三:舍入模式
//进一法 BigDecimal.ROUND_UP
//去尾法 BigDecimal.ROUND_FLOOR
//四舍五入 BigDecimal.ROUND_HALF_UP
BigDecimal divide = bd1.divide(bd2, 2, BigDecimal.ROUND_HALF_UP);
System.out.println(divide);
}
}
-
总结
1.BigDecimal是用来进行精确计算的
2. 创建BigDecimal的对象,构造方法使用参数类型为字符串的。
3. 四则运算中的除法,如果除不尽请使用divide的三个参数的方法。代码示例:
BigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式); 参数1 ,表示参与运算的BigDecimal 对象。 参数2 ,表示小数点后面精确到多少位 参数3 ,舍入模式 BigDecimal.ROUND_UP 进一法 BigDecimal.ROUND_FLOOR 去尾法 BigDecimal.ROUND_HALF_UP 四舍五入
3.包装类
3.1 基本类型包装类(记忆)
-
基本类型包装类的作用
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
常用的操作之一:用于基本数据类型与字符串之间的转换
-
基本类型对应的包装类
基本数据类型 包装类 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
3.2 Integer类(应用)
-
Integer类概述
包装一个对象中的原始类型 int 的值
-
Integer类构造方法
方法名 说明 public Integer(int value) 根据 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 MyIntegerDemo1 {
public static void main(String[] args) {
//需求:我要判断一个整数是否在 int 范围内?
//Integer
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}
public class IntegerDemo {
public static void main(String[] args) {
//public Integer(int value):根据 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);
System.out.println("--------");
//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");
System.out.println(i4);
}
}
3.3 自动拆箱和自动装箱(理解)
-
自动装箱
把基本数据类型转换为对应的包装类类型
-
自动拆箱
把包装类类型转换为对应的基本数据类型
-
示例代码
Integer i = 100; // 自动装箱 i += 200; // i = i + 200; i + 200 自动拆箱;i = i + 200; 是自动装箱
3.4 int和String类型的相互转换(记忆)
-
int转换为String
-
转换方式
- 方式一:直接在数字后加一个空字符串
- 方式二:通过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
-
转换方式
- 方式一:先将字符串数字转成Integer,再调用valueOf()方法
- 方式二:通过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); } }
-
3.5 字符串数据排序案例(应用)
-
案例需求
有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:27 38 46 50 91
-
代码实现
public class IntegerTest { public static void main(String[] args) { //定义一个字符串 String s = "91 27 46 38 50"; //把字符串中的数字数据存储到一个int类型的数组中 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++) { arr[i] = Integer.parseInt(strArray[i]); } //对 int 数组进行排序 Arrays.sort(arr); for(int i=0; i<arr.length; i++){ System.out.print(arr[i] + " "); } }