null的伟大与可怕 之 String字符串方法2

null的伟大与可怕 之 String字符串方法2

null的伟大与可怕 之 String字符串 1
http://blog.csdn.net/sunrainamazing/article/details/71591334
null的伟大与可怕 之 String字符串方法2
http://blog.csdn.net/sunrainamazing/article/details/71593030
null的伟大与可怕 之 Optional操作 3
http://blog.csdn.net/sunrainamazing/article/details/71596437

package sun.rain.amazing.strnull;

import org.junit.Test;

/**
 * Created by sunRainAmazing on SUN_RAIN_AMAZING
 * @author sunRainAmazing
 */

public class MethodNull {

    private final static String s1 ="null";
    private final static String s2 = null;
    private final static String s3 = "";

    @Test
    public void initVariable(){
        System.out.println(s1);
        System.out.println(s2);

        //对print的引用不明确
//      System.out.print(null);
    }

    /**
     *  运行时异常
     *  contains 方法 内部调用的 时 indexOf 方法
     *
     *  但是 contains 的返回值是 boolean
     *      indexOf  的返回值是 int
     *  String contains 和 indexOf 方法无法对null进行运算
     *
     * 但是 == 可以
     */
    @Test
    public void testMethod() {

        //报空指针异常 java.lang.NullPointerException
//      System.out.println(s1.indexOf(s2));
//      System.out.println(s1.contains(s2));

        // 报空指针异常 java.lang.NullPointerException
//      System.out.println(s2.contains(s1));
//      System.out.println(s2.contains(s1));



        //报空指针异常  java.lang.NullPointerException
//      System.out.println(s1.indexOf(null));
//      System.out.println(s1.contains(null));

        //编译错误
//      System.out.println(null.indexOf(s1));
//      System.out.println(null.contains(s1));

//      System.out.println(s1.indexOf(s3));//0
//      System.out.println(s1.contains(s3));//true



//      System.out.println(null==s1);//false
//      System.out.println(s2==s1);//false

        System.out.println(s2 == null);//true
        System.out.println(null == null);//true
    }

    /*
    indexOf
        public int indexOf(String str)
            返回指定子字符串在此字符串中第一次出现处的索引。返回的整数是
            this.startsWith(str, k) 为 true 的最小 k 值。

        参数:
        str - 任意字符串。
        返回:
        如果字符串参数作为一个子字符串在此对象中出现,
            则返回第一个这种子字符串的第一个字符的索引;
            如果它不作为一个子字符串出现,则返回 -1。

    contains
        public boolean contains(CharSequence s)
        当且仅当此字符串包含指定的 char 值序列时,返回 true。

        参数:
            s - 要搜索的序列
        返回:
            如果此字符串包含 s,则返回 true,否则返回 false
        抛出:
            NullPointerException - 如果 s 为 null

     */
    @Test
    public void testMethod1(){
        //以下结果都是0
        System.out.println("".indexOf(""));
        System.out.println(s3.indexOf(""));
        System.out.println(s3.indexOf(s3));


        //indexOf方法没有外抛异常声明  null没有length属性
        //报空指针异常  java.lang.NullPointerException
//      System.out.println(s3.indexOf(s2)); 
//      System.out.println(s3.indexOf(null)); 


        //以下结果全是true

        System.out.println("".contains(""));
        System.out.println(s3.contains(""));
        System.out.println(s3.contains(s3));

    }



    /**
     * equals()方法可以比较null
     * 但通常将 null作为参数 进行判断
     * 而不是null.equals(str)  -- 会报 NPE 异常
     */
    @Test
    public void testEquals(){
        System.out.println(s1.equals(s2));
        System.out.println(null instanceof String);

        System.out.println(null+"");
        System.out.println(null+"" instanceof String);

    }



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值