String类、StringBuffer类、StringBuilder类、Math类,random类,包装类
String类、StringBuffer类、StringBuilder类都用来表示字符串但有一定的区别
1.String是内容不可变的,而StringBuffer,StringBuilder是内容可变的
2.StringBuffer是同步的,数据安全,效率低,StringBuilder是不同步的,数据不安全,效率高
String类
比较字符串
boolean startswith(String a);
对象.startswith(String a);
转换,字符串转整型
static int parseInt(String s);
Integer.parseInt(String s);
转换,字符串转浮点型
static double parseDouble(String s);
float.parseDouble(String s);
定位
int indexOf();
indexOf(char a)或indexOf(String b);
提取指定字符串
String substring(int a,int b);
对象.(int a,int b);
替换
String replace(char old,char new);
String replace(String old,String new);
对象.replace(char old,char new);
对象.replace(String old,String new);
去除字符串两端空格
String trim();
对象.trim();
按字典顺序比较两个字符串
int compareTo(String str);
对象.compareTo(str);
StringBuffer类
为了解决字符串一但创建就不能被改变的问题Java提供了StringBuffer类(字符串缓冲区),线程安全的可变字符序列
注释:string是字符串常量的表示StringBuffer是字符串变量的表示,可以改变字符串的长度
StringBuffer的构造方法
StringBuffer(); 无参构造方法
StringBuffer(int capacity);
注释:构造一个具有指定初始容量的字符串缓冲区
也可以不指定(去掉参数即可)即无参构造方法,其初始容量为16个字符
StringBuffer(String str);
注释:构造一个字符串缓冲区,将其内容初始化为指定的字符串内容
StringBuffer中的两个方法
int capacity(); 返回当前容量
int length(); 返回字符串长度
StringBuffer的添加功能
public StringBuffer append(String str);
把任意类型数据添加到字符串缓冲区,并返回字符串本身
public StringBuffer insert(int offset,String str);
在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
可以链式编程
StringBuffer的删除功能
public StringBuffer delete(int,int);
删除指定范围子串
public StringBuffer deleteCharAt(int);
删除指定位置字符
public StringBuffer delete(0,对象.length());
清空缓冲区
StringBuffer的修改功能
public StringBuffer setCharAt(int,str);
修改指定位置字符
public StringBuffer replace(int ,int,str);
替换指定位置字符
public StringBuffer reverse();
字符串翻转
String转换为StringBuffer的方法
1.构造方法
2.append(String str)方法
StringBuffer转换为String的方法
1.构造方法
2.toString方法(返回序列中数据的字符串表示形式)
3.String substring(int start);
String substring(int start,int end);
System类和Runtime类
System类的一个常用静态长整形方法
static long currentTimeMillis();
更精确的一个方法
static long nanoTime();
单位:好微妙,精确6个单位
使用:
long t1=System.currentTimeMillis();
long t2=System.currentTimeMillis();
时间差 long t3 = t2-t1;
返回一个以毫秒为单位的当前时间(当前时间与协调世界时1970年1月1日午夜之间的时间差,这个时间差以毫秒为单位,格林威治时间,和北京时间差8个小时)
Runtime类——是一个单例模式类
exec()方法能够让当前本机的所有可执行程序在Java中执行
public Process exec(String command)throw IOException
在单独的进程中执行指定的命令
如打开记事本,打开河南科技大学官网Runtime rt = Runtime.getRuntime();
rt.exec("notepad.exe");
rt.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe www.haust.edu.cn");浏览器可运行程度运行路径+网址
Process process=rt.exec("notepad.exe");
//程序休眠3秒
Thread.sleep(3000);
//关闭进程
process.destroy();——这个进程Win11有点问题
Math类和random类
Math类里面的所有方法都是静态的,包括成员变量也是静态的,
两个常量:
圆周率π=3.1415~:Math.PI
自然对数函数的底数e=2.718~:Math.E
绝对值
public static int abs(int a);
向上去整
public static double ceil(double a);
向下去整
public static double floor(double a);
最大值
public static int max(int a,int b);
最小值
public static int min(int a,int b);
public static int min(min(int a,int b),int c);
随机数
public static double random();范围[0.0,1.0)
random();默认种子,当前时间的毫秒值,作为种子,种子数不同,产生的随机数不同
random(long seed);指定种子,每次种子相同,随机数相同
int nextInt()返回int范围内的随机数
int nextInt(int n)返回[0.0,n)范围内的随机数
获取1-100之间中随机整数
(int)(random()*100+1);
四舍五入
public static double round(float a);
正平方根
public static double sqrt(double a);
包装类
8种基本数据类型名首字母大写就是其对应的包装类(除char->Character和int->Integer外)就是8种包装类
把100这个数据的二进制,八进制,十六进制计算出来
int i=100;
System.out.println(Integer.toBinarystring(i));
System.out.println(Integer.to0ctalstring(i));
System.out.println(Integer.toHexstring(i));
判断一个数据是否是int范围内的。首先你得知道int的范围是多力
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
十进制到其他进制
public static String tostring(int i,int radix)由这个我们也看到了进制的范围:2-36
为什么呢?0,...9,a...z 10+26=36
JDK5的新特性
自动装箱:
把基本类型的数据当做对应的包装类的时像使用自动拆箱:把包装类的对象当做对应的基本类型的数据使用
Integer i=new Integer(100);
Integer ii=100;//自动装箱
ii+=20;//包含了自动拆箱和自动装箱操作 ii=ii+20;
System.out.println("ii:"+ii);
//通过反编译后的代码
Integer i = new Integer(100);
Integer ii = Integer.valueof(100);
ii = Integer.value0f(ii.intvalue()+ 20);
System.out.println((new StringBuilder("ii:")).append(ii).toString());