2022-7-28 第一小组 甘源册 学习笔记
目录
1.Java的值传递
Java本质上是只有值传递,所有的赋值传参都是一次值 的拷贝
引用数据类型拷贝的就是引用地址,基本数据类型拷贝的就是值,不会传入实例对象
-
出现new 就是创建了新的地址,值就会被拷贝成一份新的
-
private String name; public Ch01(String name) { this.name = name; } public static void ch(Ch01 ch01){ ch01=new Ch01("十大"); } public static void ch(Integer ch01){ ch01=10; } public static void main(String[] args) { Ch01 daf = new Ch01("daf"); ch(daf); //毫无用处 System.out.println(daf.name); //daf Integer i=10; ch(i); //毫无用处 System.out.println(i); //10 }
2.常用API
API(Application program Interface )应用程序接口
- JDK给我们提供的一些已经写好的类,我们可以直接调方法来解决问题
- 我们类的方法,在宏观是都可以称为接口。
2.1时间API
时间戳:1970.1.1 00:00:00 (格林尼治时间)到今天的毫秒数(在全世界都是固定的)
//获取时间戳
long l = System.currentTimeMillis();
2.2日期类(Date)
Date date=new Date(1658974879083l);
Date date1 = new Date();
System.out.println(date.compareTo(date1));
//当返回负数时 调用者时间是在参数时间之前
System.out.println(date.compareTo(date));
//当返回0时 调用者时间与参数时间相同
System.out.println(date1.compareTo(date));
//当返回正数时 调用者时间是在参数时间之后
2.3日历类(Calendar)
Calendar是一个抽象类,不能初始化
- 提供了一组对“年月日时分秒星期”等信息的操作函数。操作不同的时区的信息。
- JDK1.1版本开始,在处理时间和日期是,系统推荐使用Calendar类。(Calendar比Date强大的多)
2.4时区类(TimeZone)
-
Calendar calendar = new GregorianCalendar(); //向上转型 Calendar instance = Calendar.getInstance(); //向上转型 System.out.println(instance.get(Calendar.DAY_OF_WEEK)); //获取当前时区id System.out.println(TimeZone.getDefault()); //设置时区 calendar.setTimeZone(TimeZone.getTimeZone("GMT-8:00")); System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
2.5日期格式化(SimpleDateFormat)
-
format方法 :格式化Date类型 把Date转换成String
-
String s = format.format(new Date()); System.out.println(s);
-
-
pares方法 :把String类型转换成Date类型---从客户端传过来的时间参数都是String类型,存入数据库
-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH-mm-ss"); Date parse = null; try { parse = simpleDateFormat.parse(str); } catch (ParseException e) { System.out.println("请按格式输入(YYYY-MM-dd HH-mm-ss)"); }
-
2.6日期时间类(JDK8)
- Instant
- LocalDate
- LocalTime
- LocalDateTime
- DateTimeFormatter
2.6.1 Instant类
Instant now = Instant.now();
//获得当前时区的时间
System.out.println(now.atZone(ZoneId.systemDefault()));
//获得1970到现在的毫秒数
System.out.println(now.toEpochMilli());
long l = System.currentTimeMillis();
//把时间戳转换成时间
System.out.println(Instant.ofEpochMilli(l));
//把字符串转换成时间
String s="2022-07-28T06:06:33.324Z";
Instant parse = Instant.parse(s);
System.out.println(parse);
//在当前时间下进行加天,月,年-----返回新的对象
System.out.println(now.plus(Duration.ofDays(7)));
System.out.println(now);
2.6.2 LocalDate类
LocalDate now = LocalDate.now();
//输出日期
System.out.println(now); //2022-07-28
//指定日期
LocalDate of = LocalDate.of(1028, 9, 10);//2022-09-10
System.out.println(of);
//判断是不是闰年
System.out.println(of.isLeapYear());
2.6.3LocalTime类
LocalTime now = LocalTime.now();
System.out.println(now);
2.6.4LocalDateTime类
LocalDateTime now = LocalDateTime.now();
System.out.println(now); //2022-07-28T14:23:27.550674400
2.6.5DateTimeFormatter类
- 单例模式——效率高
2.7数学类(Math)
里边的方法都是静态方法,调用时直接类名.方法名
//1~100随机数
System.out.println((int) (Math.random() * (100-1)+1));
ceil() //向上取整
floor() //向下取值
round() //取整
2.8统计类(BigDecimal)
Double d1=0.1;
Double d2=0.7;
System.out.println(d1+d2); //0.7999999999999999
BigDecimal bigDecimal = new BigDecimal(d1);
BigDecimal bigDecimal3 = new BigDecimal(d2); System.out.println(bigDecimal.add(bigDecimal3));//0.7999999999999999611421941381195210851728916168212890625
BigDecimal bigDecimal1 = new BigDecimal("0.1");
BigDecimal bigDecimal2 = new BigDecimal("0.7");
System.out.println(bigDecimal1.add(bigDecimal2));//0.8
2.9随机类(Random)
Random random = new Random();
//0~110的整数随机数---伪随机数
System.out.println(random.nextInt(110));
2.10数组工具类(Arrays)
int[] integers = new int[]{1,2,3,4,5};
//二分法查找
System.out.println(Arrays.binarySearch(integers,4));
//数组拷贝
int[] ints=Arrays.copyOf(integers,22);
//数组排序
Arrays.sort(integers);
2.11系统类(System)
//强制退出
System.exit(-1);
//错误信息
System.err.println("的撒法");
//时间戳
System.out.println(System.currentTimeMillis());
//键值对集合
Properties properties = System.getProperties();
System.out.println(properties);
2.12方法类(Objects)
- JDK1.7
//判断是不是空
System.out.println(Objects.isNull(""));
2.13可变的字符序列(StringBuffer)
同步的——
- 缺点:效率低
- 优点:安全
StringBuffer stringBuffer=new StringBuffer("尼斯");
//不可以用=赋值
System.out.println(stringBuffer); //尼斯
//追加
System.out.println(stringBuffer.append(",我好").append("大家好"));//尼斯,我好大家好---在原有的追加
System.out.println(stringBuffer);//尼斯,我好大家好
//追加
String s="hello";
System.out.println(s.concat(",wor"));//生成了一个新的变量
System.out.println(s);
System.out.println(stringBuffer.delete(4,6));//不包含终止位置
stringBuffer.deleteCharAt(0); //单个删除
stringBuffer.reverse(); //反转
2.14可变的字符序列(StringBuilder)
功能与StringBuffer相同
区别
- 异步的——
- 缺点:不安全
- 优点:效率高
开发三大准则:成本——质量——进度~~~~进度——进度——进度
面试题
三个字符串的区别?
本文作者:(≧∇≦)(≧∇≦)(≧∇≦)
本文链接:https://www.cnblogs.com/gycddd/p/16529717.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
2022 Java实训
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步