JavaSE07—Java常用类库
JavaSE07—Java常用类库
1.String类和StringBuffer类
String类用于比较两个字符串,查找和抽取串中的字符或子串,进行字符串与其他类型之间的相互转换等。String类对象的内容一旦被初始化就不能再改变。
StringBuffer类用于内容可以改变的字符串,可以将其他各种类型的数据增加,插入到字符串中,也可以转置字符串中原来的内容。一旦通过StringBuffer生成了最终想要的字符串,就应该用StringBuffer.toString()方法将其转换成String类。
2.基本数据类型的包装类
Java对数据既提供基本数据的简单类型,也提供了相应的包装类。
Integer int
Character char
Float float
Double double
Byte byte
Long long
Short short
Boolean boolean
使用Integer类中的parseInt()方法,讲一个字符串转换成基本数据类型:
class IntegerDemo{public static void main(String[] args) {String a = "123";int i = Integer.parseInt(a);i++;System.out.println(i);}}3.System类与Runtime类public class CalendarDemo{public static void main(String[] args){Calendar c1 = Calender.getInstance();System.out.println(c1.get(c1.YEAR)+"年"+(c1.get(c1.MONTH)+1)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR)+":"+c1.get(c1.MINUTE)+":"+c1.get(c1.SECOND));c1.add(c1.DAY_OF_YEAR,360);System.out.println(c1.get(c1.YEAR)+"年"+(c1.get(c1.MONTH)+1)+"月"+c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR)+":"+c1.get(c1.MINUTE)+":"+c1.get(c1.SECOND));}}5.Math与Random类public class RandomDemo{public static void main(String[] args){Random r = new Random();for(int i=0;i<5;i++)System.out.println(r.nextInt(100)+"\t");}}class Employee implements Cloneable{private String name;private int age;public Employee(String name,int age){this.name = name;this.age = age;}public Object clone() throws CloneNotSupportedException{return super.clone();}public String toString(){return "姓名:"+this.name+",年龄"+this.age;}public int getAge(){return age;}public void setAge(int age){this.age = age;}public String getName(){return name;}public void setName(String name){this.name = name}}public class CloneDemo{public static void main(String[] args){Employee e1 = new Employee("张三",21);Employee e2 = null;try{e2 = (Employee) e1.clone();}catch(CloneNotSupportedException e){e.printStackTrace();}e2.setName("李四");e2.setAge(30);System.out.println("两个对象的内存地址比较:"+(e1==e2));System.out.println(e1);System.out.println(e2);}}