------- android培训、java培训、期待与您交流! ----------
API是应用程序编程接口,在java中API的选择是很重要的
。先说下String类和StringBuffer类,String类用于比较,查
找,抽取俩字符串,String类对象一旦被初始化就不能被更改
而StringBuffer类则弥补了这个缺点,他可以修改其内容,当
得到想要的字符串就toString一下将其装换成String类,+操作
符用于连接字符串
String x=“a”+“c”;等效于String x=new StringBuffer
().append("a").append("c").toString();
关于基本数据类型的包装类
int--Integer char--Character float,double等地包装类都
是其首字母大写的单词
使用包装类
class IntegerDemo{
public static void main(String[] args){
String a="123";
int i=Integer.parseInt(a);
i++;
System.out.println(i);
}
}
关于System和Runtime类
System类封装一些系统相关的重要的方法和变量,下面来打印
当前虚拟机的所有环境属性的变量和值
import java.util.*;
public calss SystemDemo{
public static void main(String[] args){
Properties p1=System.out.getProperties();
Enumeration e=p1.propertyNames();
while(e.hasMoreElements()){
String key=(String)e.nextElement();
System.out.println(key+"="p1.getProperty(key));
}
}
}
java -D<name>=<value> 类名
例如:java -DAuthor=liubin SystemDemo
这样就增加了Author属性其属性值为liubin
Runtime类封装了java命令本身运行的进程,通过他的静态方法
getRuntime获得对象引用
public class RuntimeDemo{
public static void main(String[] args){
Runtime run=Runtime.getRuntime();
try{
run.exec("notepad.exe");
}catch(Exception e){
e.printStackTrace();
}
}
}
Date,Calendar,DateFormat类
Date类用于表示日期与时间,但由于没有考虑到国际化,基本
上被Calendar,DateFormat取代,关于其方法,自己去查API。
其更改日期字符串用DateFormat的format方法,但是要用
DateFormat接口的实现类去创建,例如new SimpleDateFormat
().format(new Date());
关于Math和Random,可以去查API,另外关于hashCode()方法他
用于存储散列表时使用,散列码的取得根据实际情况计算出来
的。那么关于对象克隆,对象克隆实际上是将对象复制一份
对象克隆代码:
class Employee implements Cloneable{
private String name;
private int age;
public Eleployee(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();
System.out.println("两个对象的内存地址比较:"+(e1==e2
));
System.out.println(e1);
System.out.println(e2);
}
}
所以实现对象的克隆需要实现Cloneable接口,然后再改写
clone()方法