Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中编译时变量初始化出错_Java - Fatal编程技术网

java中编译时变量初始化出错

java中编译时变量初始化出错,java,Java,我有两个Java程序 程序一 class Ideone { public static void main (String[] args) { double price = 10; String model; if (price > 10) model = "Smartphone"; else if (price <= 10) model = "landl

我有两个Java程序

程序一

class Ideone
{
    public static void main (String[] args)
    {

        double price = 10;
        String model;
        if (price > 10)
            model = "Smartphone";
        else if (price <= 10)
            model = "landline";
            System.out.println(model);
    }
}
类表意符
{
公共静态void main(字符串[]args)
{
双倍价格=10;
弦模型;
如果(价格>10)
model=“智能手机”;

否则,如果(price编译器足够聪明。

在第二种情况下,完全填充所有条件。在所有条件之后,还有一部分要保存

但在第一种情况下,没有其他部分。这意味着,如果没有条件通过,则有一个空白字段

您必须提供一个确保初始化部分

double price = 10;
        String model;
        if (price > 10)
            model = "Smartphone";
        else if (price <= 10)
            model = "landline";
        else
           model = null
双倍价格=10;
弦模型;
如果(价格>10)
model=“智能手机”;
如果有的话(价格10)
model=“智能手机”;
如果(价格)
我在这里的问题是,既然这两个项目几乎相似,为什么我
我得到的变量可能尚未初始化

实际上它们不一样。您有一个字段名
model
,它没有初始化为:

String model = null;

编译器正在考虑未被任何if条件初始化的情况,这就是它抱怨的原因。在第一种情况下,
else
缺失。

问题是,在第一个程序中,您可以看到您使用了
if else if
语句,编译器无法确定这些条件将求值为true或false,导致变量未初始化,因此您会得到错误

但是在第二个程序中,您可以看到,您使用了if-else语句,因此在本例中,编译器知道,如果if条件不满足,那么它(编译器)可以执行else语句并将值赋给变量

所以把这个放在你的第一个节目里

String model;
if (price > 10)
model = "Smartphone";
else 
model = "landline";
System.out.println(model);

我删除了else if的if部分,只是为了让您知道我在第一个代码段中做了什么,虽然从逻辑上讲,if或else必须出现,但Java编译器并不“知道”这。它假定
if
语句都可能失败,因此会向您发出警告消息。下面是代码中发生的情况的详细说明:

double price = 10;
String model;
if (price > 10)
    model = "Smartphone";
else if (price <= 10)           // the Java compiler doesn't "know" that one
    model = "landline";         // of these two cases will always occur
    System.out.println(model);

在第二个代码段中,您使用显式的
if-else
进行初始化,因此编译器在这一点上保持沉默。

在第一种情况下,将
else-if
更改为just
else
。Java编译器不够聪明,无法实现这两个条件中的一个将100%执行。

在第一种情况下t case您需要初始化,因为如果两个条件都为false(if和else if),那么将显示什么样的默认值。 在你的第一个代码中添加一个else部分,它不会给出这样的警告


在第二种情况下,当第一部分(如果(条件)==false)然后执行else部分,因此不存在变量color没有任何值的情况。

color将始终初始化,只有在某些情况下,模型才会初始化,编译器就是这样认为的。+1 for Java编译器不够聪明,无法意识到这两个条件中的一个将100%执行。编译器是智能的。这就是为什么它创建了一个错误,以确保始终存在初始化部分。
String model;
if (price > 10)
model = "Smartphone";
else 
model = "landline";
System.out.println(model);
double price = 10;
String model;
if (price > 10)
    model = "Smartphone";
else if (price <= 10)           // the Java compiler doesn't "know" that one
    model = "landline";         // of these two cases will always occur
    System.out.println(model);
double price = 10;
String model;
if (price > 10)
    model = "Smartphone";
else {
    model = "landline";         // in case the if fails, this else will ALWAYS
    System.out.println(model);  // happen, so 'model' is guaranteed to get
}                               // intiailized