Java中的变量默认值 [英] Variable default value in Java

查看:499
本文介绍了Java中的变量默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的每个类型在声明时都具有原始值。本文包含原始数据类型的说明原始数据类型 。知道这一点,为什么Eclipse显示错误告诉我变量可能尚未初始化?如果我有例如

Every type in Java has a primitive value when declared. This article contains a description for primitive data types Primitive Data Types. Knowing this, why does Eclipse show an error telling me the variable may not have been initialized? If I have for example

int x;
x++;


推荐答案

从链接


局部变量略有不同;编译器永远不会为未初始化的局部变量分配默认值。如果无法初始化声明它的局部变量,请确保在尝试使用它之前为其赋值。访问未初始化的局部变量将导致编译时错误。

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

这篇关于Java中的变量默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