10.1 数学类
10.1.1 Math类
- 提供的API:
- 获取字符串长度:length();
- 案例 ```java package com.qfedu.nz203.test1;
public class Test2 { public static void main(String[] args) { //字符串是不可改变的 对其值进行修改 就将创建一个新的对象 产生新的实例 String str=”abc”; str=”abc”+”abc”; //以上代码 会创建两个对象 第一次就是 abc //第二次 abc+abc 重新将 str这个引用指向 abcabc 不再指向原来的 //原来的abc依然存在内存中 等待垃圾回收
String str1="abcdefg";//不要跟数组的属性 length搞混 方法有括号System.out.println(str1.length());}
}
- 比较字符串内容:equals();
- 忽略大小写比较:equalsIgnoreCase();
- 案例
```java
package com.qfedu.nz203.test1;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入你的用户名,不区分大小写");
String userName=input.next();
System.out.println("请输入你的密码,不区分大小写");
String pwd=input.next();
if(userName.equalsIgnoreCase("tom") && pwd.equalsIgnoreCase("abcd")) {
System.out.println("登录成功");
}else {
System.out.println("用户名密码错误");
}
}
}
- 转换成小写:toLowerCase();
- 转换成大写:toUpperCase();
- 案例
package com.qfedu.nz203.test1;
public class Test6 {
public static void main(String[] args) {
String str1 = "abcdefg";
String upperStr = str1.toUpperCase();
System.out.println(upperStr);
String str2 = "HIJKLMN";
String lowerCase = str2.toLowerCase();
System.out.println(lowerCase);
}
}
- 拼接字符串
- 案例 ```java package com.qfedu.nz203.test1;
public class Test8 { public static void main(String[] args) { String str1 = “abc”; String str2 = “def”; String str3 = str1 + str2; String str4 = str1.concat(str2);//参数为需要拼接的字符串 返回值是拼接以后新的字符串 System.out.println(str1 + str2); System.out.println(str4); } }
- 查找第一个字符所在字符串中的索引:indexOf();
- 案例
```java
package com.qfedu.nz203.test1;
public class Test9 {
public static void main(String[] args) {
// indexOf() 参数支持 int 支持String
// 找到参数在当前字符串中的下标 找到就返回一个下标 从0开始
// 如果没有找到 返回-1
String str1 = "abcdedfgdhidjklmn";
System.out.println("字母d的下标"+str1.indexOf("d"));
String str2="学java,到千锋,千锋教育找帅栋";
System.out.println("千字的下标是"+str2.indexOf("千"));
String str3="abcdefg";
//int类型的值是ASCII码或者 unicode编码
System.out.println(str3.indexOf(98));
String str4="中国千锋教育";
System.out.println(str4.indexOf(20013));;//20013对应的是unicode编码中 中 字 的十进制
System.out.println(str4.indexOf(22269));
}
}
- 查找最后一个在字符串中所在索引:lastIndexof();
- 截取字符串:subString();
- 案例: ```java package com.qfedu.nz203.test1;
public class Test11 { public static void main(String[] args) { // subString 截取字符串 支持传入一个索引 // 也支持传入两个索引 一个开始的 一个结束的 String str1 = “abcdefghijklmn”; System.out.println(str1.substring(3));
String str2 = "xzyhijkdmvb";
// 包前不包后
System.out.println(str2.substring(1, 5));
}
}
- 去除前后空格:trim();
- 分割字符串:split();//返回一个字符数组
- 案例:
```java
package com.qfedu.nz203.test2;
public class Test2 {
public static void main(String[] args) {
String song="长亭外 古道边 芳草碧连天 晚风拂柳 笛声残 夕阳山外山";
//split 分割字符串 参数为分割的条件 返回的是一个String的数组
String [] str=song.split(" ");
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}
}
- 将字符串的某个元素转换为char字符
- 案例: ```java package com.qfedu.nz203.test2;
public class Test2 {
public static void main(String[] args) {
String song=”长亭外 古道边 芳草碧连天 晚风拂柳 笛声残 夕阳山外山”;
//split 分割字符串 参数为分割的条件 返回的是一个String的数组
String [] str=song.split(“ “);
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}
}
- 判断以什么结尾:endWith();
- 判断以什么开头:startsWith();
- 案例:
```java
package com.qfedu.nz203.test2;
public class Test6 {
public static void main(String[] args) {
//判断字符串名称是否以 ** 结尾的 方法 endsWith
//判断字符串是否以 ** 开头的 方法 startsWith
String str="ancdefg.java";
System.out.println(str.endsWith(".java"));
System.out.println(str.startsWith("abc"));
}
}
10.2.2 常量池
一些引用数据和byte范围内的数值作为引用符号,字符串引用作为引用符号存储在常量池中,将来一些变量或者方法在加载时或者调用时会被栈帧中的动态链接转换成直接引用。
10.3 StringBuffer&StringBulider
10.3.1 区别
- 两个都是可变字符
- StringBuffer是线程安全的、底层的方法都是用syhcronized同步锁修饰的
- StringBulider是线程非安全的
StringBuffer由于线程安全的所以效率较低,StringBuilder是非线程安全的所以效率较高
10.3.2 提供API
获取容量:.capcity();
- 获取长度:length();
- 添加字符串:append();
- 转换成字符:charAt();
- 删除:delete(0,5);
- 插入:insert(0,”dee”);
- 翻转:reverse();
- 案例:
package com.qfedu.nz203.test3;
public class Test2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());
System.out.println(sb.length());
sb.append("abc");
sb.append(false);
//append 方法 支持传入各种参数 但是都是字符串
System.out.println(sb);
sb.append("bnsasadfasfdsasfasdfsadsaa");
System.out.println(sb);
char chars=sb.charAt(0);
System.out.println(chars);
StringBuffer sb1=sb.delete(0, 5);
System.out.println(sb1);
StringBuffer sb2=sb1.insert(0, "哈哈哈");
System.out.println(sb2);
StringBuffer sb3=sb2.reverse();
System.out.println(sb3);
StringBuilder sb4=new StringBuilder();
System.out.println(sb4.capacity());
sb4.append("abc");
System.out.println(sb4);
sb4.insert(0, "哈哈哈");
System.out.println(sb4);
System.out.println(sb4.delete(0, 3));
}
}
10.4 Runtime类
10.4.1 对象的获取
- 它是一个单例模式,不能直接new对象
-
10.4.2 提供的API
获得默认的内存大小:totalMemory();//当前内存1/64;
- 获取空闲内存的大小:freeMemory();
- 获取最大内存的大小:maxMemory();//当前系统的1/4;
- 案例 ```java package com.qfedu.nz203.test1;
public class Test1 { public static void main(String[] args) { //Runtime runTime=new Runtime();//饿汉式的 (饥不择食) 单 例 模式 Runtime runtime=Runtime.getRuntime(); //获取当前空闲内存的方法 字节为单位的 System.out.println(Runtime.getRuntime().freeMemory()/1024/1024);//空闲内存 //总的内存大小 当前电脑内存大小的 1/64 System.out.println(Runtime.getRuntime().totalMemory()/1024/1024); //最大的内存大小 当前电脑内存的 1/4 System.out.println(Runtime.getRuntime().maxMemory()/1024/1024);
}
}
<a name="Acc5D"></a>
### 10.5 System类
<a name="xiERP"></a>
#### 10.5.1 获取对象的方法
- 不能直接new对象
- getProperties,返回一个Properties对象
- 当前信息时一个Map集合,这个集合用来存放的是当前一个虚拟机的信息
- 案例:
```java
package com.qfedu.nz203.test1;
import java.util.Properties;
public class Test2 {
public static void main(String[] args) {
System.out.println();
//垃圾回收的方法 gc
Test1 test1=new Test1();
System.gc();//调用的是Runtime类中的gc 用于垃圾回收 但不是立刻
System.out.println(test1.hashCode());
//1.先获取到所有的信息
Properties pro =System.getProperties();
//2.通过当前对象获取操作系统版本
System.out.println(pro.get("os.name"));
System.out.println(pro.get("java.version"));
System.out.println(pro.get("user.name"));
System.out.println(pro.get("user.dir"));
}
}
10.5.2 提供的一些API
- currentTimeMilis();返回重1970年1月1日至今的一个毫秒数
- System.error.println();用于异常打印
- System.exist();表示退出
- System.gc();表示提醒垃圾回收,并不会立即退出
- System.arrayCopy();用于数组的复制
10.6 日期类Date
10.6.1 构造方法
- 无参构造:获得是当期的日期
- 有参构造:传入的参数是一个Long类型的一个毫秒数
- 案例 ```java package com.qfedu.nz203.test1;
import java.util.Date;
public class Test3 { public static void main(String[] args) { // Date 日期类 位于java.util工具包中的 不要到错 倒为java.sql包的 // 无参的构造方法 Date date1 = new Date(); System.out.println(date1.toString());// 年月日 时分秒 //获取从1970年 1月1日 0点0分0秒 到现在的一个毫秒数 //1秒 1000毫秒 System.out.println(System.currentTimeMillis()); // 有参的构造方法 Date date2 = new Date(System.currentTimeMillis()); System.out.println(date2); System.out.println(“天数”+date2.getDay()); System.out.println(“小时”+date2.getHours()); System.out.println(“月份”+date2.getMonth()); System.out.println(“分钟”+date2.getMinutes()); } }
<a name="IlJvt"></a>
### 10.7 SimpleDateFormat类
<a name="2q6Q4"></a>
#### 10.7.1 作用
主要用来对于日期的格式化
<a name="H9eOK"></a>
#### 10.7.2 构造方法
- 有参构造:传入的参数是“yyyy-MM-dd hh:mm:ss”;
- SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- format(日期对象)方法,直接返回一个字符串。
- 案例
```java
package com.qfedu.nz203.test1;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test4 {
public static void main(String[] args) {
// 日期显示的问题 默认的显示 不符合我们的阅读习惯
// 年月日 时分秒 所以我们可以对日期进行一个格式化
// SimpleDateFormat
// yyyy-MM-dd hh:mm:ss 这个表示我们格式化日期自己指定的格式
// MM 表示月份 mm表示分钟
//用于连写年月日 或者 时分秒字母中间的字符 是可以改变 根据自己的喜好
SimpleDateFormat sdf = new SimpleDateFormat("yyyy | MM | dd hh:mm:ss");
Date date = new Date(System.currentTimeMillis());
System.out.println(date);
System.out.println(sdf.format(date));
}
}
10.8 Calendar日历类
10.8.1 获取对象
- 不能直接new对象
- 可以通过getInstance()获取Calendar实例
108.2 提供的API
package com.qfedu.nz203.test1;
import java.util.Calendar;
public class Test5 {
public static void main(String[] args) {
//因为Date中的很多方法都被弃用了 所以我们在获取一些 天数 月份 小时的时候
//可以使用jdk给我们推荐的 Calendar 日历类
//这个类也不能直接new对象 通过一个静态方法
Calendar c=Calendar.getInstance();
System.out.println("一个月中的第几天"+c.get(Calendar.DAY_OF_MONTH));
System.out.println("一周中的第几天"+c.get(Calendar.DAY_OF_WEEK));
System.out.println("一年中的第几天"+c.get(Calendar.DAY_OF_YEAR));
System.out.println("一天中的第几个小时"+c.get(Calendar.HOUR_OF_DAY));
System.out.println(c.get(Calendar.MONTH)+1);
System.out.println(c.get(Calendar.MINUTE));
System.out.println(c.get(Calendar.SECOND));
}
}
