常用的API
API(Application Programming Interface),应用编程接口
JDK API 1.6.0中文版
Scanner类
Scanner类的功能:可以实现键盘输入数据,到程序当中。
引用类型的一般使用步骤:
-
导包
import 包路径.类名称;
只有java.lang包下的内容不需要导包,其他的包都需要import语句。 -
创建
类名称 对象名 = new 类名称(); -
使用
对象名.成员方法名();
import java.util.Scanner;// 1. 导包
public class Demo01Scanner {
public static void main(String[] args) {
// 2. 创建
// System.in代表从键盘进行输入2
Scanner sc = new Scanner(System.in);
// 3.使用
int num = sc.nextInt();// 获取键盘输入的int字符
System.out.println("输入的int数字是:"+num);
String str = sc.next();// 获取键盘输入的字符串
System.out.println("输入的字符串是:"+str);
}
}
匿名对象
匿名对象就是只有右边的对象,没有左边的名字和赋值运算符
new 类名称();
【注意事项】匿名对象只能使用唯一的一次,下次再用不得不再创建一个新对象。
【使用建议】如果确定有一个对象只需要使用唯一的一次,就可以使用匿名对象。
public class Demo01Anonymous {
public static void main(String[] args) {
methodParam(new Scanner(System.in));
Scanner sc = methodReturn();
int num2 = sc.nextInt();
System.out.println("输入的是" + num2);
}
// 匿名对象作为参数
public static void methodParam(Scanner sc){
int num = sc.nextInt();
System.out.println("输入的是" + num);
}
// 匿名对象作为返回值
public static Scanner methodReturn(){
return new Scanner(System.in);
}
}
Random类
生成随机数
import java.util.Random;
public class Demo01Random {
public static void main(String[] args) {
Random r = new Random();
int a = r.nextInt(3);//在[0,10)内生成随机数
System.out.println("a="+a);
}
}
对象数组Arraylist类
实现大小可变的数组
|
<E>表示泛型,也就是装在集合当中的所有元素,全都是统一的什么类型。
【注意】泛型只能是引用类型,不能是基本类型
常用方法:
import java.util.ArrayList;
public class Demo02ArrayList {
public static void main(String[] args) {
//从JDK1.7+开始,右侧尖括号内部可以不写内容
ArrayList<String> list = new ArrayList<>();
System.out.println(list);//[]
//向集合中添加数据
list.add("易烊千玺");
list.add("王俊凯");
list.add("王源");
System.out.println(list);
//从集合当中获取元素
String name = list.get(2);
System.out.println(name);
//从集合中删除元素
String whoRemoved = list.remove(2);
System.out.println(whoRemoved);
//获取集合的尺寸长度
int size = list.size();
System.out.println(size);
}
}
如果希望向集合ArrayList中存储基本类型数据,必须使用基本类型对应的“包装类”
基本类型 | 包装类(引用类型,包装类都位于java.lang包下) |
---|---|
byte | Byte |
short | Short |
int | Integer 【特殊】 |
long | Long |
float | Float |
double | Double |
char | Character 【特殊】 |
boolean | Boolean |
Integer 和 int 的区别
- Integer是int的包装类,int则是java的一种基本数据类型
- Integer变量必须实例化后才能使用,而int变量不需要
- Integer实际是对象的引用,当new一个Integer时,实际上是生成一个指针指向此对象;而int则是直接存储数据值
- Integer的默认值是null,int的默认值是0
从jdk1.5+开始,支持自动装箱,自动拆箱
自动装箱:基本类型 --> 包装类型
自动拆箱:包装类型 --> 基本类型
/*
自动装箱与自动拆箱:基本类型的数据和包装类直接可以自动的相互转换
JDK1.5之后出现的新特性
*/
public class Demo02Integer {
public static void main(String[] args) {
// 自动装箱:直接把int类型的整数赋值给包装类
Integer in = 1;//相当于Integer in = new Integer(1);
// 自动拆箱:in是包装类,无法直接参与运算,可以自动转换为基本数据类型,再进行计算
in = in + 2;//相当于in.intValue()+2=3 in = in.intValue()+2=3又是一个自动装箱
}
}
public class Demo02ArrayList {
public static void main(String[] args) {
//从JDK1.7+开始,右侧尖括号内部可以不写内容
ArrayList<String> list = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(199);//隐含自动装箱 list2.add(new Integer(199))
list2.add(188);
int num = list2.get(1);//自动拆箱 list2.get(0).intValue()
}
}
包装类
基本数据类型,使用起来非常方便,但是没有对应的方法来操作这些基本类型的数据
可以使用一个类,把基本类型的数据装起来,在类中定义一些方法,这个类叫做包装类
我们可以使用类中的方法来操作这些基本类型的数据
拆箱和装箱:
/*
装箱:把基本类型包装到包装类中
构造方法:
Integer(int value) 构造一个新分配的 Integer 对象,它表示指定的 int 值。
Integer(String s) 构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
传递的字符串,必须是基本类型的字符串,否则抛出异常 "100"正确 "a"异常
静态方法:
static Integer valueOf(int i) 返回一个表示指定的int值的Integer实例
static Integer valueOf(String s) 返回保存指定的String的值的Integer对象
拆箱:在包装类中取出基本类型的数据
成员方法:intValue() 以 int 类型返回该 Integer 的值。
*/
public class Demo01Integer {
public static void main(String[] args) {
//装箱
Integer in1 = new Integer(1);//方法上有横线,说明方法过时了
System.out.println(in1);//1 重写了toString方法
Integer in2 = new Integer("2");
System.out.println(in2);
//静态方法
Integer in3 = Integer.valueOf(3);
System.out.println(in3);
Integer in4 = Integer.valueOf("4");
System.out.println(in4);
// 拆箱
int i = in1.intValue();
System.out.println(i);
}
}
基本类型与字符串之间的相互转换
/*
基本类型与字符串类型之间的相互转换
基本类型->字符串(String)
1. 基本类型的值+"" 最简单的方法(工作中常用)
2. 包装类的静态方法toString(参数),不是Object类的toString() 重载
static String toString(int i) 返回一个表示指定整数的String对象
3. String类的静态方法valueOf(参数)
字符串(String)->基本类型
使用包装类的静态方法parseXXX("数值类型的字符串");
Integer类:static int parseInt(String s)
Double类:static int parseDouble(String s)
*/
public class Demo03Integer {
public static void main(String[] args) {
//基本类型->字符串(String)
int i1 = 100;
String s1 = i1+"";
System.out.println(s1+200);//100200
String s2 = Integer.toString(100);
System.out.println(s2+200);//100200
String s3 = String.valueOf(100);
System.out.println(s3+200);//100200
//字符串(String)->基本类型
int i = Integer.parseInt(s1);
System.out.println(i-10);
}
}