-------android培訓(xùn)java培訓(xùn)期待與您交流!----------
String類介紹
- 字符串是一個(gè)特殊的對(duì)象。
- 字符串一旦初始化就不可以被改變。
- Java 程序中的所有字符串字面值(如 "abc" )都作為此類的實(shí)例實(shí)現(xiàn)。
-
String s1 = "abc"與String s2 = new String("abc");s1和s2都是定義一個(gè)字符串為"abc"的對(duì)象變量。s1是一個(gè)類型變量,"abc"是一個(gè)對(duì)象。 - 兩者區(qū)別:
String s1 = "abc"; //s1是一個(gè)類型變量,"abc"是一個(gè)對(duì)象
String s2 = new String("abc");//s1和s2區(qū)別:s1在內(nèi)存中有一個(gè)對(duì)象;s2在內(nèi)存中有兩個(gè)對(duì)象
- String比較內(nèi)容方法:
s1.equals(s2);String特有的比較方法,復(fù)寫了Object中的方法。 - 常量池:String在內(nèi)存中一個(gè)常量池,常量池中有一個(gè)數(shù)組用來存儲(chǔ)a、b、c等這樣的字符,如果新的字符串中的字符內(nèi)容在常量池中有,就會(huì)在常量池中組合產(chǎn)生出一個(gè)新的字符串對(duì)象。
String常見方法:
package com.sergio.String;
/**
* String的Java封裝API的使用介紹
* Created by Sergio on 2015/1/6.
*/
public class IntroductionString {
public static void main(String[] args) {
StringJudgment();
StringMethodGet();
StringTransform();
StringCutAndReplaceAndSubstring()
StringChangeAndRemoveAndCompare()
}
//打印各種方法。狀態(tài)特性O(shè)bject obj
public static void sop(Object obj) {
System.out.println(obj);
}
//字符串獲取方法介紹
public static void StringMethodGet() {
//定義字符串
String str = "abcdefpsf";
sop(str.length()); //獲取字符串長(zhǎng)度
sop(str.charAt(4)); //獲取索引處字符。當(dāng)訪問到字符串中不存在的角標(biāo)時(shí)會(huì)發(fā)生StringIndexOutOfBoundsException異常
sop(str.indexOf('a')); //根據(jù)字符獲取索引。a在字符串中第一次出現(xiàn)的位置
sop(str.indexOf('a', 2)); //從2索引位置開始往后找a出現(xiàn)的位置。如果沒有找到返回-1。
sop(str.lastIndexOf('s')); //獲取a字符從字符串從右往左數(shù)出現(xiàn)的位置的角標(biāo)數(shù),角標(biāo)數(shù)還是從左往右的來計(jì)數(shù)。
}
//判斷字符串
public static void StringJudgment() {
String str = "Welcome Java world";
sop(str.isEmpty()); //判斷長(zhǎng)度是否為0;
sop(str.startsWith("Welcome")); //判斷是否從Welcome字符串開始
sop(str.endsWith(".java")); //判斷是否是.java結(jié)尾文件
sop(str.contains("Java")); //判斷字符串是否在str中包含。
sop(str.equals("world")); //判斷字符串內(nèi)容是否相同。復(fù)寫了Object中的equals方法
sop(str.equalsIgnoreCase("World")); //忽略大小寫比較內(nèi)容
}
//字符串轉(zhuǎn)換
//特殊:字符串和字節(jié)數(shù)組在轉(zhuǎn)換過程中,是可以指定編碼表的
public static void StringTransform() {
char[] arr = {'a', 'b', 'c', 'd', 'e', 'f'};
String str = "sfsfwsgsgd";
byte[] arr2 = {0x06,0x02,0x00,0x00,0x00,0x0E,0x01,0x01,0x00,0x00,0x00,0x01,0x19};
sop("s = " + new String(arr)); //將字符數(shù)組轉(zhuǎn)成字符串,構(gòu)造函數(shù)方式
sop("s = " + new String(arr, 1, 3)); //將字符數(shù)組轉(zhuǎn)成字符串,從1位置開始,數(shù)3個(gè)字符。構(gòu)造函數(shù)方式
sop(String.copyValueOf(arr));//將字符數(shù)組轉(zhuǎn)成字符串,靜態(tài)方法方式
sop(String.copyValueOf(arr, 2, 4)); //將字符數(shù)組轉(zhuǎn)成字符串,從2位置開始,數(shù)4個(gè)字符。構(gòu)造函數(shù)方式
sop("s = " + str.toCharArray());//將字符串轉(zhuǎn)成字符數(shù)組
sop("s = " + new String(arr2));//將字節(jié)數(shù)組轉(zhuǎn)成字符串
sop("byte[] = " + str.getBytes());//將字符串轉(zhuǎn)成字節(jié)數(shù)組
sop(String.valueOf(3)); //將基本數(shù)據(jù)類型換成字符串,可以傳的參數(shù)為:8個(gè)基本數(shù)據(jù)類型值。靜態(tài)方法方式
}
//字符切割,替換,子串獲取
public static void StringCutAndReplaceAndSubstring()
{
String str = "hello world";
//替換replace(oldchar, newchar);
sop("s = " + str.replace('e' , 'a')); //替換字符串,將e替換成a。如果要替換的字符不存在,返回還是原字符串
sop("s1 = " + str.replace("world" , "java"));//替換部分字符串
//切割String[] split(regex)
String str2 = "zhangsan, lisi, wangwu";
String[] arr = str2.split(","); //以","切割字符串
for(int x = 0; x < arr.length; x++)
{
sop(arr[x]);
}
//子字符串獲取
//String substring(begin);String substring(begin, end);
String str3 = "wowkrld";
sop(str3.substring(2)); //從指定下標(biāo)開始到結(jié)尾處。如果角標(biāo)不存在,會(huì)出現(xiàn)字符串角標(biāo)越界異常
sop(str3.substring(2, 3)); //包含頭,不包含尾。str
}
//轉(zhuǎn)換,去除空格,比較
public static void StringChangeAndRemoveAndCompare()
{
String str = "hello Java";
//大小寫轉(zhuǎn)換
sop(str.toLowerCase()); //全部轉(zhuǎn)換成小寫
sop(str.toUpperCase());//全部轉(zhuǎn)換成大寫
//去除空格
sop(str.trim());//去除字符串兩端的空格
//自然順序的比較
String s1 = "abc";
String s2 = "aaa";
sop(s1.compareTo(s2));//按順序比較不相同的第一個(gè)字符,即返回結(jié)果,后面字符不比較。如果參數(shù)字符串等于此字符串,則返回值 0;
// 如果此字符串按字典順序小于字符串參數(shù),則返回一個(gè)小于 0 的值;
// 如果此字符串按字典順序大于字符串參數(shù),則返回一個(gè)大于 0 的值。
}
}
StringBuffer
- StringBuffer是String帶有字符串緩存區(qū),可以字符串進(jìn)行修改。
- 特點(diǎn):是一個(gè)容器,長(zhǎng)度是可變化的;可以字節(jié)操作多個(gè)數(shù)據(jù)類型;最終回通過toString方法變成字符串。
- 修改動(dòng)作包括:
1. 存儲(chǔ):StringBuffer append();將指定數(shù)據(jù)作為參數(shù)加到已有數(shù)據(jù)的結(jié)尾處。StringBuffer insert(index, 數(shù)據(jù));可以將數(shù)據(jù)插入到指定index位置。
2. 刪除:StringBuffer delete(int start, int end);刪除緩存區(qū)中的數(shù)據(jù),包含start不包含end。StringBuffer deleteCharAt(index);刪除指定位置的字符。
3. 獲取:char charAt(int index);int indexOf(String str);int lastIndexOf(String str);int length();String substring(int start, int end);
4. 修改:StringBuffer replace(int start, int end, String str); char setCharAt(int index, char ch);
package com.sergio.StringBuffer;
/**
* StringBuffer的API使用介紹。
* Created by Sergio on 2015/1/14.
*/
public class IntroductionStringBuffer {
public static void main(String[] args) {
add();
delete();
update();
reverseString();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
//增加存儲(chǔ)
public static void add()
{
StringBuffer s = new StringBuffer();
StringBuffer s1 = s.append(34);
s.append("abc").append(true).append(32); //返回abctrue32長(zhǎng)的字符串信息
s.insert(1, "aa"); //指定位置插入數(shù)據(jù)。插入位置超過角標(biāo)報(bào)角標(biāo)越界異常
sop(s.toString()); //打印所有動(dòng)作后的結(jié)果,是個(gè)結(jié)果集
}
//刪除
public static void delete()
{
StringBuffer sb = new StringBuffer("avds");
sb.delete(1, 3);//刪除指定位置,包含頭不包含尾
sb.delete(0, sb.length()); //清空緩存區(qū)
sb.deleteCharAt(2); //刪除指定位置字符
sop(sb.toString());
}
//替換
public static void update()
{
StringBuffer sb = new StringBuffer("afsfsdf");
sb.replace(1, 4, "java"); //替換指定位置的字符為java
sb.setCharAt(2, 'k'); //替換某一個(gè)字符
sop(sb.toString());
}
//反轉(zhuǎn)
public static void reverseString()
{
StringBuffer sb = new StringBuffer("absdfsdf");
sb.reverse();
sop(sb.toString());
}
}
StringBuilder
- JDK1.5開始出現(xiàn)。
- StringBuffer是線程同步。StringBuilder是線程不同步。
- 單線程實(shí)例推薦使用StringBuilder。跟StringBuffer是API兼容的,只需將StringBuffer聲明改成StringBuilder即可。
基本數(shù)據(jù)類型包裝類
- 基本數(shù)據(jù)類型對(duì)應(yīng)的引用數(shù)據(jù)類型:
| 基本數(shù)據(jù)類型 | 引用數(shù)據(jù)類型 |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| boolean | Boolean |
| float | Float |
| double | Double |
| char | Character |
- 基本數(shù)據(jù)類型對(duì)象包裝類的最常見作用:
用于基本數(shù)據(jù)類型和字符串類型之間做轉(zhuǎn)換。
1. 基本數(shù)據(jù)類型轉(zhuǎn)成字符串:基本數(shù)據(jù)類型.toString(基本數(shù)據(jù)類型值);
2. 字符串轉(zhuǎn)成基本數(shù)據(jù)類型:基本數(shù)據(jù)類型包裝類.parse基本數(shù)據(jù)類型(字符串);
package com.sergio.PackageObject;
/**
* Created by Sergio on 2015/1/15.
*/
public class Introduction {
public static void main(String[] args) {
System.out.println(Integer.parseInt("123") + 4);//必須傳入對(duì)應(yīng)的數(shù)據(jù)類型,將“123”變成int數(shù)值,然后進(jìn)行計(jì)算。靜態(tài)調(diào)用方式
System.out.println((new Integer(123)).intValue()); //對(duì)象調(diào)用方式
System.out.println(Integer.toBinaryString(-3));
System.out.println(Integer.toOctalString(-60));
//十進(jìn)制轉(zhuǎn)成其他進(jìn)制
System.out.println(Integer.toBinaryString(10));
System.out.println(Integer.toHexString(234));
System.out.println(Integer.toOctalString(123));
//其他進(jìn)制轉(zhuǎn)換成十進(jìn)制
int x = Integer.parseInt("110", 16);//parseInt("a", b);a代表要被轉(zhuǎn)換的數(shù)值,b代表十進(jìn)制要轉(zhuǎn)成成其他的進(jìn)制
System.out.println(x);
}
}
-
Integer x = 4;//JDK1.5以后的特性。自動(dòng)裝箱功能。
x = x + 2;x+2;x進(jìn)行自動(dòng)拆箱,變成int類型,和2進(jìn)行加法運(yùn)算,再將和進(jìn)行裝箱賦給x
Integer x = null:這是允許存在的,所以運(yùn)算前需要進(jìn)行判斷為數(shù)字值,在進(jìn)行運(yùn)算,否則會(huì)出現(xiàn)空指針異常信息
System
- System類包含一些有用的類字段和方法,不能被實(shí)例化(沒有構(gòu)造函數(shù),方法是靜態(tài)的)。
- 可以用來獲取一些系統(tǒng)的信息。系統(tǒng)信息可以動(dòng)態(tài)加載,啟動(dòng)JVM時(shí)添加如下命令
java -D添加信息鍵=添加信息值。也就是-D用來加載。
package com.sergio.JDK5;
import java.util.Properties;
/**
* System的API使用介紹。
* 獲取系統(tǒng)屬性信息:Properties getProperties();
* out:標(biāo)準(zhǔn)輸出,默認(rèn)是控制臺(tái)。
* in:標(biāo)準(zhǔn)輸入,默認(rèn)是鍵盤。
* Created by Sergio on 2015-02-27.
*/
public class SystemClass {
public static void main(String[] args) {
//Properties是HashTable的子類,同時(shí)也就是Map集合的一個(gè)子類對(duì)象,可以通過Map的方法獲取該集合中的元素。
//該集合中定義都是字符串,沒有泛型定義。
Properties prop = System.getProperties();
//自定義特有信息
System.setProperty("key", "value");
//獲取指定屬性信息
String value1 = System.getProperty("os.name");
System.out.println("value" + value1);
//獲取所有屬性信息
for (Object obj : prop.keySet()) {
String value = (String) prop.get(obj);
System.out.println(obj + "::" + value);
}
}
}
Runtime
- 每個(gè)Java應(yīng)用程序都有一個(gè)Runtime類實(shí)例,是應(yīng)用程序能夠與其運(yùn)行的環(huán)境相連接??梢酝ㄟ^getRuntime方法獲取當(dāng)前運(yùn)行時(shí),應(yīng)用程序不能創(chuàng)建自己的Runtime類實(shí)例。
- 使用了單例模式,返回值類型為本類類型。
package com.sergio.JDK5;
/**
* Runtime使用了單例設(shè)計(jì)模式完成。
* Created by Sergio on 2015-02-27.
*/
public class RuntimeClass {
public static void main(String[] args) throws Exception {
Runtime r = Runtime.getRuntime();
//打開指定路徑的程序
Process p = r.exec("C:\\Windows\\regedit.exe");
//用特定軟件打開指定的文件
Process p1 = r.exec("notepad.exe RuntimeClass.java");
Thread.sleep(4000);
//殺掉子進(jìn)程.只能殺掉自己?jiǎn)?dòng)的程序進(jìn)程
p.destroy();
}
}
Date
- 表示特定的瞬間,精確到毫秒。
package com.sergio.JDK5;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Date對(duì)象使用。
* Created by Sergio on 2015-02-27.
*/
public class DateClass {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);
//將時(shí)間模式封裝到SimpleDateFormat對(duì)象中
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
//調(diào)用format方法讓模式格式化指定Date對(duì)象
String time = sdf.format(d);
System.out.println("time = " + time);
}
}
Calendar
- Calendar類似一個(gè)抽象類,它為特定瞬間與一組諸如YEAR、MONTH、DAY_OF_MONTH、HOUR等日歷字段之間的轉(zhuǎn)換提供了一些方法,并未操作日歷字段(例如獲得下星期的日期)提供了一些方法。
package com.sergio.JDK5;
import java.util.Calendar;
/**
* Created by Sergio on 2015-02-27.
*/
public class Calender {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.set(2012, 1, 23);//設(shè)置時(shí)間日期
c.add(Calendar.DAY_OF_MONTH, -18);//對(duì)時(shí)間進(jìn)行增加減少操作
calendarPrint(c);
}
public static void calendarPrint(Calendar c) {
//查表法查詢?cè)潞托瞧趲? String[] mons = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
String[] weeks = {"", "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
//獲取角標(biāo)值來獲取具體時(shí)間
int index = c.get(Calendar.MONTH);
int index1 = c.get(Calendar.DAY_OF_WEEK);
System.out.println(mons[index]);
System.out.println(weeks[index1]);
}
}
Math-Random
- Math類包含用于執(zhí)行基本數(shù)學(xué)運(yùn)算的方法,如初等指數(shù)、對(duì)數(shù)、平方根和三角函數(shù)。
package com.sergio.JDK5;
/**
* Math的API介紹使用。
* Created by Sergio on 2015-02-28.
*/
public class MathDemo {
public static void main(String[] args) {
double d = Math.ceil(-16.34); //返回大于指定數(shù)據(jù)的最小整數(shù)
System.out.println(d);
double d1 = Math.floor(12.34);//返回小于指定數(shù)據(jù)的最大整數(shù)
System.out.println(d1);
double l = Math.round(12.42); //四舍五入
double b2 = Math.pow(2, 3); //冪數(shù)運(yùn)算。2的三次方
double b3 = Math.random() * 10 + 1;//隨機(jī)數(shù)
System.out.println(b3);
double b = 1.2345;
System.out.println(String.format("%.2f", b));//格式化數(shù)字結(jié)果為保留2位小數(shù)位
}
}