String三者的区别,代理模式,异常处理,#finally的使用方式,字符串,包装类,字符串的方法

image

(1)字符修改上的区别(主要,见上面分析)

(2)初始化上的区别,String可以空赋值,后者不行,报错

①String

String s = null;

String s = “abc”;

②StringBuffer

StringBuffer s = null; //结果警告:Null pointer access: The variable result can only be null at this location

StringBuffer s = new StringBuffer();//StringBuffer对象是一个空的对象

StringBuffer s = new StringBuffer(“abc”);//创建带有内容的StringBuffer对象,对象的内容就是字符串”

小结:(1)如果要操作少量的数据用 String;

(2)多线程操作字符串缓冲区下操作大量数据 StringBuffer;

(3)单线程操作字符串缓冲区下操作大量数据 StringBuilder。

判断字符串相等用equals

图片发自App

异常处理

package com.neuedu.chapter4_0313;

public class Demo1 {
    public void test() {
        try {
            Class.forName("123");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws ClassNotFoundException {
        /*
         * 
         * 异常:程序遇到的“小”问题
         *   如:数组越界,空指针,分母为0
         *   
         *   简述Exception和Error的区别
         *   Exception是一些通过代码能够解决的小问题
         *   
         *   
         *   Throwable
         * 
         *  将参数代表的类加载到方法区
         * ①自行处理try{放有可能纯在异常的代码}catch(){}
         * try块不能单独使用,当try块检测出异常时,try块将程序运行权转交给对应的catch块
         * catch(捕获的异常类对象)捕获
         * catch块中如果参数是Exception父类类型,这个catch块必须写最后
         * 
         * ②向上抛出(规避异常)
         * Class.forName("123")
         */
        Class.forName("123");
        

    }

}

finally的使用方式

package com.neuedu.chapter4_0313;

public class Demo2 {
    
    public static void main(String[] args) {
        try {
            Class.forName("123");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            System.out.println("hello");
        }
        // finally块一般与try..catch配合使用,用来一定要执行的代码(回收资源,关闭文件,关闭链接)
        /*
         * final,finally,finalize有什么区别
         * 
         * final修饰类(不能被继承),方法(不能被子类重写),属性(不能修改))
         * 
         * finally一般与try..catch配合使用,用来执行一定要执行的代码(回收资源--关闭文件关闭链接)
         * 
         * finalize与GC垃圾回收机制相关,JVM会把没有引用指向的对象视为垃圾,
         * JVM会自动调用这个对象的finalize()回收内存
         * 
         * 
         * 
         */
        
        
    }
}

自己建立异常

package com.neuedu.chapter4_0313;

public class Person {
    int head;
    int arms;
    public static void main(String[] args) {
        Person p = new Person(2,6);
        if(p.head !=1) {
            try {
//              主动触发
                throw new HeadExcetion();
            } catch (HeadExcetion e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
    }
    public Person(int head, int arms) {
        super();
        this.head = head;
        this.arms = arms;
    }
    
    
    
    
}

equals的判断方式以及是否可以子类重写

package com.neuedu.chapter1_0313;

public class Demo1 {

    public static void main(String[] args) {
        /*
         * Object类:java中所有类的父亲,唯一一个没有父亲的类
         *  equals----pulbic boolean equals(Object obj):比较两个引用数据类型是否相等,但子类可以重写,指定自己的比较规则
         *  ==:可以比较基本数据类型和引用数据类型是比较的字面真值,
         *      比较引用数据类型时,比较的是地址是否相等
         * 
         * 字符串的字面量会以对象的形式保存在字符串常量池当中,
         * 字符串常量池通常在方法区
         * 这两种方式有什么不一样↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
         * String str1 = "hello";String str2 = new String("hello");
         * 只创建了一个对象;堆里一个对象常量池里一个
         * 
         * 
         * 
         */
//      Demo1 d1 = new Demo1();
//      Demo1 d2 = new Demo1();
//      System.out.println(d1 == d2);
        
        String str1 = "hello";
        String str2 = new String("hello");
        System.out.println(str1== str2);
        System.out.println(str1.equals(str2));
        
        String str3 = "hell"+new String("o");
        String str4 = new String("hello");
        System.out.println(str3== str4);
        System.out.println(str3.equals(str4));


    }

}

哈希码hashCode()

package com.neuedu.chapter1_0313;

public class Demo2 {
public static void main(String[] args) {
//  public int hashCode(): 该方法返回对象的哈希码
//  如果两个对象用equals比较返回true,两个对象的哈希码必定一样
//  两个对象的哈希码一样,如果两个对象用equals比较不一定返回true
    Demo2 d = new Demo2();
    System.out.println(d);
    System.out.println(d.hashCode());
}
}

toString()方式

package com.neuedu.chapter1_0313;

public class Demo3 {
    
    public static void main(String[] args) {
            
        // public String toString():可以重写这个方法返回值定为该类属性,
        //                          遍历数组时可以不用.name等
        Demo3 d = new Demo3();
        System.out.println(d);
        System.out.println(d.toString());
    }   
}

包装类

package com.neuedu.chapter1_0313;

public class Demo4 {

    public static void main(String[] args) {
        // 包装类:将基本数据类型包装成对象
        // Everything is object
        // java中不把基本数据类型视为对象
        // 1、像基本数据一样去使用
        int num1 = 100;
        // 自动装箱
        Integer num2 = 100;// Integer num2 = new Integer(100);
        // 自动拆箱
        System.out.println(num1+num2);
        
        
        // 2、与字符串之间进行类型转换
        String strNum1="1000";
        String strNum2="2000";
        System.out.println(strNum1+strNum2);
        // 字符串转成其他基本数据类型
        int num4 = Integer.valueOf(strNum1);
        int num3 = Integer.parseInt(strNum1);
        System.out.println(num3+1+num4);
        
        //基本数据类型转成其他字符串
        int num5 =100;
        String strNum3 = num1+"";
        String strNum4 = String.valueOf(num5);
        String strNum5 = Double.toString(num5);
    }

}

字符串

package com.neuedu.chapter1_0313;

public class StringDemo1 {
    public static void main(String[] args) {
        /*
         * 
         * 字符串类
         *   String:代表一组“不可变”的字符串,
         *   对他的所有修改都是在创建新的字符串不是在原有基础上修改
         *   
         *   StringBuffer
         *   StringBuilder
         * 
         * 
         * 
         * 
         */
        String str = "hello";
        str = str+"world";
        System.out.println(str);
        
        //1、equals比较字符串内容
        System.out.println("hello".equals("Hello"));
        System.out.println("hello".equalsIgnoreCase("Hello"));
        
        //2、String toUpperCase()String toLowerCase()
        System.out.println("hello".toUpperCase());
        System.out.println("HELLO".toLowerCase());
        
        //3、char charAt(int) 返回指定索引处的字符 查不到是-1
        System.out.println("hello".charAt(1));// e
        
        //4、String substring(int begin)  截取
        //   String substring(int begin,int end)  截取
        System.out.println("hello".substring(2));
        System.out.println("hello".substring(1, 3));
        
        //5、int indexOf(xxx)返回一个字符串在另一字符串当中第一次出现的索引,若一次都没有出现返回-1
        //  int lastIndexOf(xxx)返回一个字符串在另一字符串当中第一次出现的索引,若一次都没有出现返回-1
        System.out.println("abcdfg".indexOf("ef"));
        System.out.println("abcdfg".lastIndexOf("g"));
    }
}

在字符串中查找一个字母

package com.neuedu.chapter1_0313;

public class Practice2 {

    public static void main(String[] args) {
        // 在一个字符串中查找e出现的次数
        String str = "gjkaeeelsgjeeleaeseeeghasgeehksdj";
        char[] c = str.toCharArray();
        int count = 0;
        for(char ch : c) {
            if(ch == 'e') {
                count++;
            }
            
        }System.out.print(count);
        // 查单词:在一个字符串中查找另一个字符串出现的次数

    }

}

作业:在一个字符串中查找单词

package com.neuedu.chapter1_0313;

public class Practice3 {
    

/**
 * 
 * @param father
 * @param son
 * @return 次数
 */
    public int findWord(String father,String son) {
    return 1;   
    }
    public static void main(String[] args) {



    }

}


打印字符串的每个字母和字母出现的次数

public static void main(String[] args) {
        String str="fdsfagsagsads";
        char[] a=str.toCharArray();
        HashMap map = new HashMap();
        for(char ch: a ) {
            if(!map.containsKey(String.valueOf(ch))) {
                map.put(String.valueOf(ch), 1);
            }else {
                int index =map.get(String.valueOf(ch));
                map.put(String.valueOf(ch), ++index);
            }
        }
        for(Map.Entry entry : map.entrySet()) {
            System.out.println("字母是:"+entry.getKey()+"次数为:"+entry.getValue());
        }
    }

你可能感兴趣的:(String三者的区别,代理模式,异常处理,#finally的使用方式,字符串,包装类,字符串的方法)