美文网首页
7.28下午学习总结

7.28下午学习总结

作者: lufaqiang | 来源:发表于2017-07-28 21:34 被阅读0次

    包装类的亨元模式

    • 顾名思义:共享元对象。如果在一个系统中存在多个相同的对象,那么只需要共享一份对象的拷贝,而不必为每一次使用创建新的对象。
      享元模式是为数不多的、只为提升系统性能而生的设计模式。它的主要作用就是复用对象,以节省内存空间和对象创建时间。
    public class day1 {
    public static void main(String[] args) {
        function();
    }
    public static void function(){
        String num ="6789";
        String num1 ="6789";
        String num2=new String("6789");
        System.out.println(num==num1);
        System.out.println(num2==num);
            System.out.println(num2==num1
        
    }
    }
    

    程序输出结果:
    true
    false
    false

    序列化

    什么事java对象序列化

    • Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的生命周期更长。但在现实应用中,就可能要求在JVM停止运行之后能够保存(持久化)指定的对象,并在将来重新读取被保存的对象。Java对象序列化就能够帮助我们实现该功能。
    • 使用Java对象序列化,在保存对象时,会把其状态保存为一组字节,在未来,再将这些字节组装成对象。必须注意地是,对象序列化保存的是对象的"状态",即它的成员变量。由此可知,对象序列化不会关注类中的静态变量。
    • 除了在持久化对象时会用到对象序列化之外,当使用RMI(远程方法调用),或在网络中传递对象时,都会用到对象序列化。Java序列化API为处理对象序列化提供了一个标准机制,该API简单易用。

    String类的常用方法

    length() 字符串的长度

    charAt() 截取一个字符

    public static void function_5(){
        String arr1 = "aBcd";
        System.out.println(arr1.charAt(2));
    }
    

    输出结果:
    c

    public static void function_2(){
        String arr = "jkdakjqj";
        System.out.println(arr.length());
    }
    

    输出结果:8
    toCharArray()转换为字符数组

    public static void function_3(){
        String arr = "jkdakjqj";//定义字符串
        char[] ch = arr.toCharArray();//将字符串转成字符数组
        for(int i = 0;i<ch.length;i++){
            System.out.print(ch[i]);//遍历数组
         }
    

    输出结果:jkdakjqj
    equals()和equalsIgnoreCase() 比较两个字符串

    public static void function_4(){
        String arr1 = "aBcd";
        String arr2 = "abcd";
        boolean s1 = arr1.equals(arr2);//两个字符串中字符完全相等,返回true
        boolean s2 = arr1.equalsIgnoreCase(arr2);//判断字符串是否完全相等,忽略大小写
        System.out.println(s1);
        System.out.println(s2);
    }
    

    输出结果:false ture
    startsWith()方法决定是否以特定字符串开始,

    public static void function_6(){
        String arr1 = "abcd";
        boolean b = arr1.startsWith("ab");// 判断"ab"是不是"abcd"的前缀
        System.out.println(b);
    }
    

    输出结果:ture
    endWith()方法决定是否以特定字符串结束

    public static void function_7(){
        String arr1 = "abcd";
        boolean b = arr1.endWith("cv");// 判断"ab"是不是"abcd"的前缀
        System.out.println(b);
    }
    

    输出结果:false
    compareTo()和compareToIgnoreCase() 比较字符串

    public static void function_4(){
        String arr1 = "aBcd";
        String arr2 = "abcd";
        int s1 = arr1.compareTo(arr2);
        int s2 = arr1.compareToIgnoreCase(arr2);
        System.out.println(s1);
        System.out.println(s2);
    }
    

    输出结果:-32 0

    indexOf() 查找字符或者子串第一次出现的地方。�lastIndexOf() 查找字符
    或者子串是后一次出现的地方。

    public static void function_4(){
        String arr1 = "aBcbdb"; 
        int s1 = arr1.indexOf("b"); 
        System.out.println(s1);//返回指定字符在此字符串中第一次出现处的索引。
        
    }
    

    输出结果:3
    substring()截取字符串

    public static void function_4(){
        String arr1 = "aBcbdb";
        String s1 = arr1.substring(3);//包含头,后面的全要
        System.out.println(s1);
        String s2 = arr1.substring(1,3);//包含头,尾巴不要  
        System.out.println(s2);
        
    }
    

    输出结果:bdb
    Bc
    replace() 替换

    public static void function_4(){
        String arr1 = "aBcbdb";
        String arr2 = "dsa";
        String s1 = arr1.replace(arr1, arr2);
        System.out.println(s1);
    }
    

    输出结果:dsa

    concat() 连接两个字符串
    trim() 去掉起始和结尾的空格
    toLowerCase()/ toUpperCase() 大小写
    split(String str)//将str作为分隔符进行字符串分解

    public static void function_4(){
        String arr1 = new String("abc");
        String arr2 = "abc";
        System.out.println(arr1==arr2);
        System.out.println(arr1.equals(arr2));      
    }
    

    输出结果:
    false
    true

    包装类的常用方法

    以Integer为例

    MIN_VALUE = 0x80000000;
    MAX_VALUE = 0x7fffffff;

    public static void function(){
        
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
    }
    

    输出结果:
    2147483647
    -2147483648
    byteValue() 取得用byte类型表示的整数

    public static void function(){
        Integer i = 562;
        System.out.println(i.byteValue());
    }
    

    输出结果:50
    compareTo/compare 比较大小

    public static void function(){
        Integer  i = 4521;
        Integer  j = 1222;
        Integer  k = 1244;
        System.out.println(i.compareTo(j));
        System.out.println(Integer.compare(j,k));
        
    }
    

    输出结果:
    1
    -1
    toBinaryString(int i) 给定一个int类型数据,返回这个数据的二进制字符串。

    public static void function(){
        String str = Integer.toBinaryString(230);
        System.out.println(str);
    }
    

    输出结果:
    11100110
    bitCount(int i) 给定一个int类型数据,返回这个数据的二进制串中“1”的总数量。
    Integer decode(String nm) 给定一个10进制,8进制,16进制中任何一种进制的字符串,该方法可以将传入的字符串转化为10进制数字的Integer类型并返回。

    相关文章

      网友评论

          本文标题:7.28下午学习总结

          本文链接:https://www.haomeiwen.com/subject/hxqflxtx.html