zoukankan      html  css  js  c++  java
  • 【原】Java学习笔记024

     1 package cn.temptation;
     2 
     3 public class Sample01 {
     4     public static void main(String[] args) {
     5         // 之前对于基本数据类型都是直接进行声明操作,缺少现成的成员方法可以使用
     6         // Java针对基本数据类型缺少成员方法的问题,提供了解决方案 ----- 包装类(WrapClass),对基本数据类型进行包装的类
     7         
     8         /*
     9          *         基本数据类型            对应的包装类
    10          *         byte                Byte
    11          *         short                Short
    12          *         int                    Integer
    13          *         long                Long
    14          * 
    15          *         float                Float
    16          *         double                Double
    17          * 
    18          *         char                Character
    19          * 
    20          *         boolean                Boolean
    21          */
    22         
    23         // 包装类Integer:Integer 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个 int 类型的字段。 
    24         
    25         // Integer类的常用字段:
    26         // 1、static int MAX_VALUE :值为 2^31-1 的常量,它表示 int 类型能够表示的最大值。 
    27         // 2、static int MIN_VALUE :值为 -2^31 的常量,它表示 int 类型能够表示的最小值。 
    28         System.out.println("Integer.MAX_VALUE:" + Integer.MAX_VALUE);        // 2147483647
    29         System.out.println("Integer.MIN_VALUE:" + Integer.MIN_VALUE);        // -2147483648
    30         
    31         // Integer类的构造函数
    32         // 1、Integer(int value) :构造一个新分配的 Integer 对象,它表示指定的 int 值。 
    33         // 2、Integer(String s) :构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。 
    34         int i = 2;
    35         Integer integer1 = new Integer(i);
    36         System.out.println("integer1:" + integer1);            // 2
    37         System.out.println(integer1 instanceof Integer);    // true
    38         // 语法错误:Syntax error, insert "Dimensions" to complete ArrayType
    39 //        System.out.println(integer1 instanceof int);
    40         System.out.println("-----------------------------------------");
    41         
    42         String str = "123";
    43         Integer integer2 = new Integer(str);
    44         System.out.println("integer2:" + integer2);
    45         System.out.println(integer2 instanceof Integer);    // true
    46         
    47         String strEx = "java";
    48         // 执行异常:java.lang.NumberFormatException: For input string: "java"
    49         Integer integer3 = new Integer(strEx);
    50         System.out.println("integer3:" + integer3);
    51         System.out.println(integer3 instanceof Integer);
    52     }
    53 }
     1 package cn.temptation;
     2 
     3 public class Sample02 {
     4     public static void main(String[] args) {
     5         /*
     6          * Integer包装类的常用成员方法:
     7          * 1、static String toBinaryString(int i) :以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。 
     8          * 2、static String toHexString(int i) :以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。 
     9          * 3、static String toOctalString(int i) :以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。 
    10          */
    11         System.out.println(Integer.toBinaryString(3));        // 11
    12         System.out.println(Integer.toHexString(10));        // a
    13         System.out.println(Integer.toOctalString(9));        // 11
    14     }
    15 }
     1 package cn.temptation;
     2 
     3 public class Sample03 {
     4     public static void main(String[] args) {
     5         /*
     6          * int类型   和   string类型之间的转换
     7          * 
     8          * int类型----->string类型
     9          * string类型----->int类型
    10          */
    11         
    12         // int类型----->string类型
    13         int i = 2;
    14         
    15         // 方法1、使用String类的valueOf()方法
    16         String str1 = String.valueOf(i);
    17         System.out.println(str1);
    18         
    19         // 方法2、使用int类型和空字符串做加法
    20         String str2 = i + "";
    21         System.out.println(str2);
    22         
    23         // 方法3、使用Integer包装类作为中间桥梁,使用Integer的构造函数和toString方法
    24         Integer integer1 = new Integer(i);
    25         System.out.println(integer1.toString());
    26         
    27         // 方法4、使用Integer包装类的toString(int i)方法
    28         System.out.println(Integer.toString(i));
    29         
    30         System.out.println("------------------------------------------------");
    31         
    32         // string类型----->int类型
    33         String str = "123";
    34         
    35         // 方法1、使用Integer包装类作为中间桥梁,使用Integer的构造函数 和 intValue方法
    36         Integer integer2= new Integer(str);
    37         System.out.println(integer2.intValue());
    38         
    39         // 方法2、使用Integer包装类的parseInt方法
    40         int j = Integer.parseInt(str);
    41         System.out.println(j);
    42         
    43         // 执行异常:java.lang.NumberFormatException: For input string: "java"
    44         System.out.println(Integer.parseInt("java"));
    45     }
    46 }
     1 package cn.temptation;
     2 
     3 public class Sample04 {
     4     public static void main(String[] args) {
     5         /*
     6          * Integer包装类的常用成员方法:
     7          * 1、static String toString(int i, int radix) :返回用第二个参数指定基数表示的第一个参数的字符串表示形式。
     8          * 2、static int parseInt(String s, int radix) :使用第二个参数指定的基数,将字符串参数解析为有符号的整数。  
     9          */
    10         System.out.println(Integer.toString(2, 2));            // 10
    11         System.out.println(Integer.toString(10, 16));        // a
    12         System.out.println(Integer.toString(8, 8));            // 10
    13         
    14         System.out.println(Integer.toString(10, -2));        // 10
    15         System.out.println(Integer.toString(17, 17));        // 10
    16         System.out.println(Integer.toString(10, 17));        // a
    17         System.out.println(Integer.toString(16, 17));        // g
    18         
    19         // 查看Integer包装类的toString方法
    20         //if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
    21         //    radix = 10;
    22         // 查看Character包装类的字段
    23         //public static final int MIN_RADIX = 2;
    24         //public static final int MAX_RADIX = 36;
    25         
    26         System.out.println("------------------------------------");
    27         
    28         System.out.println(Integer.parseInt("2", 10));        // 2
    29         System.out.println(Integer.parseInt("7", 8));        // 7
    30         System.out.println(Integer.parseInt("F", 16));        // 15
    31         System.out.println(Integer.parseInt("10", 16));        // 16
    32         
    33     }
    34 }
     1 package cn.temptation;
     2 
     3 public class Sample05 {
     4     public static void main(String[] args) {
     5         /*
     6          * 自动装箱(boxing)   和  自动拆箱(unboxing)
     7          * 
     8          * 自动装箱:把基本数据类型装换为包装类类型(值类型----->引用数据类型)
     9          * 自动拆箱:把包装类类型装换为基本数据类型(引用数据类型----->值类型)
    10          */
    11         
    12         // 自动装箱
    13         Integer integer1 = new Integer(2);
    14         System.out.println(integer1);
    15         
    16         // 简化写法:把int类型的数字字面量直接赋值给Integer包装类的变量
    17         Integer integer2 = 3;
    18         // 通过反编译class文件,看到
    19 //        Integer integer2 = Integer.valueOf(3);
    20         System.out.println(integer2);
    21         
    22         System.out.println("---------------------------------------");
    23         
    24         Integer integer3 = 4;
    25         integer3 += 5;
    26         // 通过反编译class文件,看到
    27 //        Integer integer3 = Integer.valueOf(4);            // 自动装箱
    28 //        integer3 = Integer.valueOf(integer3.intValue() + 5);        // 先做自动拆箱,拿着基本数据类型int和另一个基本数据类型int做加法,再做自动装箱
    29         System.out.println(integer3);
    30         
    31         System.out.println("---------------------------------------");
    32         
    33         Integer integer4 = null;
    34         System.out.println(integer4);
    35         // 注意:37行语句上已经有警告:Null pointer access: The variable integer4 can only be null at this location
    36         // 执行异常:java.lang.NullPointerException
    37 //        System.out.println(integer4.toString());
    38         
    39         // 注意:41行语句上已经有警告:Null pointer access: This expression of type Integer is null but requires auto-unboxing
    40         // 执行异常:java.lang.NullPointerException
    41 //        integer4 += 6;
    42 //        System.out.println(integer4);
    43         
    44         // 对于包装类类型的对象进行非空验证
    45         // 注意:47行语句上已经有警告:Dead code,因为eclipse判断了integer4就是null
    46         if (integer4 != null) {
    47             integer4 += 6;
    48             System.out.println(integer4);
    49         }
    50     }
    51 }
     1 package cn.temptation;
     2 
     3 public class Sample06 {
     4     public static void main(String[] args) {
     5         // == 比较的是包装类对象的地址,自然不同;equals比较的是内容,自然相同
     6         Integer integer1 = new Integer(127);
     7         Integer integer2 = new Integer(127);
     8         System.out.println(integer1 == integer2);        // false
     9         System.out.println(integer1.equals(integer2));    // true
    10         
    11         System.out.println("----------------------------------------");
    12         
    13         // == 比较的是包装类对象的地址,自然不同;equals比较的是内容,自然相同
    14         Integer integer3 = new Integer(128);
    15         Integer integer4 = new Integer(128);
    16         System.out.println(integer3 == integer4);        // false
    17         System.out.println(integer3.equals(integer4));    // true
    18         
    19         System.out.println("----------------------------------------");
    20         
    21         Integer integer5 = 127;
    22         Integer integer6 = 127;
    23         System.out.println(integer5 == integer6);        // true
    24         System.out.println(integer5.equals(integer6));    // true
    25         
    26         System.out.println("----------------------------------------");
    27         
    28         Integer integer7 = 128;
    29         Integer integer8 = 128;
    30         System.out.println(integer7 == integer8);        // false
    31         System.out.println(integer7.equals(integer8));    // true
    32         
    33         // 查看反编译后的class文件
    34 //        Integer integer5 = Integer.valueOf(127);
    35 //        Integer integer6 = Integer.valueOf(127);
    36         
    37 //        Integer integer7 = Integer.valueOf(128);
    38 //        Integer integer8 = Integer.valueOf(128);
    39         
    40         // Integer.valueOf(127),传入127,使用byte缓冲区,所以使用==,比较的地址是同一个地址
    41         // Integer.valueOf(128),传入128,使用new创建新的空间,所以使用==,比较的地址是不同的地址
    42         
    43         // 推测可能是valueOf方法中的写法的差别
    44         // 查看Integer类的valueOf方法源码
    45 //        public static Integer valueOf(int i) {
    46 //            if (i >= IntegerCache.low && i <= IntegerCache.high)
    47 //                return IntegerCache.cache[i + (-IntegerCache.low)];
    48 //            return new Integer(i);
    49 //        }
    50         
    51         // 查看IntegerCache内部类的源码
    52 //        static final int low = -128;
    53 //        static final int high;
    54 //        static final Integer cache[];
    55 //        static {
    56 //            int h = 127;
    57 //            ...
    58 //        }
    59 //        high = h;
    60         
    61         // IntegerCache内部类的cache范围:-128~127,也称为byte缓冲区/byte缓存区
    62     }
    63 }
     1 package cn.temptation;
     2 
     3 public class Sample07 {
     4     public static void main(String[] args) {
     5         // 类 Character:在对象中包装一个基本类型 char 的值。Character 类型的对象包含类型为 char 的单个字段。
     6         
     7         /*
     8          * Character类的构造函数:
     9          * Character(char value) :构造一个新分配的 Character 对象,用以表示指定的 char 值。
    10          * 
    11          * Character类的常用成员方法:
    12          * 1、static boolean isUpperCase(char ch) :确定指定字符是否为大写字母。
    13          * 2、static boolean isLowerCase(char ch) :确定指定字符是否为小写字母。
    14          * 3、static boolean isDigit(char ch) :确定指定字符是否为数字。
    15          * 4、static char toUpperCase(char ch) :使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为大写。
    16          * 5、static char toLowerCase(char ch) :使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为小写。     
    17          */
    18         Character character = new Character('a');
    19         System.out.println(character);                // a
    20         System.out.println("------------------------------------");
    21         
    22         System.out.println(Character.isUpperCase('A'));        // true
    23         System.out.println(Character.isUpperCase('a'));        // false
    24         System.out.println(Character.isLowerCase('A'));        // false
    25         System.out.println(Character.isLowerCase('a'));        // true
    26         
    27         System.out.println(Character.isDigit('1'));        // true
    28         System.out.println(Character.isDigit('a'));        // false
    29         System.out.println("------------------------------------");
    30         
    31         System.out.println(Character.toUpperCase('a'));        // A
    32         System.out.println(Character.toLowerCase('A'));        // a
    33     }
    34 }
     1 package cn.temptation;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Sample08 {
     6     public static void main(String[] args) {
     7         // 需求:根据键盘录入的字符串(仅限大写字符、小写字符、数字字符),统计有多少个大写字符、多少个小写字符、多少个数字字符?(使用包装类)
     8         
     9         String str = "";
    10         // 大写字符个数
    11         int upperCount = 0;
    12         // 小写字符个数
    13         int lowerCount = 0;
    14         // 数字字符个数
    15         int digitCount = 0;
    16         
    17         System.out.println("输入一个字符串(仅限大写字符、小写字符、数字字符):");
    18         Scanner input = new Scanner(System.in);
    19         if (input.hasNextLine()) {
    20             str = input.nextLine();
    21         } else {
    22             System.out.println("输入不正确!");
    23         }
    24         input.close();
    25         
    26         char[] arrChar = str.toCharArray();
    27         
    28         for (char item : arrChar) {
    29             if (Character.isUpperCase(item)) {
    30                 upperCount++;
    31             } else if(Character.isLowerCase(item)) {
    32                 lowerCount++;
    33             } else if (Character.isDigit(item)) {
    34                 digitCount++;
    35             }
    36         }
    37         
    38         System.out.println("字符串" + str + "中,大写字符有:" + upperCount + "个,小写字符有:" + lowerCount + "个,数字字符有:" + digitCount + "个");
    39     }
    40 }
     1 package cn.temptation;
     2 
     3 public class Sample09 {
     4     public static void main(String[] args) {
     5         int[] arr1 = null;
     6         System.out.println(arr1);        // null
     7         System.out.println("--------------------------------");
     8         
     9         float[] arr2 = null;
    10         System.out.println(arr2);        // null
    11         System.out.println("--------------------------------");
    12         
    13         boolean[] arr3 = null;
    14         System.out.println(arr3);        // null
    15         System.out.println("--------------------------------");
    16         
    17         char[] arr4 = null;
    18         // 执行异常:java.lang.NullPointerException
    19         System.out.println(arr4);
    20         
    21         // 查看反编译class文件,发现没有区别
    22 //        int arr1[] = null;
    23 //        System.out.println(arr1);
    24 //        System.out.println("--------------------------------");
    25 //        float arr2[] = null;
    26 //        System.out.println(arr2);
    27 //        System.out.println("--------------------------------");
    28 //        boolean arr3[] = null;
    29 //        System.out.println(arr3);
    30 //        System.out.println("--------------------------------");
    31 //        char arr4[] = null;
    32 //        System.out.println(arr4);
    33         
    34         // 所以,推测打印方法println()是否有不同的处理
    35         // 通过查看println()方法,可以看出,打印int[]、float[]、boolean[]数组,都是调用String的valueOf方法,打印出"null"
    36         // 而打印char[]数组,调用write()方法,因为null没有相应的属性,所以使用时产生空指针异常
    37         
    38 //        public void println(Object x) {
    39 //            String s = String.valueOf(x);
    40 //            synchronized (this) {
    41 //                print(s);
    42 //                newLine();
    43 //            }
    44 //        }
    45 //        
    46 //        调用String类的valueOf方法
    47 //        public static String valueOf(Object obj) {
    48 //            return (obj == null) ? "null" : obj.toString();
    49 //        }
    50 //        
    51 //        public void println(char x[]) {
    52 //            synchronized (this) {
    53 //                print(x);
    54 //                newLine();
    55 //            }
    56 //        }
    57         
    58 //        public void print(char s[]) {
    59 //            write(s);
    60 //        }
    61     }
    62 }
  • 相关阅读:
    SKAction类
    SpriteKit所有的类
    Reachability下载地址
    IOS学习教程
    SpriteKit游戏开发
    APP开发者到期续费说明
    Unique Paths
    Letter Combinations of a Phone Number
    Reverse Nodes in k-Group
    Remove Nth Node From End of List
  • 原文地址:https://www.cnblogs.com/iflytek/p/6568350.html
Copyright © 2011-2022 走看看