public class testString {
public static void main(String [] args)
{
String s = "Michael";
s += ", ok";
System.out.println(s);
StringBuffer x = new StringBuffer("Ming");
x.append("ming").append(", good!");
System.out.println(x.toString());
// 频繁修改字符串,使用 StringBuffer 性能更好
}
}
输出:
Michael, ok
Mingming, good!
String s1 = "10";
// Integer.parseInt 字符串转整数
int n = Integer.parseInt(s1,16);// x 进制, 不写默认10进制
System.out.println(++n); // 17
// System 类, 如打印系统信息
Properties sp = System.getProperties();
Enumeration e = sp.propertyNames();
while(e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(key + " = " + sp.getProperty(key));
}
System.exit(-1);
Runtime run = Runtime.getRuntime();
try{
run.exec("notepad.exe");// 打开记事本,exec返回子进程Process类对象
}
catch (Exception ep){
ep.printStackTrace();
}
// 时间类
Calendar c1 = Calendar.getInstance();
System.out.println(c1.get(c1.YEAR)+"年"+(c1.get(c1.MONTH)+1)+"月"+
c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR_OF_DAY)+":"+
c1.get(c1.MINUTE)+":"+c1.get(c1.SECOND));
c1.add(c1.DAY_OF_MONTH, 3); // 加3天
System.out.println(c1.get(c1.YEAR)+"年"+(c1.get(c1.MONTH)+1)+"月"+
c1.get(c1.DAY_OF_MONTH)+"日"+c1.get(c1.HOUR_OF_DAY)+":"+
c1.get(c1.MINUTE)+":"+c1.get(c1.SECOND));
输出:
2021年2月26日23:5:26
2021年3月1日23:5:26
SimpleDateFormat sp1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat sp2 = new SimpleDateFormat("yyyy年MM月dd日 hh点mm分ss秒");
try{
Date d = sp1.parse("2021-2-26 23:02:03");
System.out.println(sp2.format(d));
// 2021年02月26日 11点02分03秒
}
catch (ParseException e2){
e2.printStackTrace();
}
Random r = new Random();//以时间为种子的
for(int i = 0; i < 5; ++i)
System.out.print(r.nextInt(100)+"\t");
Cloneable
接口Object
类 的 clone()
方法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 "name: "+this.name+" age: "+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;
}
}
class test5{
public static void main(String[] args){
Employee e1 = new Employee("Michael", 18);
Employee e2 = null;
try{
e2 = (Employee) e1.clone();
}
catch (CloneNotSupportedException e){
e.printStackTrace();
}
e2.setName("Ming");
e2.setAge(19);
System.out.println("两个对象的地址比较:"+(e1 == e2));
System.out.println(e1);
System.out.println(e2);
}
}
输出:
两个对象的地址比较:false
name: Michael age: 18
name: Ming age: 19