java 常用类

开发工具与关键技术: eclipse 和 Java
作者:杜永鹏
撰写时间:2021 年 6 月 6日

Java常用类
1.1.System类
System类代表当前Java程序的运行平台,程序不能创建System类的对象, System类提供了一些类变量和类方法,允许直接通过System类来调用这些类变量和类方法
常用方法:
修饰符: static 返回值类型: long 方法: currentTimeMillis() 说明: 返回以毫秒为单位的当前时间。
修饰符: static 返回值类型: void 方法: exit(int status) 说明: 终止当前正在运行的 Java 虚拟机。
修饰符: static 返回值类型: void 方法: gc() 说明: 运行垃圾回收器。
修饰符: static 返回值类型: Map 方法: getenv() 说明: 返回一个不能修改的当前系统环境的字符串映射视图。
修饰符: static 返回值类型: String 方法: getenv(String name) 说明: 获取指定的环境变量值。
修饰符: static 返回值类型: Properties 方法: getProperties() 说明: 确定当前的系统属性。
修饰符: static 返回值类型: String 方法: getProperty(String key) 说明: 获取指定键指示的系统属性。
修饰符: static 返回值类型: String 方法: getProperty(String key,String def) 说明: 获取用指定键描述的系统属性。
修饰符: static 返回值类型: int 方法: identityHashCode(Objectx) 说明: 返回给定对象的哈希码,该代码与默认的方法 hashCode() 返回的代码一样,无论给定对象的类是否重写hashCode()。
修饰符: static 返回值类型: long 方法:nanoTime() 说明: 返回最准确的可用系统计时器的当前值,以毫微秒为单位。
例如:
public static void main(String[] args) {
//输出
System.out.println(“输出到控制台”);
System.err.println(“打印错误信息到控制台”);
//输入
Scanner scanner=new Scanner(System.in);

	long t=System.currentTimeMillis();
	System.out.println(t); 
	
	long t2=System.nanoTime();
	System.out.println(t2);
	  
	for (int i = 0; i < 100; i++) {
		System.out.println(System.currentTimeMillis()+"  "
				+System.nanoTime());
	}
	
	//只是通知jvm进行垃圾回收,但是jvm什么时间进行回收,时间不定
	System.gc();//通知jvm 希望进行一次垃圾回收
	
	Map envMap=System.getenv();//获取到的是环境变量
	System.out.println(envMap);

	String envJavaHome=System.getenv("JAVA_HOME");
	System.out.println(envJavaHome);
	
	//确定当前的系统属性
	Properties p1=System.getProperties();
	System.out.println(p1);
	//获取指定键指定的系统属性 
	System.out.println(System.getProperty("os.name"));
	//获取用指定键描述的系统属性 
	System.out.println(System.getProperty("os.name1","默认值"));
	//返回给定对象的哈希码    
	String str="123";
	String str1=new String("123");
	System.out.println(str==str1);
	System.out.println(System.identityHashCode(str));
	System.out.println(System.identityHashCode(str1));
	System.out.println(str.hashCode());
	System.out.println(str1.hashCode());
	
	System.exit(0);//关闭当前java程序
	
}

