包装类
包装类让基本数据类型具有了对象的性质, 丰富了基本数据的操作
包装类都是首字母大写 除了integer Character
常用方法
Integer常用方法
new Integer(), new Integer(12) 构造方法
parseInt() 把数值字符串转成相应的int值
valueof() 把数值字符串转成对应的Integer值
compare() 比较, 返回值证书表示第一个值大, 0表示相等, 附属表示第二个值大
Integer.Max_Value int的表数范围的最大值
Integer.Min_Value int的表数范围的最小值
public static void main(String[] args) { //构造方法 Integer in1 = new Integer(12); Integer in2 = new Integer("12"); int i = in2 + 12; //1,parseInt() 把数值字符串转成对应的int值 64+16+4+1 使用2进制转换 int i1 = Integer.parseInt("01010101", 2); System.out.println(i1); //2,valueOf() 把数值字符串转成对应的Integer值 Integer i2 = Integer.valueOf("100100"); // 4 System.out.println(i2); // compare比较 返回值是正数表示第一个值大 0表示相等 负数表示第二个值大 int compare = Integer.compare(10, 20); System.out.println("compare " + compare); // int的表数范围的最大值 System.out.println(Integer.MAX_VALUE); // int的表数范围的最小值 System.out.println(Integer.MIN_VALUE); }
包装类型缓冲池
Java虚拟机为了优化8钟基本数据类型的包装对象,为他们提供了缓冲池,缓冲池的大小为一个字节。(8个bit位,长度为2^8位,范围是-128~127)超过缓冲池的范围,包装类对象就自己在缓冲池外面赋值。如果对象的值在缓冲池范围内,就指向缓冲池的值,缓冲池里面的值是共享的。( 不包含浮点数 )
public static void constantPool() { Integer in1 = new Integer(12); Integer in2 = new Integer(12); // -128-127 Integer in3 = 127; Integer in4 = 127; Long l1 = (long) 12; Long l2 = (long) 12; System.out.println(l1 == l2); System.out.println("in3VS in4 " + (in3 == in4)); System.out.println(in1 == in2); Character c1 = 'a'; Character c2 = 'a'; Float f1 = (float) 1.0; Float f2 = (float) 1.0; System.out.println("字符是否相等" + (c1 == c2)); System.out.println(f1 == f2); }
不可变的对象
在Integer内部数值使用final修饰, 一旦改值就是新的对象
private static class IntegerCache { //在Integer内部数值使用final修饰 private final int value; }
public static void method() { Integer in1 = 10; // 一旦改值 就是新的对象 in1 = 20; System.out.println(in1); }
进制与位移
public static void radix() { int b = 24;// 00011000 // 位移运算符 0011 11000 // System.out.println(b << 3);//相当于乘以 2的三次方 System.out.println(b >> 3); //相当于除以 2 的三次方 //十进制 System.out.println(12); // 八进制表现形式 0 System.out.println(052);// 2+5*8 // 十六进制 0X/x 10-A 11-B 15-F F 1111 System.out.println(0XFA); // 二进制 0B/b System.out.println(0B101); int a = 0x52; System.out.println(a); }
Character类
isDigit() 判断字符串是否已数字组成
isUpperCase() 判断字符串是否以大写字母组成
isLetter() 判断字符串是否以小写组成
toLowerCase() 将字符串转小写
public static void wrapCharacter() { Character ch = 'a'; // 0-9 boolean flag1 = Character.isDigit(ch); boolean flag2 = Character.isUpperCase(ch); // A-Z boolean flag3 = Character.isLetter(ch); // 转小写 char c = Character.toLowerCase(ch); System.out.println("字符是否是数字字符 " + flag1); System.out.println("字符是否是大写字符 " + flag2); System.out.println("字符是否是字母 " + flag3); }
Math类
Math类内部定义了数学运算相关的方法, 全部使用static修饰
pow() 取n次方
sqrt() 取平方根
abs() 取绝对值
radom() 生成0-1之内的随机值 取值范围: 0<=x<1
ceil() 向上取最小整数
floor() 向下取整
round() 四舍五入
max() 取两个数中最大值
min() 取两个数中最小值
public static void main(String[] args) { double pi = Math.PI; System.out.println(pi); double pow = Math.pow(3, 4); double sqrt = Math.sqrt(16); int abs = Math.abs(-12); // 0-1 0=<x<1 随机值 double random = Math.random(); // 向上取最小整数 double ceil = Math.ceil(4.3); // 向下取整 double floor = Math.floor(4.3); // 四舍五入值 long round = Math.round(4.6); double max = Math.max(2.3, 4.5); int min = Math.min(10, 2); System.out.println("pow : " + pow); System.out.println("sqrt : " + sqrt); System.out.println("abs绝对值 : " + abs); System.out.println("ceil : " + ceil); System.out.println("floor : " + floor); System.out.println("round : " + round); System.out.println("max : " + max); System.out.println("min : " + min); }
运行结果:


浙公网安备 33010602011771号