• java入门篇4 --- 引用类型


    1.String字符串

    我们常用的字符串属于引用类型,它可以包含0至n个字符,String使用双引号,字符串的拼接也可以使用+

    public class HelloWorld {
    public static void main(String[] args) {
    String s = "我爱中国";
    String s1 = "i " + "love " + "china";
    String s2 = null;
    // String s3; // Error:(9, 28) java: 可能尚未初始化变量s3
    System.out.println(s); // 我爱中国
    System.out.println(s2); // null
    System.out.println(s1); // i love china
    }
    }

    字符串不可变,如果进行重新赋值,那么变量就会指向新的内存

    对于字符串来讲,可以将将其赋予null,注意null跟“”不一样,后者是变量指向“”这个字符串,另外,如果字符串不进行赋值就调用,就会报错

    字符串也有内部命令可以进行拼接字符串,格式化字符串,例如:

    public class HelloWorld {
        public static void main(String[] args) {
            String s = "我爱".concat("中国");
            System.out.println(s);  // 我爱中国
            String x = String.format("%s爱中国%d年", "我", 100);
            System.out.println(x);  // 我爱中国100年
        }
    }

    对于字符串的操作,先讲数组再将

    2.数组

    定义一个数组使用:类型[],这个跟go是相反的,不要搞混了,数组有两个特点:

    数组初始化未赋值状态下,里面所有的值都是空值

    数组的大小一旦定义,就不可变,例如 int[] x = new int[5],这个数组的大小就是5。

    public class HelloWorld {
        public static void main(String[] args) {
            int[] n = new int[5];
            String[] m = new String[5];
            System.out.println(n);  // [I@38af3868
            System.out.println(n[0]);  // 0
            System.out.println(m);  // [Ljava.lang.String;@77459877
            System.out.println(m[0]);  // null
            n[1] = 5;
            m[4] = "a";
            System.out.println(n);  // [I@38af3868
            System.out.println(n[1]); // 5
            System.out.println(m); // [Ljava.lang.String;@77459877
            System.out.println(m[4]);  // a
            int[] x = new int[]{1, 2, 3, 4, 5};  // 这种写法不用声明数组长度,由编译器自行推断出
            System.out.println(x);  // [I@5b2133b1
            System.out.println(x[1]);  // 2
            x[1] = 100;
            System.out.println(x);  // [I@5b2133b1
            System.out.println(x[1]);  // 100
        }
    }

    从上述可以看出,打印出来的数字是地址,对数组内容进行更改,数组地址不会发生辩护,因此可以判定,数组没有发生变化,数组中的元素指向了不同的内存

    3.字符串操作

    下面引用了部分字符串操作

    public class HelloWorld {
        public static void main(String[] args) {
            char[] s = new char[]{'我', '爱', '中', '国', '爱'};  
            String x = new String(s);
            System.out.println(x);  //我爱中国爱
            System.out.println(x.compareTo("我爱中国"));  //1  把这个字符串和另一个对象比较。
            System.out.println(x.compareTo("我爱中国爱"));  //0  把这个字符串和另一个对象比较。0代表完全一致
            System.out.println(x.split(""));  //[Ljava.lang.String;@38af3868  根据给定正则表达式的匹配拆分此字符串。
            System.out.println(x.split("")[0]);  //我 根据给定正则表达式的匹配拆分此字符串。
            System.out.println(x.replace('爱', '你'));  //我你中国你  返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
            System.out.println(x.replaceFirst("爱", "love"));  //我love中国爱   使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。。
            System.out.println(x.length());  //5  返回长度
            System.out.println(x.charAt(1));  // 爱  返回指定索引处的 char 值。
            System.out.println(x.contains("我"));  // true  是否包含指定指定字符串
            System.out.println(x.endsWith("爱"));  // true  是否以某字符串结尾
            System.out.println(x.indexOf(1));  // -1   返回指定子字符串在此字符串中第一次出现处的索引。-1代表没找到
            System.out.println(x.indexOf("爱"));  // 1   返回指定子字符串在此字符串中第一次出现处的索引。
            System.out.println(x.indexOf("我", 2));  // -1  判断index的位置是否是指定字符串
    
            String n = " n  N  ";
            System.out.println(n);  // " n  N  "
            System.out.println(n.trim());  // "n  N"  去掉前后的空白
            System.out.println(n.toUpperCase());  // " N  N  "  所有字母大写
            System.out.println(n.toLowerCase());  // " n  n  "  所有字母小写
        }
    }

    数组操作会放在流程控制里面讲。

  • 相关阅读:
    GRIDVIEW导出到EXCEL
    数据表示:字节 高位低位
    js学习笔记0
    12奇招,循序删除顽固的文件
    加快开关机速度
    PHP正则表达式的快速学习
    firefox下height不能自动撑开的解决办法
    给MySQL加密(也适用于Wamp5中)
    我的电脑创建资源管理器
    css 圆角7种CSS圆角框解决方案
  • 原文地址:https://www.cnblogs.com/yangshixiong/p/12157908.html
Copyright © 2020-2023  润新知