本文主要介绍java中实用类中包含的常用方法,使用这些方法可在JavaAPI中进行查询。
目录
七、String类、StringBuffer类和StringBuilder的区别
一、String中常用方法
String string1 = "qwertyuiop";
//获取字符串的长度
int length =string1.length();
System.out.println("字符串长度:"+length);
//比较两个字符串的内容是否相同
String str1 = "qwe";
String str2 = "qwe";
System.out.println(str1.equals(str2));//true
System.out.println(str1==str2);//true
//其它比较字符串的方法
String str5 = "qwert";
String str6 = "QWERT";
System.out.println(str5.equals(str6));
//equalsIgnoreCase()不区分大小写比较
System.out.println(str5.equalsIgnoreCase(str6));//true
String result = str6.toLowerCase();
System.out.println(str6);//QWERT
System.out.println(result);//qwert
System.out.println(str5.toUpperCase());
//字符串常用提取方法
String str1 = "qwertyuiopqwer";
System.out.println(str1.length());
//indexOf(int ch) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
//indexOf(String value) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
int index = str1.indexOf(110);
System.out.println(index);
System.out.println(str1.indexOf("qwer"));
//lastIndexOf(int ch) 搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
//lastIndexOf(String value)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
System.out.println(str1.lastIndexOf("yu"));
System.out.println(str1.lastIndexOf("qw"));
// substring(int index)提取从位置索引开始的字符串到末尾部分
System.out.println(str1.substring(8));
//substring(int beginindex, int endindex)提取beginindex(包括)和endindex(不包括)之间的字符串部分
System.out.println(str1.substring(2, 7));
//trim()返回一个前后不含任何空格的调用字符串的副本
String str2 = " qwert yuiop ";
System.out.println(str2);
System.out.println(str2.trim());
//字符串拆分方法
String str3 = "长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
String[] strs = str3.split(" ");
for (String string : strs) {
System.out.println(string);
}
String str4 = "我爱你但是你不爱我可是我依然爱你";
String[] strs2 = str4.split("爱");
System.out.println(Arrays.toString(strs2));
//判断字符串是否以指定内容结尾
String str5 = "HelloWorld.java";
System.out.println(str5.endsWith(".java"));
//判断字符串是否以执行内容开头
System.out.println(str5.startsWith("He"));
//将字符串变成byte类型的数组
byte[] bytes = str5.getBytes();
for (byte b : bytes) {
System.out.print((char) b);
}
}
二、日期类
// 系统当前时间(精确到毫秒)
Date nowTime = new Date();
System.out.println(nowTime);
// 日期格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss SSS");
System.out.println(sdf.format(nowTime));
// 日期字符串转换成Date类型
String time = "2000-1-1 01:01:01 111";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyy-MM-dd HH:mm:ss SSS");
Date dateTime = sdf1.parse(time);
System.out.println(dateTime);
// 获取自1970年1月1日00: 00:00 000到当前系统时间的总毫秒数。
// 1秒=1000毫秒
long nowTimeMillis = System.currentTimeMillis();
System.out.println(nowTimeMillis);
// 执行方法前记录一个毫秒数
long begin = System.currentTimeMillis();
print();
// 执行方法后记录一个毫秒数
long end = System.currentTimeMillis();
System.out.println("耗费时长:"+(end-begin)+"毫秒");
}
public static void print(){
for(int i = 0;i<1000000000;i++){
// System.out.println("i = "+i);
}
}
三、Math类
//Math类:Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
double pi =Math.PI;
System.out.println(pi);
System.out.println(Math.max(100, 200));
System.out.println(Math.min(99, 199));
System.out.println(Math.abs(-10));
System.out.println(Math.floor(9.8));//返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。
System.out.println(Math.ceil(9.1));//返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
System.out.println(Math.round(10.5));//返回最接近参数的 int。
System.out.println(Math.round(-11.6));
//随机数方法:Math.random():随机返回一个介于0.0(包括)~1.0(不包括)之间的double类型的数据
double num1 =Math.random();
System.out.println(num1);
/*随机返回一个[num1,num2)之间的int类型的整数(num2>num1)
* int num = (int)(Math.random()*(num2-num1)+num1);
*/
四、包装类
基本数据类型 包装类型
byte java.lang.Byte
short java.lang.Short
int java.lang.Integer
long java.lang.Long
float java.lang.Float
double java.lang.Double
boolean java.lang.Boolean
char java.lang.Character
1.装箱
基本数据类型-(转换为)->引用数据类型(装箱)
Integer i = new Integer( 123);
2.拆箱
将引用数据类型--(转换为)->基本数据类型
int retValue = i.intValue();
System.out.println(retValue);
3.自动装箱
基本数据类型--(自动转换)-->包装类型:自动装箱
Integer x = 900;
System.out.println(x);
4.自动拆箱
包装类型―--(自动转换)-->基本数据类型:自动拆箱
int y = x;
System.out.println(y);
五、枚举类
枚举:一枚一枚可以列举出来的,才建议使用枚举类型。
枚举类是由一组固定的常量组成的类型
public static void main(String[] args) {
Result retValue = divide(1,1);
System.out.println(retValue == Result.SUCCESS ? "计算成功" : "计算失败");
}
public static Result divide(int a, int b){
try{
int c = a/b;
return Result.SUCCESS;
}catch(Exception e){
return Result.FAIL;
}
}
enum Result{
// SUCCESS是枚举Result类型中的一个值
// FAIL是枚举Result类型中的—个值
// 枚举中的每一个值,可以看做是常量”
SUCCESS,FAIL
}
六、StringBuffer类
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一串数字:");
String num = sc.next();
StringBuffer sb = new StringBuffer(num);
for(int i = sb.length()-3;i>0;i -= 3){
// 在字符串中插入其它字符
sb.insert(i,",");
}
System.out.println(sb);
}
七、String类、StringBuffer类和StringBuilder的区别
1.String类是一个不可变类
StringBuffer与StringBuilder都继承自AbstractStringBuilder类,
在AbstractStringBuilder中也是使用字符数组保存字符串,是可变类。
2.当创建String对象时,可以利用构造方法String str = new String("Java")的方式来对其进行初始化,
也可以直接用赋值的方式String s = "Java"来初始化。
而StringBuffer只能使用构造方法StringBuffer sb = new StringBuffer("hello")的方式初始化。
3.StringBuffer和StringBuilder在修改字符串方面比String的性能要高。
String需要重新创建对象
4.StringBuilder是线程不安全的,StringBuffer是线程安全的
5.在执行效率方面,StringBuilder最高,StringBuffer次之,String最低,
对于这种情况,一般而言,如果要操作的数量比较小,应优先使用String类;
如果是在单线程下操作大量数据,应优先使用StringBuilder类;
如果是在多线程下操作大量数据,应优先使用StringBuilder类。