Java 基础知识易错记录
1、++运算符
2、continue和break
3、成员变量 局部变量
4、switch case
5、StringBuffer StringBuilder
6、重载 重写
7、throw throws
8、finally catch
9、数组创建
10、抽象类和接口
11、泛型
1、++运算符
public static void main(String[] args) {
int a = 1;
System.out.println(a++);
System.out.println(++a);
}
++在后,后加
++在前,先加
2、continue和break
continue:跳过,根据判断条件跳过这一次循环继续下一次
break:终止,根据判断条件达到条件终止当前循环
continue
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
break
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
3、成员变量 局部变量
4、switch case
5、StringBuffer StringBuilder
StringBuffer :可变字符序列,线程安全
StringBuilder : 可变字符序列,线程不安全,相对于StringBuffer更快
6、重载 重写
7、throw throws
throw 关键字用于在代码中抛出异常,而 throws 关键字用于在方法声明中指定可能会抛出的异常类型
public void test() throws Exception {
throw new Exception();
}
一个方法可以声明抛出多个异常,多个异常之间用逗号隔开
import java.io.*;
public class className
{
public void withdraw(double amount) throws RemoteException,
InsufficientFundsException
{
// Method implementation
}
//Remainder of class definition
}
8、finally catch try
9、数组创建
格式:
// 首选的方法
dataType[] arrayRefVar;
// 效果相同,但不是首选方法
// dataType arrayRefVar[] 风格是来自 C/C++ 语言 ,在Java中采用是为了让 C/C++ 程序员能够快速理解java语言
dataType arrayRefVar[];
示例:
// 首选的方法
double[] myList;
// 效果相同,但不是首选方法
double myList[];
静态创建:
int[] a = {1,2,3};
Object[ ] objects= {new Object(),new Object()};
动态创建:
int[] a = new int[2];
a[0]=1;
a[1]=2;
二维数组创建:
//type 可以为基本数据类型和复合数据类型
//typeLength1 和 typeLength2 必须为正整数
//typeLength1 为行数,typeLength2 为列数
type[][] typeName = new type[typeLength1][typeLength2];
示例:
int a[][] = new int[2][5];
String[][] s = new String[2][];
//为最高维分配引用空间,也就是为最高维限制其能保存数据的最长的长度
s[0] = new String[2];
s[1] = new String[3];
//为其每个数组元素单独分配空间
s[0][0] = new String("Good");
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("you");
s[1][2] = new String("!");
10、抽象类和接口
11、泛型
<? extends T>和<? super T>的区别
<? extends T>表示该通配符所代表的类型是T类型的子类 <? super T>表示该通配符所代表的类型是T类型的父类 ```bash 737067250(交友) ```