1.String
String的作用:
- 字符串拼接:将多个字符串连接在一起
- 字符串比较:判断两个字符串是否相等或大小关系
- 字符串查找:在字符串中查找特定字符或子串
- 字符串替换:用一个新的字符串替换原有的字符串内容
- 字符串截取:从一个字符串中截取出部分内容
- 字符串转换:将字符串转换为其他数据类型或格式
String的默认值是null,不是“”,是java的一个类,也是引用类型。
String类是不可变的,即一旦创建了String对象,其内容就不能被修改。String对象的内容在创建后不可更改,任何对String对象的操作都会返回一个新的String对象,而不会修改原始对象的内容。
String声明和拼接等常用方法:
package com.easy718;
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources_sv;
import java.util.Arrays;
public class EasyString {
//字符串
//String 类 引用类型
//默认值 null 不是""
public static void main(String[] args) {
//声明字符串
String str="abc";
str=new String();
str=new String("你好");
char[] arr={'a','b',97};
str=new String(arr);
System.out.println(str);//aba
str=String.valueOf(12);
System.out.println(str);//12
//字符串的拼接 + 加号在做字符串拼接和数学运算时,优先级是一样的
//字符串和所有类型相加(拼接)后得到的都是字符串
str="123"+"abc";//123abc
str="123"+23;//12323
str="123"+new Object();//123+这个对象的toString的结果 123java.lang.Object@1b6d3586
str="123"+new int[]{1,2,3};//123[I@4554617c
System.out.println(str);
System.out.println("123"+123+123);//123123123
System.out.println(123+123+"123");//246123
//字符串比较 使用equals方法比较字符串
boolean bool="123".equals("123");//true
bool=new String("123").equals(new String("123"));//true
//String类型中的常用方法
//valueof 将传入的参数转成字符串
String.valueOf('1');//将所有类型转换成字符串
test(null);//String 就近,找更精确的
//查找子串出现的位置(index)
int index="123456".indexOf("34");//返回3的下标(2) 子串第一个地址,找不到就返回-1
System.out.println(index);
index="123123123".indexOf("1");//0
index="123123123".lastIndexOf("1");//6
//查找指定位置的字符
char item="123456".charAt(4);//返回5
System.out.println(item);
//截取字符串 包含开始下标,不包含结束下标(左闭右开)
str="123456".substring(1);//返回23456
str="123456".substring(1,4);//返回234
//替换
str="123456".replace("34","aaa");//返回12aaa56
str="123456123456".replace("34","aaa");//返回12aaa5612aaa56
str="12.312.3".replaceAll(".","A");//返回AAAAAAAA,在正则表达式中.代表任意字符
System.out.println(str);
//分割
String[] strArr="1231231231".split("1");//[, 23, 23, 23] 1在前面形成空串,在最后没事
strArr="1231231231".split("2");//[1, 31, 31, 31]
System.out.println(Arrays.toString(strArr));
//字符串长度 length()
int l="123123".length();
//去除前后空白位 空格 \n \r \t \f trim
str="\n\r \t 12 3 \n\r";
System.out.println(str);
System.out.println("-----------------");
System.out.println(str.trim());
//大写 小写 (针对字母)
str="123abc".toUpperCase();//转成大写
System.out.println(str);
str="123ABC".toLowerCase();//转成小写
//判断是否是空串
bool = "123".isEmpty();//是空串返回true
System.out.println(bool);//false
if (!str.isEmpty()){
//str中有内容就要执行此代码 记得取反,因为空串返回true
}
"123456".startsWith("12");//true
"123456".endsWith("65");//false
}
public static void test(String str){
System.out.println("String");
}
public static void test(Object obj){
System.out.println("Object");
}
}
String的对象再定义后就不可改变(常量)private final修饰的value数组
字符串常量池 容器 重用 字符串怎么样加入常量池中---使用量的方式声明的字符串就会加入到常量池
package com.easy718;
public class EasyStringB {
public static void main(String[] args) {
//String 对象 定义后就不可改变 (常量) private final修饰value数组
//字符串常量池 容器 重用
//字符串怎么样加入常量池中---使用量的方式声明的字符串就会加入到常量池中
int a=12;
String str="abc";
Integer.valueOf("23");//双引号中的就是量的形式
char[] arr={'a','b','c'};
str=new String();
//程序中第一次使用量的形式定义"123"。会将这个字符串对象存入字符串常量池中
//之后再使用量的形式使用该对象,就执行使用常量池中的对象
String strA="123";
String strB="123";
System.out.println(strA==strB);//true B的量是从池中得到的
String strC=new String("123");
String strD=new String("123");
System.out.println(strA==strC);//false
System.out.println(strC==strD);//false
String strE="12"+"3";//javac时解析成"123"
String strF="1"+"2"+"3";//javac时解析成"123"
String item="12";
String strG=item+"3";//item是变量,只有运行时才能获得值(调用方法也一样)
String strGG=item+3;
System.out.println(strG==strGG);//false
String strH="12"+3;//和"12"+"3"一样
System.out.println(strA==strE);//true
System.out.println(strA==strF);//true
System.out.println(strE==strF);//true
System.out.println(strA==strG);//false
System.out.println(strA==strH);//true
System.out.println(strC==strG);//false
final String aa="12";
String strI=aa+"3";//常量不可变,结果明确,常量计算优化,解析时就是"123"
System.out.println(strA==strI);//true
String bb=new String("12");//构造方法,只有运行时才知道12
String strJ=bb+"3";
System.out.println(strA==strJ);//false
//intern
str.intern();
//返回 str对象在字符串常量池中的副本对象
//过程:检查str是否在字符串常量池中存在副本
//如果不存在,就复制一份存入到常量池中,然后返回常量池中的副本对象
//如果已经存在副本对象,直接返回副本对象
//所以两个字符串 equals为true 那么两个字符串的intern方法是==
strA=new String("123123");
strB=new String("123123");
System.out.println(strA.equals(strB));//true
System.out.println(strA==strB);//false
System.out.println(strA.intern()==strB.intern());//true
//new String (“abc")创建了几个对象
//一个或两个,看abc是否在常量池中有副本
}
}
StringBuffer和StringBuilder的作用和区别
StringBuffer:StringBuffer是线程安全的,所有的方法都是同步的,适合在多线程环境下使用。由于线程安全的特性,性能相对较低,适合在多线程环境下进行字符串操作。
StringBuilder:StringBuilder是非线程安全的,性能比StringBuffer更高,适合在单线程环境下使用。由于非线程安全的特性,StringBuilder的操作速度更快,适合在单线程环境下进行频繁的字符串操作。
package com.easy718;
import java.util.Objects;
public class EasyStringBuilder {
public static void main(String[] args) {
//String 字符串定义后就不可改变 存在常量池中
String str="";
for (int i = 0; i <10 ; i++) {
str=str+i;
}
//0 1 12 123 1234....
//再拼接字符串时不要产生中间串 StringBuilder StringBuffer(线程安全)
StringBuilder strB=new StringBuilder();
strB.append("123");
strB.append("abc");
strB.append("456");
//调用append 往StringBuilder数组中追加字符串
//其中没有产生字符串对象
System.out.println(strB.toString());
//StringBuilder 默认容量是多少 16
//StringBuilder 扩容
//追加字符时容量不够就需要扩容(value) 默认 原来的容量*2+2
//System.arraycopy(0,5,2,4,4,);
}
}
2.Math
floor - 向下取整:floor方法用于将一个浮点数向下取整到最接近的整数。无论小数部分有多小,floor都会将其向下取整到比原始值小的整数。
round - 四舍五入:round方法用于将一个浮点数四舍五入到最接近的整数。它会根据小数部分的值决定是向上还是向下舍入。
ceil - 向上取整:ceil方法用于将一个浮点数向上取整到最接近的整数。即使小数部分非常小,ceil也会将其向上取整到比原始值大的整数。
random - 生成随机数:random方法用于生成一个0到1之间的随机浮点数。
package com.easy718;
import java.util.Random;
public class EasyMath {
public static void main(String[] args) {
//随机数
double ran=Math.random();//[0,1)随机数
// ran*50 0-50 [8-90) [0-82)+8
double ranNum=ran*82+8;
//随机数对象
Random ranObj=new Random(12);
Random ranObjN=new Random(12);
int a=ranObj.nextInt();
int b=ranObjN.nextInt();
System.out.println(a+"-----------"+b);//-1160101563------------1160101563
a=ranObj.nextInt();//假随机
b=ranObjN.nextInt();
System.out.println(a+"-----------"+b);//256957624-----------256957624
ranObj.nextInt(200);//200是设定的界限
//取整
ranNum=12.5;
//四舍五入 是double返回long,是float返回int
long num=Math.round(ranNum);//13
System.out.println(num);
Math.round(12.33f);//返回int
//向上取整
double ceilNum=Math.ceil(ranNum);//13.0
System.out.println(ceilNum);
//向下取整
double floorNum=Math.floor(ranNum);//12.0
System.out.println(floorNum);
}
}
3.Date
Date类用于表示一个特定的时间点,精确到毫秒级别。Date类主要用于获取、表示和操作日期和时间。
date是long类型,从1970-1-1 00:00:00开始计时,每一毫秒加一。
package com.easy718;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
public class EasyDate {
//long 1970-1-1 00:00:00开始计时,每一毫秒加一
//实际类型 Date
public static void main(String[] args) {
//获取当前时间 --执行时的时间
Date date =new Date();//传入long值时,表示时间戳
System.out.println(date);
long time=date.getTime();
System.out.println(time);
time=time+2*24*60*60*1000;//两天后 的时间
date=new Date(time);
System.out.println(date);
date.getMonth();//0-11
System.out.println(date.getMonth());//7月输出6
//时间格式化
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String sdfStr=sdf.format(date);
System.out.println(sdfStr);
LocalDateTime ldt =LocalDateTime.now();
ZonedDateTime zdt =ZonedDateTime.now();//时区
Object obj =zdt.getZone();
System.out.println(obj);
}
}