java 笔记--可删
1、类和方法(函数)的命名规则
2、变量命名的方法
总结: 类(首字母大写!!!),函数、变量 首字母小写其余首字母大写
3、注释的方法
//这是一个注释
/*
这是一个多行的注释
这个是多行的注释
*/
4、把看似json的字符变成字典
依赖: https://www.cnblogs.com/kaibindirver/p/15797247.html
import com.alibaba.fastjson.JSONObject;
String json="{name:\"张三\",age:\"18\"}";
JSONObject jo = JSONObject.parseObject(new String(json));
System.out.println(jo.get("name"));
//将javabean转化成json字符串
String jsonStr = JSON.toJSONString(Bean bean);
//将json字符串转化成对应的javabean
Bean bean = JSON.parseObject(jsonStr, Bean.class);
5、try 和 catch用法
方法一:
try {
WebElement txtbox=driver.findElement(By.id("kw2"));
}catch(Exception e) {
System.out.println("看这里"+e);
}
其中 e为try代码执行错误的原因
方法二:
try后不抛异常可以在catch里面放入null # 这个待实验,伟程君说不是这样的
catch(Exception e) {
null
}
方法三:
catch(Exception e) {
e.printStackTrace() ---printStackTrace()方法的意思是:在命令行打印异常信息在程序中出错的位置及原因。
}
注意:
1、如果是内存溢出这种错误 ,要用 Error 才能捕获
6、踩坑点(if判断的时候不要使用== 会判断错误,要使用.equals )
if ("我是谁".equals("我是谁")){
System.out.println(Bb.uu);
}else{
System.out.println(123);
}
if ( ! "我是谁".equals("我是谁")) 不等于 前面加感叹号
7、2边条件都为Ture
if(null !=id2 && id2.equals("苹果")){
};
8、数据类型
Long 是可以为空 long是不可以为空
9、构造函数 (等于python的初始化)
https://www.cnblogs.com/ibelieve618/p/6364541.html
10、遍例数组打印
// 创建出一个数组
List<String> strList = Arrays.asList("YangHang", "AnXiaoHei", "LiuPengFei");
strList.forEach(System.out::println);
11、判断字符类型
https://blog.csdn.net/kuangay/article/details/81563992
1.判断是否为double型
object instanceof Double
2.判断是否是int型
object instanceof Interger
3.判断是否是String型
object instanceof String
用下面这个更方便要判断的变量+.getClass().getName()
jsonArray.getClass().getName()
12、判断字符对应字符在第几位 且进行截断
int position = tager.indexOf("[exodus]");
截断
String targe2=tager.substring(position+9);
参考: https://www.cnblogs.com/bbllw/p/10032631.html
13、判断返回变量是否为空简写 返回为true or false
https://codingdict.com/questions/110969
Objects.isNull(x)
意思就是
x == null
Objects.nonNull(x)
意思就是
x != null
14、避免空指针异常的写法
List<String> list = null;
List<String> newList = Optional.ofNullable(list).orElse(Lists.newArrayList()); //意思是判断list是否为null,如果为null那么就给他赋值一个空的数组
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
参考: https://www.jianshu.com/p/790f7c185d3e
15、操作数组
https://www.cnblogs.com/kaibindirver/p/11825791.html
https://www.cnblogs.com/kaibindirver/p/11911091.html
16、抛运行时异常的方法
if (id.equals("1")){
log.error("id不能为空");
throw new RuntimeException("id不能为空");
}
17、判断数组是否为空的api
if(CollectionUtils.isEmpty(list)){
18、字符类型转换
数字转字符类型
String s2 =String.valueOf(num);
19、操作json
https://www.cnblogs.com/kaibindirver/p/16151749.html
20、替换字符
字符.toString().replaceAll("张三", "替换里斯")
21、正则匹配 返回true or false
字符.matches("\\d+\\.?\\d*"))
.matches("\\d+\\.?\\d*")) 匹配 纯数字 或带小数的数字
// 整数校验
public final static String INT_REGEX_STR = "^[0-9]{1,20}$";
// 字符校验
public final static String STR_REGEX_STR = "^[A-Za-z0-9]{1,100}$";
// 价格校验
public final static String PRICE_REGEX_STR = "^-?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?$";
// 中文、数字、字符校验
public final static String CHINESE_STR_REGEX_STR = "^[A-Za-z0-9\\u4E00-\\u9FA5]{1,100}$";
// 中文校验
public final static String CHINESE_REGEX_STR = "^[\\u4E00-\\u9FA5]{1,100}$";