Hello World
public class HelloWorld {
public static void main(String []args) {
System.out.println("Hello World!");
}
}
基本数据类型
- byte 1字节整数
- short 2字节整数
- int 4字节整数
- long 8字节整数
- float 4字节浮点数
- double 8字节浮点数
- boolean 布尔型
- char 字符型
包装数据类型
基本数据类型首字母大写后为包装数据类型,可以使用包装数据类型方法
byte b = 21
Byte b = Byte.valueOf("21");
System.out.println(Byte.MAX_VALUE); //127
System.out.println(Byte.MIN_VALUE); //-128
System.out.println(Byte.SIZE); //8
包装数据类型转基本数据类型
Byte b = 21;
byte b1 = b.byteValue();
int b2 = b.intValue();
short b3 = b.shortValue();
类型转换
自动类型转换
byte a = 123;
int b = a;
byte->short->int->long->float->double
char->int
强制类型转换
会损失部分数据
int a = (int)123.456; // a = 123
变量、常量声明
用类型 变量名 = 数据
声明变量,用final
声明的数据不可变
int a = 123;
a = 345;
final int A = 123;
A = 345; // error
接收用户输入
Scanner sc = new Scanner(System.in);
System.out.println("你输入了" + sc.nextLine());
控制流
if-else
int a = 1;
if (a == 3) {
System.out.println("a == 3");
} else if (a == 2) {
System.out.println("a == 2");
} else {
System.out.println("a == 1");
}
while
int a = 1;
while (a < 10) {
System.out.println(a);
a ++;
}
do-while
int a = 0;
do {
System.out.println(a);
a ++;
} while (a < 10);
for
for(int a = 0; a < 10; a ++) {
System.out.println(a);
a ++;
}
switch
int a = 1;
switch(a) {
case 0:
System.out.println(0);
break;
case 2:
System.out.println(2);
break;
case 1:
System.out.println(1);
break;
default:
System.out.println(1);
break;
}
字符串
String str = "hello";
String str2 = "World ";
String str3 = "hello";
String str4 = new String("hello");
str.length(); // 5 获取字符串长度
str == str3; // true 比较字符串
str.substring(1,3); // "el" 截取字符串
str.toUpperCase(); // "HELLO" 转换为大写
str2.toLowerCase(); // "world " 转换为小写
str.indexOf("ll"); // 2 查找子字符串位置
str2.trim(); // "World" 去除前后空格
StringBuffer
StringBuffer sb = new StringBuffer("aaa");
sb.append("bbb"); // aaabbb
sb.append(123); // aaabbb123
sb.insert(3, '.'); // aaa.bbb123
sb.reverse(); // 321bbb.aaa
数组
数组是一组变量集合,存储相同数据类型
的一组数据
- 长度固定不变
- 用
类型名[] 变量名
或类型名 变量名[]
声明 - 用
new 类型名[数组长度]
分配空间 - 用
变量名[索引]
访问数组元素
int []array;
array = new int[30]; // [0 * 30]
array[0] = 90;
int []arr = new int[]{1, 2, 3, 4}; // [1, 2, 3, 4]
int []arr2 = {1, 2, 3, 4}; // [1, 2, 3, 4]
用for-in遍历数组
int[] arr = {1, 2, 3, 4};
for (int i : arr) {
System.out.print(i);
}
// 1234
数组方法
int[] arr1 = {1, 2, 3};
int[] arr2 = {3, 1, 2};
// 数组比较
System.out.println(Arrays.equals(arr1, arr2)); // false
// 数组排序
Arrays.sort(arr2);
System.out.println(Arrays.equals(arr1, arr2)); // true
// 填充
Arrays.fill(arr1, 40); // arr1 = [40, 40, 40]
// 二分搜索 数组必须是已排序的
Arrays.binarySearch(arr2, 2); // 1
// 拷贝为一个指定长度数组
Arrays.copyOf(arr2, 5); // [1, 2, 3, 0, 0]
// 截取数组
Arrays.copyOfRange(arr2, 1, 2); // [2]
使用Java8 Stream API和Lambda操作数组
Java8 Lambda
(参数列表) -> {函数体}
Function f = (Object arg) -> {return 1;};
- 有且仅有一个参数时,参数列表的括号可省略
- 函数体仅有一个表达式且返回值为该表达式值时,可省略函数体括号
用于代替匿名类的写法
Integer[] arr = {1, 4, 2, 3};
// 数组排序
Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
for(Integer i: arr) System.out.print(i); // 1234
// 使用Lambda
Arrays.sort(arr, (o1, o2) -> o1 - o2);
for(Integer i: arr) System.out.print(i); // 1234
// 使用方法引用
Arrays.sort(arr, Integer::compareTo);
- Lambda可以用于编写任何实现函数式接口的类
- 函数式接口:只有一个方法的接口
- Lambda会变为以Lambda实现函数式接口方法的类,上例中,Lambda实现了Comparator类的compare方法
- 类名、参数类型、返回值类型都可以由编译器自行推断
Java8 Stream API
int[] arr = {1, 4, 2, 3};
// 使用Arrays.stream()将数组转换为Stream
IntStream stm = Arrays.stream(arr);
// 遍历操作
stm.forEach(System.out::print); // 1423
stm.forEach(System.out::print); // error 一个Stream只能被使用一次
// 数组映射 返回Stream的方法可以链式调用
Arrays.stream(arr)
.map(e -> e * 2)
.map(e -> e + 1); // 3957
// 累积计算
Arrays.stream(arr).reduce(0, (left, right) -> left + right); // 10
// 平均值
Arrays.stream(arr).average().getAsDouble() // 2.5