简体   繁体   English

在Java中处理未初始化的字符串

[英]Handling uninitialized Strings in Java

Here's part of a small program I'm doing as a homework assignment: 这是我作为家庭作业所做的一个小程序的一部分:

public Exam maxGrade() {
    Node p = firstNode;
    int max = 0;
    String name;
    while (p!=null) {
        if (p.info.getGrade()>max) {
            max = p.info.getGrade();
            name = p.info.getName();
        }
        p = p.next;
    }

    if (name==null)
        return null;
    else
        return searchByName(name);
}

So when I go ahead and compile, the compiler outputs this message: 因此,当我继续编译时,编译器输出以下消息:

Student.java:127: error: variable name might not have been initialized if (name==null) Student.java:127:错误:如果(name == null),可能尚未初始化变量名称

The problem is easily solved by substituting the fourth line with: 用以下代码替换第四行可以很容易地解决这个问题:

String name = null;

Now, I can see some logic in this. 现在,我可以看到一些逻辑。 But I'd really like to grasp the workings behind the problem. 但我真的很想掌握这个问题背后的运作方式。 I mean, it seems reasonable that the compiler checks whether a variable is initialized if it sees you're doing something with it in your code, but I don't think I'm doing anything that NEEDS the variable to be initialized. 我的意思是,如果变量在你的代码中看到你正在对它做一些事情,那么编译器检查一个变量是否被初始化似乎是合理的,但我认为我没有做任何需要初始化变量的事情。

According to sources like this when I simply declare my String (or any other Object) variable "name", it already points to null. 据消息人士透露像这样的时候,我只是申报我的字符串(或任何其它对象)变量“名”,它已经指向NULL。 Then why is it considered an anomaly to simply check if that value is null? 那么为什么仅仅检查该值是否为空它被认为是异常? Will the compiler consider an error anything that I do to my uninitialized variable other than assignments? 除了赋值之外,编译器是否会考虑我对未初始化变量所做的任何错误?

Fields of object type are initialized to null by default. 默认情况下,对象类型的字段初始化为null。 Array members are also initialized to null by default. 默认情况下,数组成员也初始化为null。

Local variables are not - they must be initialized explicitly. 局部变量不是 - 它们必须明确初始化。

This is a good thing. 这是一件好事。 Uninitialized variables are frequently an indication of error. 未初始化的变量通常是错误的指示。

From "Initial Values of Variables" in chapter 4 of the Java Language Specification: 来自Java语言规范第4章中的“变量的初始值”

A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16). 局部变量(§14.4,§14.14)必须在使用前通过初始化(§14.4)或赋值(§15.26)显式赋予值,其方式可以使用明确赋值规则进行验证(§ 16)。

The compiler requires that you initialize the Object to be null if you're making any assumption as to its value. 如果您对其值进行任何假设,编译器要求您将Object初始化为null This is simply a (very useful) precaution. 这只是一个(非常有用的)预防措施。

Addendum:The compiler cant check the semantics of your program. 附录:编译器无法检查程序的语义。 Even if you know that a variable is initilized before its first usage the compiler cant. 即使你知道变量在第一次使用之前被启动,编译器也不能。

consider the following function: 考虑以下功能:

public boolean mod2(int i){
    if(i % 2 == 0){
        return true;
    }
    if(i % 2 != 0){
        return false;
    }
}

We all know that this method would always return true or false. 我们都知道这种方法总会返回true或false。 The compiler instead cant ensure that there will always be a return value because he has to know that there will only those two results. 编译器不能确保始终存在返回值,因为他必须知道只有那两个结果。 So the compiler will report an error about a missing return statement. 因此编译器将报告有关缺少return语句的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM