String 类
/**
* String 简介 final修饰的类
*/
public class Demo{
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);//内存地址的比较//true
int len = s1.length();
for (int i = 0; i < len; i++) {
//charAt(int index)获取指定位置的字符
System.out.println(s1.charAt(i));//a//b//c
}
String s3 = "http://www.baidu.com/images/logdgre/dgo.gif";
if (s3 != null && s3.length() > 0) {
//从字符串中截取文件名(后部分数据)
String fileName = s3.substring(s3.lastIndexOf("/") + 1);
System.out.println(fileName);//dgo.gif
//截取前部分数据
String prefix = s3.substring(0, s3.indexOf(":"));
System.out.println(prefix);//http
}
//比较
String s4 = "acdaaa";
String s5 = "cd";
//进行内容比较
System.out.println(s4.equalsIgnoreCase(s5));//false
//判断 是否包含子字符串
System.out.println(s4.contains(s5));//true
//将字符串转为字节数组
String name = "张三";
//默认编码为unicode, 可选的有utf-8,gbk,gb2312,iso8859-1
byte[] bytes;
try {
bytes = name.getBytes("gb2312");//gbk和gb2312编码中,每一个汉字点2个字节
System.out.println(bytes.length);//4
//注意: 编码和解码必须保持一致
System.out.println(new String(bytes, "gb2312"));//张三
//基本数据类型转成字符串
String s6 = String.valueOf(100);
s6 = String.valueOf(78.85);
//查看字符串的hashCode()
System.out.println("张三".hashCode() + "," + name.hashCode());
//比较字符中,如果两个字符串完全相等,则返回0
System.out.println("abd".compareTo("abddddddd"));//-6
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
StringBuilder 类
public class Demo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("?bdsalbdgs");
//构造器模式,链式编程风格
sb.append("dasd").append("&psd=")
.append("123123").append("&phone=")
.append("3424341423542");
String url = sb.toString();
System.out.println(url);
//在头部插入" ?offset=1&"
sb.insert(url.indexOf("?") + 1, "offset=1&");
System.out.println(sb.toString());
}
}
自动拆装箱
/**
* 自动拆装箱
*/
public class Demo {
public static void main(String[] args) {
String ss = "19.5";
Double ssD = Double.parseDouble(ss);
//int ssI=Integer.parseInt(ss); //报异常,不能将带有小数的字符串转成整数
System.out.println(ssD.toString());//19.5
System.out.println(Integer.toString(34, 10));//转成指定进制(转成十进制)//34
Integer a = 100; //自动装箱: 先创建Integer,然后将100存入到Integer创建的内存中
int sum = a + 10; //自动拆箱: 先调用a.intValue()将其值取出,与10进行计算,将结果赋于sum
System.out.println(sum);//110
Integer a1 = 100;
Integer a2 = 100;
//java默认情况下,会在常量池中创建-128~127常量对象
System.out.println(a1 == a2);//true
////////////////////////////////
Integer a5 = 234;
Integer a6 = 234;
//java默认情况下,会在常量池中创建-128~127常量对象
System.out.println(a5 == a6);//false
////////////////////////////////
Integer a3 = 0;
System.out.println(a1 == (a2 + a3));//true
System.out.println(a1.hashCode() + "," + a2.hashCode());//true
//注:整型数值的hashCode()是其本身,除Long之外(有可能不相同)
Long l1 = new Long(1234567800L);
System.out.println(l1.hashCode());//1234567800
}
}
Calendar类
public class Demo {
public static void main(String[] args) {
// 获取日历对象
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);// 获取年
int month = cal.get(Calendar.MONTH); // 获取月份
int day = cal.get(Calendar.DAY_OF_MONTH);// 日
int hour = cal.get(Calendar.HOUR_OF_DAY); // 24小时
int minute = cal.get(Calendar.MINUTE);// 分钟
int second = cal.get(Calendar.SECOND);
int week=cal.get(Calendar.DAY_OF_WEEK);//星期(1~7,1:周日,7:周六)
System.out.println(String.format("%d-%d-%d %d:%d:%d", year, month + 1,
day, hour, minute, second));
System.out.println("================================");
String[] wName={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
System.out.println(wName[week-1]+","+week);
System.out.println("================================");
cal.set(Calendar.MONTH, 12-1);//12月份
//查看每一个月中的天数
for(int i=1;i<=cal.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
cal.set(Calendar.DAY_OF_MONTH, i);
week=cal.get(Calendar.DAY_OF_WEEK);
System.out.println(wName[week-1]+","+week+","+(i)+"号");
}
}
}
Date类
/**
* Date类
*/
public class Demo {
public static void main(String[] args) {
Date date = new Date();//获取当前的时间
System.out.println(date.toString());
long time = date.getTime();
Date d2 = new Date(time);
System.out.println(d2.toString()); //获取Long类型的时期值
//使用定义好的日期格式化工具类
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);//获取日期的格式化对象
DateFormat dtf = DateFormat.getDateTimeInstance();//获取日期和时间格式化对象
System.out.println(df.format(date));
System.out.println(dtf.format(date));
try {
Date d3 = df.parse("17-07-19");
System.out.println(d3.toString());
} catch (ParseException e) {
e.printStackTrace();
}
//自定义格式化日期或时间
//注:月 MM,分钟:mm,ss: 秒,SSS:毫秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
System.out.println(sdf.format(date));
}
}
运行结果
补充日期相减
public class Demo {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse("2016-09-08 10:10:10");
Date d2 = sdf.parse("2016-09-15 00:00:00");
System.out.println(daysBetween(d2, d1));
//System.out.println(daysBetween("2012-09-08 10:10:10","2012-09-15 00:00:00"));
}
public static int daysBetween(Date smdate, Date bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 字符串的日期格式的计算
*/
public static int daysBetween(String smdate, String bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
}
补充
int和Integer有什么区别?
Integer 是int 的包装类.
原始类型: boolean,char,byte,short,int,long,float,double
包装类型:Boolean,Character,Byte,Short,Integer,Long,Float,Double
-
注意
public static void main(String[] args) { Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150; System.out.println(f1 == f2);//true System.out.println(f3 == f4);//false }
如果整型字面量的值在-128到127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,所以上面的面试题中f1==f2的结果是true,而f3==f4的结果是false。
Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?
Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在参数上加0.5然后进行下取整。
是否可以继承String类?
String 类是final类,不可以被继承。
String和StringBuilder、StringBuffer的区别?
String和StringBuffer/StringBuilder,它们可以储存和操作字符串。其中String是只读字符串,也就意味着String引用的字符串内容是不能被改变的。而StringBuffer/StringBuilder类表示的字符串对象可以直接进行修改。StringBuilder是Java 5中引入的,它和StringBuffer的方法完全相同,区别在于它是在单线程环境下使用的,因为它的所有方面都没有被synchronized修饰,因此它的效率也比StringBuffer要高。
String s = new String("xyz");创建了几个字符串对象?
两个对象,一个是静态区的"xyz",一个是用new创建在堆上的对象。
怎样将GB2312编码的字符串转换为ISO-8859-1编码的字符串?
String s1 = "你好";
String s2 = new String(s1.getBytes("GB2312"), "ISO-8859-1");