包装类
1.Integer
构造方法:1)Integer(int number) 2)Integer(String str)
注意:字符串str要用数值型,否则会抛出NumberFormatException异常
public class demo1 {
public static void main(String[] args) {
System.out.println("----------构造方法-----------");
Integer n1=new Integer(12);
Integer n2=new Integer(23);
System.out.println("-----------常用方法------------");
System.out.println(n1.byteValue());//12 返回的是byte型
System.out.println(n1.compareTo(n2));//-1 返回的是int型 值相等返回0,小于返回负值,大于返回正值
System.out.println(n1.equals(n2));//false 返回的是布尔型
System.out.println(n1.intValue());//12 返回的是int型
System.out.println(n1.shortValue());//12 返回的是short型
System.out.println(Integer.toString(n2));//23 返回的是String型(十进制)
System.out.println(Integer.toBinaryString(n2));//10111 返回的是String型(二进制)
System.out.println(Integer.toHexString(n2));//17 返回的是String型(十六进制)
System.out.println(Integer.toOctalString(n2));//27 返回的是String型(八进制)
System.out.println(Integer.valueOf("90"));//90 返回的是
System.out.println(Integer.parseInt("100"));//100 返回的是int型
}
}
2.Boolean
构造方法:1)Boolean(boolean value) 2)Boolean(String str)
注意:针对第二个构造方法,只有当字符串内容为"true"时,boolean返回的值才为true,否则为false。
public class demo2 {
public static void main(String[] args) {
System.out.println("----------构造方法-----------");
Boolean bool1=new Boolean("12");
Boolean bool4=new Boolean("true");
Boolean bool2=new Boolean("我爱学习");
Boolean bool3=new Boolean(true);
System.out.println(bool1);//false
System.out.println(bool2);//false
System.out.println(bool3);//true
System.out.println(bool4);//true
System.out.println("-----------常用方法------------");
System.out.println(Boolean.parseBoolean("true"));//true
System.out.println(Boolean.parseBoolean("false"));//false
System.out.println(bool2.toString());//false
System.out.println(Boolean.valueOf("沉迷学习,无法自拔"));//false
System.out.println(bool3.equals(bool4));//true
System.out.println(bool1.booleanValue());//false
System.out.println("------------常量--------------------");
Boolean boo=Boolean.TRUE;
Boolean boo2=Boolean.FALSE;
Class boo3=Boolean.TYPE;
System.out.println(boo);//true
System.out.println(boo2);//false
System.out.println(boo3);//boolean
}
}
3.Byte
构造方法:1)Byte(byte value) 2)Byte(String str)
注意:对于第二个构造方法,String对应的只能是数值型参数,否则会抛出异常NumberFormatException
public class demo3 {
public static void main(String[] args){
System.out.println("----------构造方法-----------");
Byte bt1=new Byte((byte)12);
Byte bt2=new Byte("12");
//Byte bt3=new Byte("我爱学习");
System.out.println(bt1);//12
System.out.println(bt2);//12
//System.out.println(bt3);//NumberFormatException
System.out.println("-----------常用方法------------");
System.out.println(bt1.byteValue());//12
System.out.println(bt1.compareTo(bt2));//0
System.out.println(bt2.doubleValue());//12.0
System.out.println(bt1.doubleValue());//12.0
System.out.println(bt1.intValue());//12
System.out.println(bt2.intValue());//12
System.out.println(Byte.parseByte("122"));//122
System.out.println(bt1.toString());//12
System.out.println(bt2.toString());//12
System.out.println(Byte.valueOf("122"));//122
System.out.println(bt2.equals(bt1));//true
System.out.println("------------常量--------------------");
System.out.println(Byte.MAX_VALUE);//127
System.out.println(Byte.MIN_VALUE);//-128
System.out.println(Byte.SIZE);//8
System.out.println(Byte.TYPE);//byte
}
}
4.Character 构造方法:Character(char value)
public class demo4 {
public static void main(String[] args) {
System.out.println("----------构造方法-----------");
Character ch=new Character('我');
Character ch1=new Character('2');
Character ch2=new Character('a');
Character ch4=new Character('a');
Character ch5=new Character('A');
Character ch3=new Character('-');
System.out.println(ch1);//2
System.out.println(ch2);//a
System.out.println(ch3);//-
System.out.println(ch);//我
System.out.println("------------常用方法--------------------");
System.out.println(ch.charValue());//我
System.out.println(ch2.compareTo(ch5));//32(a-A=97-65=32)
System.out.println(ch2.equals(ch4));//true
System.out.println(ch2.equals(ch5));//false
System.out.println(Character.toUpperCase('a'));//A(变大写)
System.out.println(Character.toLowerCase('B'));//b(变小写)
System.out.println(ch.toString());//我(返回对象的字符串形式)
System.out.println(ch3.charValue());//-
System.out.println(Character.isUpperCase('A'));//true(判断是不是大写字符)
System.out.println(Character.isUpperCase('-'));//false
System.out.println(Character.isLowerCase('a'));//true(判断是不是小写字符)
System.out.println(Character.isLowerCase('我'));//false
System.out.println("------------常量--------------------");
System.out.println(Character.CONNECTOR_PUNCTUATION);//23表示Unicode规范中的常规类别“Pc”
System.out.println(Character.UNASSIGNED);//0表示Unicode规范中的常规类别“Cn”
System.out.println(Character.TITLECASE_LETTER);//3表示Unicode规范中的常规类别“Lt”
}
}
5.Double和Float(都是Number的子类)
构造方法:1)Double(double value) 2)Double(String str)
注意:对于第二个构造方法,String对应的只能是数值型参数,否则会抛出异常NumberFormatException
public class demo5 {
public static void main(String[] args) {
System.out.println("----------构造方法-----------");
Double dou=new Double(12.4);
Double dou2=new Double("3.1415926");
System.out.println(dou);//12.4
System.out.println(dou2);//3.1415926
System.out.println("-----------常用方法------------");
System.out.println(dou.byteValue());//12(强制转换)
System.out.println(dou2.compareTo(dou));//-1(等于返回0,小于返回负值,大于返回正值)
System.out.println(dou.equals(dou2));//false(等于返回true,否则返回false)
System.out.println(dou.intValue());//12(强制转换)
System.out.println(dou2.intValue());//3(强制转换)
System.out.println(dou.isNaN());//false(判断对象是不是数字)
System.out.println(Double.valueOf("123"));//123.0(返回用字符串表示的对象)
System.out.println(dou.doubleValue());//12.4
System.out.println(dou2.doubleValue());//3.1415926
System.out.println(dou.longValue());//12(强制转换)
System.out.println(dou2.longValue());//3(强制转换)
System.out.println(dou.toString());//12.4(返回此对象的字符串形式)
System.out.println("------------常量--------------------");
System.out.println(Double.MAX_VALUE);//1.7976931348623157E308(表示有限double变量可能具有的最大指数)
System.out.println(Double.MIN_VALUE);//4.9E-324(表示标准化double变量可能具有的最小指数)
System.out.println(Double.NEGATIVE_INFINITY);//-Infinity(表示保存double类型的负无穷大值的常量)
System.out.println(Double.POSITIVE_INFINITY);//Infinity(表示保存double类型的正无穷大值的常量)
}
}
6.Number 抽象类Number是BigDecimal、BigInteger、Byte、Double、Float、Integer、Long和Short类的父类。 常用方法: byteValue() intValue() floatValue() shortValue() longValue() doubleValue()
日常鸡汤:再不疯狂,我们就老了。。。