1.2.Runtime类
Runtime类代表Java程序的运行时环境,可以访问JVM的相关信息,每个Java程序都有一个与之对应的Runtime实例,应用程序通过该对象与其运行时环境相连。应用程序不能创建自己的Runtime实例,但可以通过getRuntime()方法获取与之关联的Runtime对象。
常用方法:
修饰符: static 返回值类型: Runtime 方法: getRuntime() 说明: 返回与当前 Java 应用程序相关的运行时对象。
修饰符: static 返回值类型: int 方法: availableProcessors() 说明: 向 Java 虚拟机返回可用处理器的数目。
修饰符: static 返回值类型: long 方法: totalMemory() 说明: 返回 Java 虚拟机中的内存总量。
修饰符: static 返回值类型: long 方法: freeMemory() 说明: 返回 Java 虚拟机中的空闲内存量。
修饰符: static 返回值类型: long 方法: maxMemory() 说明: 返回 Java 虚拟机试图使用的最大内存量。
修饰符: static 返回值类型: Process 方法: exec(Stringcommand) 说明: 在单独的进程中执行指定的字符串命令。
修饰符: static 返回值类型: void 方法: gc() 说明: 运行垃圾回收器。
修饰符: static 返回值类型: void 方法: exit(int status) 说明: 通过启动虚拟机的关闭序列,终止当前正在运行的 Java 虚拟机。
修饰符: static 返回值类型: void 方法: halt(int status) 说明: 强行终止目前正在运行的 Java 虚拟机。
例如:
public static void main(String[] args) {
// 获取Java程序关联的运行时对象
Runtime rt = Runtime.getRuntime();
System.out.println(“处理器数量: " + rt.availableProcessors());
System.out.println(“Jvm空闲内存数: " + rt.freeMemory() / 1024 / 1024);
System.out.println(“Jvm总内存数: " + rt.totalMemory() / 1024 / 1024);
System.out.println(“Jvm试图使用的最大内存: " + rt.maxMemory() / 1024 / 1024);
// 运行外部程序
try {
rt.exec(“mspaint.exe”);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
// 运行垃圾回收
//System.gc()内部也使用这个方法 Runtime.getRuntime().gc();
rt.gc();
//退出
//rt.exit(0);
rt.halt(0);//强制退出
System.out.println(“exit”);
}
1.3.String类
1.String类常用构造器:
构造器: String() 说明: 初始化一个新创建的 String 对象,使其表示一个空字符序列。
例如:
// String()
System.out.println(”---------String()------------”);
String str = new String();// “”
System.out.println(str);
构造器: String(byte[] bytes)说明: 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
例如:
// String(String original)
System.out.println(”-------String(String original)------”);
String str1 = new String(“String”);
System.out.println(str1);
构造器: String(byte[] bytes, Charset charset)说明: 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的String。

例如:// String(byte[] bytes,Charset charset)
System.out.println("-----String(byte[] bytes, Charset charset)-----");
byte[] bytes = new byte[] { -24, -65, -103, -26, -104, -81, -28, -72, -83, -26, -106, -121};
String str3 = new String(bytes);//默认UTF-8
System.out.println(str3);
构造器: String(byte[] bytes, Stringchar setName)说明: 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的String。
构造器: String(byte[] bytes, intoffset, int length)说明: 通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
构造器: String(byte[] bytes, int offset,int length, Charset charset)说明: 通过使用指定的 charset 解码指定的 byte 子数组,构造一个新的 String。
例如://String(byte[] bytes, Charset charset)
System.out.println("-----String(byte[] bytes, Charset charset)------");
byte[] bytes1 = new byte[]{ -24, -65, -103, -26, -104, -81, -28, -72, -83, -26, -106, -121};
String str4 = new String(bytes,Charset.forName(“GBK”));
System.out.println(str4);
构造器: String(byte[] bytes, int offset,int length, String charsetName)说明: 通过使用指定的字符集解码指定的 byte 子数组,构造一个新的String。
例如: //String(byte[] bytes, String charsetName)
System.out.println("------String(byte[] bytes, String charsetName)-----");
String str5 =new String(bytes2,“GBK”);
System.out.println(str5);

构造器: String(char[] value) 说明: 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
例如://String(char[] value)
System.out.println("------String(char[] value)");
char [] char1 = new char[] {‘这’,‘是’,‘中’,‘文’};
String str9 = new String(char1);
System.out.println(str9);
构造器: String(char[] value, int offset,int count)说明: 分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
例如://String(char[] value, int offset, int count)
System.out.println("-----String(char[] value, int offset, int count)-----");
String str10 = new String(char1,1,2);
System.out.println(str10);
构造器: String(String original)说明: 初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。
构造器: String(StringBuffer buffer) 说明: 分配一个新的字符串,它包含字符串缓冲区参数中当前包含的字符序列。
构造器: String(StringBuilder builder) 说明: 分配一个新的字符串,它包含字符串生成器参数中当前包含的字符序列。
2.String常用判断方法:
返回值类型: boolean 方法名称: equals(Object obj) 说明: 比较字符串的内容是否相同,区分大小写
返回值类型: boolean 方法名称: equalsIgnoreCase(String str) 说明: 比较字符串的内容是否相同,忽略大小写
返回值类型: boolean 方法名称: contains(String str) 说明: 判断大字符串中是否包含小字符串
返回值类型: boolean 方法名称: startsWith(String str) 说明: 判断字符串是否以某个指定的字符串开头
返回值类型: boolean 方法名称: endsWith(String str) 说明: 判断字符串是否以某个指定的字符串结尾
返回值类型: boolean 方法名称: isEmpty() 说明: 判断字符串是否为空。
例如:public static void main(String[] args) {
String str1=“Abc”;
String str2=“ABC”;

	System.out.println("equals(Object obj)");
	System.out.println(str1.equals(str2));//区分大小判断
	
	System.out.println("equalsIgnoreCase(String str )");
	System.out.println(str1.equalsIgnoreCase(str2));
	
	System.out.println("contains(String str)");
	String str3="djkdjkdjkdjdkdjdkdjsdisdruiosdfjmnjsddjhhjsd";
	System.out.println(str3.contains(str1));
	System.out.println(str3.contains(str2));
	
	System.out.println("startWith() endWith()");
	System.out.println(str3.startsWith("dj"));
	System.out.println(str3.endsWith("dj"));
	
	
	System.out.println("isEmpty()");
	//isEmpty() 只能判断空字符串
	System.out.println(str3.isEmpty());
	String str4 = new String();
	str4=null;
	System.out.println(str4.isEmpty());

}

3.String类常用的获取方法:
返回值类型: int 方法名称: length() 说明: 获取字符串的长度。
返回值类型: char 方法名称: charAt(int index) 说明: 获取指定索引位置的字符
返回值类型: int 方法名称: indexOf(int ch) 说明: 返回指定字符在此字符串中第一次出现处的索引。
返回值类型: int 方法名称: indexOf(int ch,int fromIndex) 说明: 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
返回值类型: int 方法名称: indexOf(String str) 说明: 返回指定子字符串在此字符串中第一次出现处的索引。
返回值类型: int 方法名称: indexOf(String str,int fromIndex) 说明: 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
返回值类型: int 方法名称: lastIndexOf(int ch) 说明: 返回指定字符在此字符串中最后一次出现处的索引。
返回值类型: int 方法名称: lastIndexOf(int ch,int fromIndex) 说明: 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
返回值类型: int 方法名称: lastIndexOf(String str) 说明: 返回指定子字符串在此字符串中最右边出现处的索引。
返回值类型: int 方法名称: lastIndexOf(String str,int fromIndex) 说明: 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
返回值类型: String 方法名称: substring(int start) 说明: 从指定位置开始截取字符串,默认到末尾。
返回值类型: String 方法名称: substring(int start,int end) 说明: 从指定位置开始到指定位置结束截取字符串。
例如:public static void main(String[] args) {
String str = “sadfghjk165jhgfj jds256”;

	System.out.println("----length-----");
	System.out.println("length:"+str.length());
	
	System.out.println("-------charAt------");
	System.out.println(str.charAt(5));//索引从0开始
	
	//-1代表没找到
	System.out.println("----indexOf----");
	//indexOf 从左向右
	System.out.println(str.indexOf('j'));
	System.out.println(str.indexOf('j',7));
	System.out.println(str.indexOf("jk"));
	System.out.println(str.indexOf("jk",7));
	
	
	System.out.println("----lastIndexOf----");
	//indexOf 从右向左
	System.out.println(str.lastIndexOf('j'));
	System.out.println(str.lastIndexOf('j',7));
	System.out.println(str.lastIndexOf("jk"));
	System.out.println(str.lastIndexOf("jk",7));
	
	System.out.println("-----substring----");
	String str1=str.substring(5);
	System.out.println(str1);
	String str2=str.substring(5,10);//[startindex,endindex)
	System.out.println(str2);
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值