c1 == null || c1.equals("")

解决方案 »

  1.   

    c1 == null || c1.equals("") || (c1.trim()).equals("")
      

  2.   

    不得不更正一下:
    使用c1.equals("")来判断是否为null,是不正确得。因为如果c1 是 null 得话,就会抛出异常~~~
      

  3.   

    c1 == null || c1.equals("") || (c1.trim()).equals("")
    前面通过了就直接跳到里面,没有问题.
    c1.equals("")是判断是否为"",楼主并没有指明他要的空是null还是"",
      

  4.   

    String c1;
    if (c1!=null)
    {}===============
    c1 == null || c1.equals("")
    ===============
    各位!c1都没有初始化,怎么不都是错呀!!!
      

  5.   

    ft,用的是||操作,不会有问题的。
    false || true || false || true,其实这里只会判断到第二个true,接下来的根本不会执行的。所以: flashroom(找到啦)的说法有问题。
      

  6.   

    to: StevenWSF(算死草) 
    String c1; //调用了构造器,怎么会没有初始化呢?
      

  7.   

    takecare(大厅)
    那你自己试试吧
      

  8.   

    补充:出于性能考虑,不要用c1.equals(""),使用c1.length=0。
      

  9.   

    to: StevenWSF(算死草) 
    看看string.java的源码吧,下面的是String的缺省构造器的源码:
        /**
         * Initializes a newly created <code>String</code> object so that it
         * represents an empty character sequence.
         */
        public String() {
            value = new char[0];
        }我说的有什么问题吗?
      

  10.   

    同意 wangwenyou(王文友) 的说法。方法调用确实要比访问成员慢的多。
      

  11.   

    to:takecare(大厅)class  test
    {
    public static void main(String[] args) 
    {
    String c1;
    if(c1==null ){
    System.out.println("ok");
    } }
    }test.java:6: variable c1 might not have been initialized
    if(c1==null ){
                       ^
    1 error
      

  12.   

    to: StevenWSF(算死草) 
    对,编译出错。
      

  13.   

    作为一个对象变量,必须要实例化才能使用。
    String c1;改为 String c1=new String("");