裝箱和拆箱
1.封裝類
所有的基本類型,都有對應的類類型
比如int對應的類是Integer
這種類就叫做封裝類
2.Number類
數字封裝類有
Byte,Short,Integer,Long,Float,Double
這些類都是抽象類Number的子類
3.基本類型轉封裝類
int
i =
5
;
//基本類型轉換成封裝類型
Integer it =
new
Integer(i);
4.封裝類轉基本類型
int
i =
5
;
//基本類型轉換成封裝類型
Integer it =
new
Integer(i);
//封裝類型轉換成基本類型
int
i2 = it.intValue();
5.自動裝箱
不需要調用構造方法,通過=符號自動把 基本類型 轉換為 類類型 就叫裝箱
int
i =
5
;
//基本類型轉換成封裝類型
Integer it =
new
Integer(i);
//自動轉換就叫裝箱
Integer it2 = i;
6.自動拆箱
不需要調用Integer的intValue方法,通過=就自動轉換成int類型,就叫拆箱
int
i =
5
;
Integer it =
new
Integer(i);
//封裝類型轉換成基本類型
int
i2 = it.intValue();
//自動轉換就叫拆箱
int
i3 = it;
7.int的最大值,最小值
int的最大值可以通過其對應的封裝類Integer.MAX_VALUE獲取
字符串轉換
1.數字轉字符串
int
i =
5
;
//方法1
String str = String.valueOf(i);
//方法2
Integer it = i;
String str2 = it.toString();
String str =
"999"
;
int
i= Integer.parseInt(str);
System.out.println(i);
float
f1 =
5
.4f;
float
f2 =
5
.5f;
//5.4四舍五入即5
System.out.println(Math.round(f1));
//5.5四舍五入即6
System.out.println(Math.round(f2));
//得到一個0-1之間的隨機浮點數(取不到1)
System.out.println(Math.random());
//得到一個0-10之間的隨機整數 (取不到10)
System.out.println((
int
)( Math.random()*
10
));
//開方
System.out.println(Math.sqrt(
9
));
//次方(2的4次方)
System.out.println(Math.pow(
2
,
4
));
//π
System.out.println(Math.PI);
//自然常數
System.out.println(Math.E);
String name =
"蓋倫"
;
int
kill =
8
;
String title=
"超神"
;
//直接使用+進行字符串連接,編碼感覺會比較繁瑣,並且維護性差,易讀性差
String sentence = name+
" 在進行了連續 "
+ kill +
" 次擊殺後,獲得了 "
+ title +
" 的稱號"
;
System.out.println(sentence);
//使用格式化輸出
//%s錶示字符串,%d錶示數字,%n錶示換行
String sentenceFormat =
"%s 在進行了連續 %d 次擊殺後,獲得了 %s 的稱號%n"
;
System.out.printf(sentenceFormat,name,kill,title);
int
year =
2020
;
//總長度,左對齊,補0,千比特分隔符,小數點比特數,本地化錶達
//直接打印數字
System.out.format(
"%d%n"
,year);
//總長度是8,默認右對齊
System.out.format(
"%8d%n"
,year);
//總長度是8,左對齊
System.out.format(
"%-8d%n"
,year);
//總長度是8,不够補0
System.out.format(
"%08d%n"
,year);
//千比特分隔符
System.out.format(
"%,8d%n"
,year*
10000
);
//小數點比特數
System.out.format(
"%.2f%n"
,Math.PI);
//不同國家的千比特分隔符
System.out.format(Locale.FRANCE,
"%,.2f%n"
,Math.PI*
10000
);
System.out.format(Locale.US,
"%,.2f%n"
,Math.PI*
10000
);
System.out.format(Locale.UK,
"%,.2f%n"
,Math.PI*
10000
);
System.out.println(Character.isLetter(
'a'
));
//判斷是否為字母
System.out.println(Character.isDigit(
'a'
));
//判斷是否為數字
System.out.println(Character.isWhitespace(
' '
));
//是否是空白
System.out.println(Character.isUpperCase(
'a'
));
//是否是大寫
System.out.println(Character.isLowerCase(
'a'
));
//是否是小寫
System.out.println(Character.toUpperCase(
'a'
));
//轉換為大寫
System.out.println(Character.toLowerCase(
'A'
));
//轉換為小寫
String a =
'a'
;
//不能够直接把一個字符轉換成字符串
String a2 = Character.toString(
'a'
);
//轉換為字符串
System.out.println(
"使用空格無法達到對齊的效果"
);
System.out.println(
"abc def"
);
System.out.println(
"ab def"
);
System.out.println(
"a def"
);
System.out.println(
"使用\\t制錶符可以達到對齊的效果"
);
System.out.println(
"abc\tdef"
);
System.out.println(
"ab\tdef"
);
System.out.println(
"a\tdef"
);
System.out.println(
"一個\\t制錶符長度是8"
);
System.out.println(
"12345678def"
);
System.out.println(
"換行符 \\n"
);
System.out.println(
"abc\ndef"
);
System.out.println(
"單引號 \\'"
);
System.out.println(
"abc\'def"
);
System.out.println(
"雙引號 \\\""
);
System.out.println(
"abc\"def"
);
System.out.println(
"反斜杠本身 \\"
);
System.out.println(
"abc\\def"
);
String str1 =
"the light"
;
String str2 =
new
String(str1);
//==用於判斷是否是同一個字符串對象
System.out.println( str1 == str2);
String str1 =
"the light"
;
String str3 =
"the light"
;
System.out.println( str1 == str3);
public
interface
IStringBuffer {
public
void
append(String str);
//追加字符串
public
void
append(
char
c);
//追加字符
public
void
insert(
int
pos,
char
b);
//指定比特置插入字符
public
void
insert(
int
pos,String b);
//指定比特置插入字符串
public
void
delete(
int
start);
//從開始比特置删除剩下的
public
void
delete(
int
start,
int
end);
//從開始比特置删除結束比特置-1
public
void
reverse();
//反轉
public
int
length();
//返回長度
}