基本数据类

    技术2022-07-10  112

    Integer类

    java.lang包下,为8种基本数据类型提供了对应的包装类。包装类提供了字符串、基本数据类型、包装类之间的相互转换的方法数字基本数据类型的包装类都继承了Number类,它们的使用方式相似下面以常用的基本数据类型int对象的包装类Integer为例,说明数字类型包装类的使用

    示例

    package SE01.n4Integer; public class Demo01Integer { public static void main(String[] args) { // 1.基本数据类型int值转化为Integer类型对象 // 装箱:把基本类型的数据包装到包装类中 int i=20; Integer i1=new Integer(i);//方法一 Integer i2=Integer.valueOf(i);//方法二 // 2.Integer对象转化为基本数据类型int值 // 拆箱:在包装类中取出基本类型的数据 Integer in=new Integer(20); int in1=in; System.out.println(in1); // 3.String类型的数字转化为Integer对象 String s="120"; Integer int1=new Integer(s);//方法一 Integer int2=Integer.valueOf(s);//方法二//是Integer型 parseInt是int型 // 4.Interger类型转化为String类型 Integer int0=new Integer(30); String s1=int0.toString(); // 5.String类型的数字转化为基本数据类型的int值 int a=Integer.parseInt(s);//int型 // 6.基本数据类型int转化为String类型 int b=40; String str=String.valueOf(b);//方法一 String str2=Integer.toString(b);//方法二 int aa=new Integer(1); // 这里触发了编译器的自动拆箱特性(JDK1.5之后才有),编译器会在编译后的.class文件中将代码补充为: // int a =new Integer(1).intValue(); // 自动拆箱特性是编译器认可,而不是虚拟机认可。 Integer integer=aa; // 这里触发了自动装箱特性,编译器会将代码补全为: // Integer integer=Integer.valueOf(a); // Integer的equals方法继承自Object,并且进行了重写, // 当两个对象包含的值相等时返回true否则false Integer i10=new Integer(10); Integer i20=new Integer(20); Integer i30=new Integer(10); boolean b12=i10.equals(i20);//F boolean b13=i10.equals(i30);//T boolean b23=i20.equals(i30);//F System.out.println(b12); System.out.println(b13); System.out.println(b23); } }

    Boolean类

    package SE01.n4Wrapper; public class Demo01Boolean { public static void main(String[] args) { // Boolean:用于将一个基本数据类型boolean的值包装为对象 // 1.将boolean转Boolean Boolean b1=new Boolean(true);//方法一 Boolean b2=Boolean.valueOf(true);//方法二 // 2.将Boolean转boolean Boolean b3=new Boolean(true); boolean b4=b3.booleanValue(); // 3.String转Boolean String s="true"; Boolean bo1=new Boolean(s); Boolean bo2=Boolean.valueOf(s); // 4.将Boolean转String Boolean bo3=new Boolean(true); String s1=bo3.toString(); String s2=Boolean.toString(bo3); String s3=String.valueOf(bo3); // 5.boolean转String boolean b=true; String str=String.valueOf(b); // 6.String转boolean String str11="true"; boolean bool1=Boolean.parseBoolean(str); Boolean bool2=new Boolean(str11); boolean bool3=bool2.booleanValue(); } }

    Character类

    类中的特有方法: 1.判断一个字符是否为字母或阿拉伯数字 isLetter(‘A’); isDigit(‘9’); 2.判断是否为空格 isWhitespace(’ '); 3.判断是否为大写或小写 isLowerCase(‘A’) isUpperCase(‘A’) 4.返回只含该字符的字符串 toString(‘A’) 5.转换为大写或小写 toUpperCase(‘a’) toLowerCase(‘A’)

    package SE01.n4Wrapper; public class Demo02Character { public static void main(String[] args) { // Character:用于将char类型值包装为对象 // 1.char转character Character c1=new Character('A'); Character c2=Character.valueOf('A'); // 2.Character转char Character c3=new Character('A'); char c4=c3.charValue(); char c5=Character.valueOf(c3); char c6=Character.toString(c3).charAt(0); // Character除了提供包装类和基本数据类型的转换外,还提供了一些实用的方法: // 3.判断一个字符是否为字母或阿拉伯数字 System.out.println(Character.isLetter('A')); System.out.println(Character.isDigit('9')); // 4.判断是否为空格 System.out.println(Character.isWhitespace(' ')); // 5.判断是否为大写或小写 System.out.println(Character.isLowerCase('A')); System.out.println(Character.isUpperCase('A')); // 6.返回只含该字符的字符串 System.out.println(Character.toString('A')); // 7.转换为大写或小写 System.out.println(Character.toUpperCase('a')); System.out.println(Character.toLowerCase('A')); } }
    Processed: 0.010, SQL: 9