Java语法基础
关键字保留字
abstract boolean break byte case catch char class continue default do double else enum extends final finally float for if implements import instanceof int interface long native new null
package private protected public return short static super switch this throw throws try void volatile while
命名规范
包的命名:小写、点分隔符之间有且有一个自然语义的英语单词、统一单数形式
类的命名:大写开头、最好一个单词,驼峰命名、尽量不省略
接口:跟类很类似。
字段变量:小写开头,可以包括多个单词,但是第一个单词小写,后面驼峰、类型与中括号挨着表示数组(int[] arrayDemo)
常量:全部大写,连接使用_,MAX_PRIRITY
函数:跟字段变量相同
注释
单行注释://
多行注释:/…/ 但还是推荐使用多个//
数据类型
byte short int long float double//数值类型
char //字符型
boolean//布尔类型
数组
接口
类 引用类型
数据类型的转换
两种数据类型彼此兼容,目标类型的取值范围大于源数据类型
public static void main(String[] args) {
float price1 = 10.9f; // 定义牙膏的价格
double price2 = 5.8; // 定义面巾纸的价格
int num1 = 2; // 定义牙膏的数量
int num2 = 4; // 定义面巾纸的数量
double res = price1 * num1 + price2 * num2; // 计算总价
System.out.println("一共付给收银员" + res + "元"); // 输出总价
}
byte b = 50;
b = b * 2; // Type mismatch: cannot convert from int to byte
当表达式求值时,操作数自动提升了变量,为int,然后将int类型赋值给一个byte类型就违反了操作规范。
public static void main(String[] args) {
float price1 = 10.9f;
double price2 = 5.8;
int num1 = 2;
int num2 = 4;
int res2 = (int) (price1 * num1 + price2 * num2);
System.out.println("一共付给收银员" + res2 + "元");//44,强制类型转换直接去掉小数点后面的数字。
}
运算符
- 算术运算符
+,-,*,/,%,++,–:加减乘除取余自加自减
System.out.println(10.0/0.0); Infinity
System.out.println(0.0/0.0);NaN
- 关系运算符
,<,>=,<=,!=,==:大于小于,大于等于,小于等于,不等于,相等
- 位运算符
- 逻辑运算符
&&||! 很少只用&|因为无论第一个条件是否为真,还是会判断第二个。 - 赋值运算符
- 三元运算符:(a<b)?a:b
流程控制语句
if else语句
if(condition){
//this is process
}else{
//this is other
}
if (condition){
//
}else if(condition){
//
}else{
//
}
switch语句
switch(condition){
case alpha:break//
case beta:break//
default ://
}
forloop
for(int i=0;i<arrayList.length;i++){
System.out.println(arrayList[i])
}
foreach
for(int ele:arrayList){
System.out.println(ele)
}
while 使用
while(condition){
//循环体
//注意condition的处理,防止死循环!!!
}
do{
//进行处理
}while(condition);
break
continue
这两个跟其他语言都是类似的(python),break跳出当前循环,continue跳出当前condition进行处理;进入下一次循环
java中的final修饰的变量对于基本类型是不可变得,
final int value=10;
如果继续
valule=11;//这里就会出现错误
final String[] array=new String[]{"abc","xyz","ijk"};
array[0]="temp";//这里是没有问题的
array= new String[]{"ttt","sss"};//这里是不行的,
这里的final 对于值类型的是不可变得,对于类型的就是内存地址不能修改,但是内存地址上的数据是可以修改的
已经对比过ArrayList,HashSet,HashMap,对于这几种(List,Set,Map)这几种类型的只要不重新赋值,直接在原来的内存地址上进行修改是可以;
这点有点类似于python的tuple元组,
验证的过程
final ArrayList<String> alphaArrayList = new ArrayList<>(List.of("xyz", "abc", "ijk", "mnk", "abc"));
System.out.println(alphaArrayList);
alphaArrayList.set(0, "temp");
System.out.println(alphaArrayList);
final HashSet<String> alphaHashSet = new HashSet<>(alphaArrayList);
System.out.println(alphaHashSet);
alphaHashSet.add("chenzi");
System.out.println(alphaHashSet);
//
class User {
public String key;
public int value;
User(String key, int value) {
this.key = key;
this.value = value;
}
}
//这里通过创建一个类,来将ArrayList转换成HashMap,
List<User> users = new ArrayList<>();
ArrayList<String> keys = new ArrayList<>(List.of("abc", "xyz", "mnk"));
ArrayList<Integer> values = new ArrayList<>(List.of(11, 32, 55));
for (int index = 0; index < values.size(); index++) {
users.add(new User(keys.get(index), values.get(index)));
}
System.out.println(users.size());
final Map<String, Integer> userDict = users.stream().collect(Collectors.toMap(user -> user.key, user -> user.value));
System.out.println(userDict);
userDict.replace("abc",999);
System.out.println(userDict);
Map<String, Integer> userDictBeta = new HashMap<>();
for (int index = 0; index < keys.size(); index++) {
userDictBeta.put(keys.get(index), values.get(index));
}
System.out.println(userDictBeta);