常用类:字符串、包装类(查阅API Doc文档)、时间日期类、System、Runtime、Math类、properties类
1.字符串:String、String Buffer、String Builder的用法
1.String:
1.语法特殊性:提供了String常量对象
2.内存存放特殊性:String常量对象在加载期就会被产生,放到数据段的字符串常量池中,运行起来后要使用,直接到常量池取!
注意:
String对象内容不可变!
3.提供了大量的字符串操作方法:
1.跟数组有关的方法
2.跟字母有关的方法
3.跟操作技巧有关的方法
4.特殊方法:3个
下列实例:
package com.lovo.test;
import java.util.Scanner;
public class TestString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 语法特殊性--提供了String常量对象
// 内存存放的特殊性---String常量对象在加载期就会被产生,帮到数据段的字符串常量池当中
// ---运行起来以后需要使用,直接到常量池取就可以了
// String对象内容不可变
// String str0 = "hello";
// String str1 = "hello";
// String str2 = new String("hello");
// String str3 = new String("hello");
// String str4 = str2;
// System.out.println(str0 == str1);
// System.out.println(str1 == str2);
// System.out.println(str2 == str3);
// str2 = str2 + "world";
// System.out.println(str2);
// System.out.println(str4);
// 空与空串是不同的
// String tst = null;//空--没对象
// String tst1 = "";//空串--有对象
//
// if(tst != null && !tst.equals("")){
// System.out.println("tst内容非空");
// }
String str = "hello";
// String的方法
// 1、跟字符数组有关的方法
// System.out.println(str.length());//String对象的长度
// char[] array = str.toCharArray();//把String对象转换成char数组
// System.out.println(str.charAt(3));//根据下标得到String对象该下标位置的字符
// System.out.println(str.indexOf('l'));//得到某个字符在String对象当中首次出现的位置
// System.out.println(str.lastIndexOf('l'));//得到某个字符在String对象当中最后一次出现的位置
// 2、跟字母有关的方法
// System.out.println(str.toUpperCase());//得到一个全大写的新String对象
// System.out.println(str.toLowerCase());//得到一个全小写的新String对象
// System.out.println("hello".equalsIgnoreCase(str));//忽略大小写比较相等
// System.out.println("hell".compareTo("hello"));//让两个String对象做字典顺序比较
// System.out.println("Hell".compareToIgnoreCase("hello"));//让两个String对象忽略大小写做字典顺序比较
// 3、跟使用技巧有关系的方法
// System.out.println(str.contains("eo"));//判断一个String对象是否包含另一个子串
// System.out.println(str.startsWith("he"));//判断一个String对象以什么开头
// System.out.println(str.endsWith("lo"));//判断一个String对象以什么结尾
// System.out.println(str.replace('l', 'o'));//将String对象中的某个字符替换成另一个字符
// System.out.println(str.replace("ll",
// "fuck"));//将String对象中的某个子串替换成另一个String对象
// System.out.println(str.replaceAll("ll",
// "fuck"));//将String对象中的某个子串替换成另一个String对象,支持正则表达式
// System.out.println(str.replaceFirst("l",
// "fuck"));//将String对象中首次出现的某个子串替换成另一个String对象
// 4、特殊方法
// 4-1、trim方法,去掉String的前后空格---只要做用户输入字符串就要无条件trim一次
// String input = " fuck you ";
// String newStr = input.trim();
// System.out.println(newStr.length());
// 4-2、split方法,根据分隔符拆分字符串
// 特殊性:当以分隔符结尾的时候,后面部分将不再拆分
// String birthday = "1982-2-18--";
// String[] array = birthday.split("-");
// System.out.println(array.length);
// 4-3、matches方法,做正则表达式校验
// 正则表达式--regex---就是用来规范字符串格式的一种表达式,其表现形式就是字符串
// 直白的说--任何一个字符串都是正则表达式,比如:"hello"就是一个,只不过它固定格式只有"hello"自己本身满足
// --要想有更多的满足情况,必须加入模糊匹配
// System.out.println("请输入电子科大的电话:");
// String phoneNum = new Scanner(System.in).next();
//
// String regexP = "8320([0-9]{4}|1[12][04])";
// if(phoneNum.matches(regexP)){
// System.out.println("这是正确的电话号码。");
// }else{
// System.out.println("你输入的号码不正确!");
// }
System.out.println("请输入您要存入的金额:");
String inputMoney = new Scanner(System.in).next();
if (inputMoney.matches("(200|1[0-9]{2}|[1-9][0-9]?)00")) {
System.out.println("输入正确,开始操作");
} else {
System.out.println("输入错误,请重新输入");
}
}
}
2.String Buffer、String Builder
1.StringBuffer和StringBuilder内容是可变的(区分String),只能new出来
2.使用场景:字符串拼接时使用比String更有效率,拼接时不能用“+”符号拼接(“+”只支持String拼接字符串),这里用append(后面拼接)和insert(中间插入拼接)方法
例子:
package com.lovo.test;
public class TestStringBuffer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//StringBuffer也是Java中表示字符串的一种数据类型,与String的区别是它的内容可变
//由于StringBuffer内容可变,通常我们在做大量字符串拼接时使用它比使用String更有效率
//StringBuffer的方法没有String丰富,主要都是类似于append\insert这样的跟
//拼接有关系的方法。
//StringBuffer线程安全,效率低
// StringBuffer sb = new StringBuffer("hello");
// StringBuffer sb1 = sb;
// StringBuffer sb2 = new StringBuffer("world");
// sb.append(sb2);
// sb.insert(5, ' ');
// System.out.println(sb1);
//StringBuilder与StringBuffer非常类似,无论是内容可变,
//还是使用的API和语法,都保持一致。
//StringBuilder线程不安全,效率高
StringBuilder sb = new StringBuilder("hello");
StringBuilder sb1 = sb;
StringBuilder sb2 = new StringBuilder("world");
sb.append(sb2);
sb.insert(5, ' ');
System.out.println(sb1);
}
}
2.包装类:是基本数据类型的封装,是基本数据类型与引用类型转换的桥梁,语法API的查阅与运用
例子:Integer,int,String之间的相互转换!
package com.lovo.test;
public class TestInteger {
public static void main(String[] args) {
//1、根据一个int变量,转换成它对应的Integer对象
// int a = 100;
// Integer in = new Integer(a);//标准做法
// Integer in1 = a;//自动封箱,这是JDK1.5提供的语法糖
// System.out.println(in);
//2、根据一个Integer对象,将它的值放到一个int变量中
// Integer in = 205;
// int a = in.intValue();//标准做法
// int b = in;//自动拆箱,这是JDK1.5提供的语法糖
// System.out.println(a);
//3、根据一个String对象,产生一个Integer对象
// String str = "123";
// Integer in = new Integer(str);
// System.out.println(in);
//4、根据一个Integer对象,产生一个String对象
// Integer in = 345;
// String str = in.toString();
// String str0 = in + "";//当对对象做字符串拼接操作,相当于默认调用对象的toString方法
// System.out.println(str);
//5、根据一个int,产生一个String对象
// int a = 120;
// String str = Integer.toString(a);
// String str0 = a + "";
// System.out.println(str);
//6、根据一个String对象,产生一个int,这是最常用的一个转换
String str = "2500";
int a = Integer.parseInt(str);//最重要最常用的一个转换方向
System.out.println(a);
}
}
3.时间日期类
Date:装当前时间日期,格式化输出时间
Calendar:可以根据输入的时间建立对象,打印时间比较麻烦
public class TestDate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//在计算机中,保存时间用的是long类型
//记录的是当前距离1970年1月1号,0:00:00:000过了多少毫秒
// long time1 = System.currentTimeMillis();
// int sum = 0;
// for(int i = 0; i < 1000; i++){
// System.out.println(i);
// }
// long time2 = System.currentTimeMillis();
//
// System.out.println(time2 - time1);
//Date来自于java.util包。
// Date now = new Date();//产生的Date对象,里面封装的是当前时间的信息
// //Date中的after、before、compareTo、equals这些时间日期比较的方法还能继续使用
// System.out.println(now);
// //在输出Date的字符串信息时,通常配合使用SimpleDateFormat这个类
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH:mm:ss 这是本月的第W周");
// String msg = sdf.format(now);
// System.out.println(msg);
Scanner scan = new Scanner(System.in);
System.out.println("请输入你出生的年份:");
int year = scan.nextInt();
System.out.println("请输入你出生的月份:");
int month = scan.nextInt();
System.out.println("请输入你出生的日期:");
int day = scan.nextInt();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE,day);
Date birthDay = cal.getTime();
String result = new SimpleDateFormat("E").format(birthDay);
System.out.println(result);
}
}