Java字符串-包装类-日期-多线程(未完)

String s1= "S";
String s2= "S";
String s3 = new String("S");
String s4 = new String("S");
System.out.println( s1==s2 + "," + s1==s3 + "," + s3==s4);//ture false false

String创建后不可改变,重新赋值意味着指向新的对象
若希望比较字符串是否相同,则可用 s3.equals(s4);//返回ture
"=="用于比较字符串首地址是否相同
常用String方法——图自慕课网


Java字符串-包装类-日期-多线程(未完)_第1张图片
String常用方法.jpg

warning:

1当返回位置时返回物理位置(从0开始)
2substring(x1,x2)返回的字串从物理地址x1——x2-1的字符串(不包括s[x2])

StringBuilder类——线程安全且可以修改的字符串
效率稍低于String
基本都支持String方法外,追加了一些方法

Java字符串-包装类-日期-多线程(未完)_第2张图片
StringBuilder常用方法.jpg

包装类

Java字符串-包装类-日期-多线程(未完)_第3张图片
包装类.jpg

常用于数据类型的相互转化
装箱:把基本类型转换成包装类,使其具有对象的性质
拆箱:和装箱相反,把包装类对象转换成基本类型的值

转化的实现:

基本类型转换为字符串有三种方法:
1使用包装类的 toString() 方法
2 使用String类的 valueOf() 方法
3 用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串

int c =5;
String str1 = Integer.toString(c);
String str2 = String.valueOf(c);
String str3 = c+" ";

将字符串转换成基本类型有两种方法
1调用包装类的 parseXxx 静态方法
2 调用包装类的 valueOf() 方法转换为基本类型的包装类,会自动拆箱

String str = "5";
int x1 = Integer.parseInt(str);
int x2 = Integer.valueOf(str);

日期的格式化输出

1Date类

//用format方法将日期转化为指定格式文本
Date d = new Date();//表示当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");指定目标格式
String today = sdf.format(d);调用format()方法格式化时间
System.out.println(today);

//用parse()方法将文本转化为日期
String day = "2016年11月10日 21:20:00";
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = df.parse(day);
System.out.println(date);

warning:

1调用 SimpleDateFormat 对象的 parse() 方法时可能会出现转换异常,即 ParseException ,因此需要进行异常处理
2使用 Date 类时需要导入 java.util 包,使用 SimpleDateFormat 时需要导入 java.text 包

2Calendar类

Calendar c = new Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);

多线程的两种实现方法

继承Thread类

class ClassName extends Thread
public int x;
public Classname(int x){ this.x = x; }
public void run()
{
System.out.println(this.x+"开始");
try{Thread.sleep(1000*10);
}catch(Exception ex){}
System.out.println(this.x+"结束");
}
public class TextClass{
public static void main(String[] args){
Classname c1 = new Classname(1);
Classname c2 = new Classname(2);
Classname c3 = new Classname(3);
c1.start();
c1.start();
c1.start();
}
}

warning:start方法重复调用的话,会出现java.lang.IllegalThreadStateException异常

Classname c1 = new Classname(1);
Classname c2 = c1;
c1.start();
c1.start();//出现error

用Runnable接口

class ClassName implements Runnable
public int x;
public Classname(int x){ this.x = x; }
public void run()
{
System.out.println(this.x+"开始");
try{Thread.sleep(1000*10);
}catch(Exception ex){}
System.out.println(this.x+"结束");
}
public class TextClass{
public static void main(String[] args){
Classname c1 = new Classname(1);
Classname c2 = new Classname(2);
Classname c3 = new Classname(3);
c1.start();
c1.start();
c1.start();
}
}

你可能感兴趣的:(Java字符串-包装类-日期-多线程(未完))