一常用类
1.System类(了解)
①作用:Java为了适应不同的操作系统(Windows Linux Mac)创建的
②简介:
2.Math类
①:作用:所有低等数学操作的类
②常用:
3.包装类(重点)
①出现的原因:Java完全面向对象的,常用的类成员通过 对象.调用; 基本数据类型无法生成对象,因此Java提出一系列关于基本数据类型的包装类
通过包装类对象 调用方法,方便操作
②实质:依然是对基本数据类型进行操作,依然符合基本数据类型的取值范围
③基本数据类型对应的包装类
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
④:Integer特殊值代码演示
public class PackageClassTest {
public static void main(String[] args) {
Integer it1 = -129;
Integer it2 = -129;
Integer it3 = -128;
Integer it4 = -128;
Integer it5 = 127;
Integer it6 = 127;
Integer it7 = 128;
Integer it8 = 128;
System.out.println("it1==it2?" + (it1 == it2));
System.out.println("尽量 不要用==毕竟数值 因为有范围限制,缓存区的存在");
System.out.println("it1==it2?" + (it1 .equals(it2)));
System.out.println("it3==it4?" + (it3 == it4));
System.out.println("it5==it6?" + (it5 == it6));
System.out.println("it7==it8?" + (it7 == it8));
/**
* 范围必须是[127,-128]
*/
}
}
⑤:包装类与基本数据类型 与String类相互转换:Integer<->int<->String
(1):包装类和基本数据类型相互转换
1)
/**
*基本数据去转换包装类
*/
public class BaseToPackageTest {
public static void main(String[] args) {
/**
* 方式1:使用 包装类的构造器 完成转换
* public Integer(int num)
* 在JDK 9版本开始 ----这种方式过时了 不推荐使用
*/
int num=25;
Integer it1=new Integer(num);
//方式2:自动装箱----从JDK5版本才能开始使用
//推荐使用
Integer it2=num;
/**
* 方式3:由 Integer类提供
* public static Integer valueOf(int num)
*/
Integer it3=Integer.valueOf(num);
System.out.println(it2+it1+it3);
//默认值是 null;
}
}
2)包装类--->基本数据类型
/**
* 演示包装类转换基本数据类型
*/
public class PackageToBaseTest {
public static void main(String[] args) {
//方式1:自动拆箱---JDK5 版本开始
Integer it=Integer.valueOf(33);
int num=it;
/**
* 方式2:由Integer类提供的
* public int intValue()
*/
int num2=it.intValue();
System.out.println(num+num2);
}
}
(2)包装类与String类相互转换
1)包装类--->String
/**
* 演示 包装了 转换String
*/
public class PackageToStringTest {
public static void main(String[] args) {
//方式1:创建一个字符串 String 的变量 之后通过+ 拼接操作
String str="";
Integer it=Integer.valueOf(64);
String newStr=str+it;
System.out.println(newStr);
/**
* 在实际开发中 不用浪费空间 别用
* 性能浪费 堆栈内存会出现问题
*/
/**
* 方式2:通过包装类去调用方法完成转换
* public String toSting()
* 使用的多
*/
String str1=it.toString();
System.out.println(str1+23);
}
}
2)String--->包装类
// String 转换成为包装类
public class StringToPackageTest {
public static void main(String[] args) {
String str="-19";
/**
* 方式1 : 通过 指定包装类的构造器来完成转换
* public Integer(String str)
* 注意事项1:当使用String 转换数值型
* String类型中的值 也必须是数值
* 注意事项2: 无法将String类型的数据 转换为 char或者character
* 注意事项3:当String 转换Boolean或者boolean类型
* ① 当String 的值不是true/false时候
* 转换的结果一定是false 此时转化的结果并不是boolean类型
* 而表示的是错误的转换
* ② 当且仅当 String 类型的值 为true/false 转换结果才是boolean
*/
//这是转不了的演示:
Boolean flag=new Boolean(str);
System.out.println(flag);
Integer it=new Integer(str);
System.out.println(it+12);
//Character c=new Character(str);
/**
* 方式2:由包装类提供的
* public static Integer valueOf(String value)
*/
Integer integer= Integer.valueOf(str);
System.out.println(integer);
//返回的是基本数据类型
integer.parseInt(str);
}
}
(3)String与基本数据类型相互转换
1)基本数据类型--->String
public class BaseToStringTest {
public static void main(String[] args) {
int num=-97;
// 方式1: 创建一个String 类型变量 用+拼接操作
String str="";
System.out.println(str+num);
/**
* 方式2:由 String 类提供的
* public static String valueOf(int num)
*/
String str1= String.valueOf(num);
System.out.println(str1);
}
}
2)String--->基本数据类型
public class StringToBaseTest {
public static void main(String[] args) {
String str="-99";
/**
* 由包装类提供的
* public static int parseInt(String value)
*/
int num=Integer.parseInt(str);
System.out.println(num);
}
}
⑥图例:描述转换方法的操作