有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    如果试图使用未定义的变量,代码将无法编译,因为在Java中,变量必须在使用之前定义

    但请注意,变量可以为null,可以检查其中一个是否为null以避免NullPointerException

    if (var != null) {
        //...
    }
    
  2. # 2 楼答案

    如果我们试图在java中使用未定义的变量,它将抛出异常。为了解决这些问题,我们使用包装类并将其分配给null

    Integer a = null; //correct
    int a = null;//error
    
  3. # 3 楼答案

    if (variableName != null)
    {
    //Do something if the variable is declared.        
    }
    else
    {
    //Do something if the variable doesn't have a value        
    }
    

    我认为应该这样做