装箱和拆箱
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();
//返回长度
}